Thread: working with simulated 2 and 3 dimensonal space

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    38

    working with simulated 2 and 3 dimensonal space

    simple question. if i wanted to simulate a 3d volume (not talking about displaying it as a picture, just simulating) , would the proper way to do this be through a 3d array as in
    Code:
     
    object_1=bigarray [x][y][z];
    object_2=bigarray[a][b][c];
    if (object_1==object_2){
    
    cout<<”aaaaahhhhhhhhhhh! colision! we're all gonna die!"
    
    }
    okay, i understand that theat wasn't exactly the best code for what i'm talking about, but the concept remains; is this kind of array good for handling, say a simulation of particles like in the dust toy, or is there some concept that i havent gotten to yet that would better handle this kind of problem, and perhaps use less memory than this?


    if i havent made myself clear enough, feel free to tell me and i'll try to elaborate, but trolls be warned!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you mean "position", then sure three values will tell you a position. If you wanted a bunch of positions (i.e. a particle system), then you'd make position a "thing" and then make an array (or more like it, a vector) of those things.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    38
    On second thought, "Object" is probably a bad name for a variable on a forum dedicated to an OOP language.... Position. I apologize for my hastily written post. It's just that the whole concept of this being so simple threw me off. I kept thinking "it's gotta be more complicated than this". Ha ha.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Just to clarify, tabstop did not validate the code you posted. Position can be expressed as 3 values for 3D space, however you cannot directly compare arrays like that unless you overload the '==' operator. What is it that you are trying to accomplish exactly?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    38
    @AndrewHunter:
    Just to clarify, tabstop did not validate the code you posted. Position can be expressed as 3 values for 3D space, however you cannot directly compare arrays like that unless you overload the '==' operator.
    I understand. That's the meaning behind me apologizing for hastily a written post. I really wasn't thinking about what I was writing at the time.

    What is it that you are trying to accomplish exactly?
    Just feeding my curiosity. I had been thinking about how a 3d cadd program like sketchup would work, and I've been playing around with the concept in my head of storing a volume in a multidimensional array. The thought of how the amount of memory that it would use would increase exponentially as the volume increased had me pondering if there is some other method, but no, I am not currently working on any program of my own.

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Just feeding my curiosity. I had been thinking about how a 3d cadd program like sketchup would work, and I've been playing around with the concept in my head of storing a volume in a multidimensional array. The thought of how the amount of memory that it would use would increase exponentially as the volume increased had me pondering if there is some other method, but no, I am not currently working on any program of my own.
    Learn the Maths involved if you don't already have a good grasp.

    In 3D, everything(almost) is stored as groups convex polygons or triangles(if you want to move lower). Think in terms of that, it would not seem so 'exponential' .

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    3D and 2D positions can be represented as vectors. Create a vector2D, vector3D, and vector4D and provide the following operations:

    • Addition
    • Subtraction
    • Multiplication by a scalar
    • Division by a scalar
    • Cross product (I do not recommend overloading an operator for this)
    • Dot product (I do not recommend overloading an operator for this)
    • Equality
    • Length
    • Length squared - often you want to work in terms of the square of the length - this is essentially a dot product but a specific function for this is clearer than using the method for dot product
    • Normalization
    • Transformation by 4x4 matrix


    Transformations can be represented by matrix classes. I have only found the need for a 4x4 matrix transform class but you might want to create one for a 3x3. Likewise with these you would overload the various operators and alternatively provide functions for:
    • Scaling matrix
    • Rotation matrix - yaw, pitch, roll, x, y, z, axis-angle, euler from quaternion, etc.
    • Translation matrix
    • Shear matrix ?
    • Skew matrix (angles are not preserved)
    • Scale matrix along an arbitrary axis ?
    • Concatenation or multiplication of two matrices
    • Eigen vector calculation
    • Vector extraction functions (up, right, and look vectors can be extracted from a known orthogonal matrix)
    • Vector transform by a matrix
    • Quaternion transform by a matrix
    • Transpose (swap row/columns)
    • Inverse
    • Determinant
    • Identity


    Once you get the basic pieces coded and they work correctly the transformation process is simple.

    The transformation pipeline is: WVP = World * View * Projection, where W = (Identity or Root) * Scale * Rotation * Translation, View = Camera view matrix, and P = projection matrix

    For 2D P = orthogonal projection matrix and V = Identity matrix
    For 3D P = 3D projection matrix

    Even though you said you do not want to render I recommend you do or you will not know if your simulation is correct. Once the pipeline is complete you are ready to simulate motion through 2D and 3D space and/or render objects given you have an API that can access the hardware or a software API than can do simple 2D rasterization. I highly recommend you use OpenGL or Direct3D since they greatly simplify the rendering process and offload a great deal of work and math to the video card.

    Note that every vertex must be ran down the WVP pipeline in order to end up in the correct space. Regardless of 2D or 3D all vertices begin in local space and are transformed to world space, view space or camera space, and then screen space. There are other stages involved but this is simple enough to get started. Remember that 2D in a 3D system is simply 3D with the Z component discarded which is essentially what an orthogonal projection matrix does.

    If you insist on simulation only then transform your vertices to world space only and forget about the view and projection matrices. All your calculations will be done on vertices which represent the edges of the 3D volume. You can either break this up into a 3D grid to get more accuracy or you can represent the volume as a series of 2 vectors....or a simple axis-aligned bounding box. A min and max 3D vector are sufficient enough to represent a 3D cubic volume. For a sphere a center point and a radius are enough to represent a 3D spherical volume, and there are formulas for representing capsules and cylinders on the internet. I recommend looking at PhysX or Bullet or some other API that will do this for you.
    Last edited by VirtualAce; 08-18-2011 at 08:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NUMA Virtual Adress Space / Physical Adress Space
    By NUMA Orly in forum Windows Programming
    Replies: 0
    Last Post: 03-14-2011, 03:19 AM
  2. Replies: 9
    Last Post: 03-30-2009, 04:09 AM
  3. Space bar?
    By alexnb185 in forum C Programming
    Replies: 9
    Last Post: 08-09-2007, 12:04 AM
  4. Multi-dimensonal array (char **) bug....
    By disruptiv in forum C Programming
    Replies: 3
    Last Post: 09-29-2005, 01:26 PM
  5. space??
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2002, 04:19 AM

Tags for this Thread