XNA Creators Club Online
Page 1 of 1 (9 items)
Sort Posts: Previous Next

colliding with ground in 3d world

Last post 09-05-2008 9:01 PM by Lord Ikon. 8 replies.
  • 08-12-2008 9:27 PM

    colliding with ground in 3d world

    i have a triangle list as my floor. i have my bounding sphere running around on the floor.  I have one ray coming out of my sphere and colliding with the triangles on the floor, but cant get my sphere to act properly.  here is my code below, if someone has a more elegant way or sample please post, cause my way stinks.  I need a solution to move around an uneven floor and change my spheres Y position as i either go down one slope or up another.  this has been done in an infinite amount of games, so i know someone can help.

    if (collidingWithWorld == true)  
                {  
                    // Just Entered the world  
                    if (oldDistanceToTriangle == 0f)  
                    {  
                        oldDistanceToTriangle = distanceToTriangle;  
                    }  
                    // Going up slope  
                    if (oldDistanceToTriangle > distanceToTriangle)  
                    {  
                        newPlayerPosition.Y += distanceToTriangle + (oldDistanceToTriangle - distanceToTriangle);  
                    }  
                    // Going down slope  
                    if (oldDistanceToTriangle < distanceToTriangle)  
                    {  
                        newPlayerPosition.Y -= distanceToTriangle - (distanceToTriangle - oldDistanceToTriangle);  
                    }  
                    // Not Moving  
                    if (oldDistanceToTriangle == distanceToTriangle)  
                    {  
                        // Idol, do nothing  
                    }  
                }  
                else 
                {  
                    newPlayerPosition.Y -= GRAVITY;   
                } 

     

     

  • 08-12-2008 10:13 PM In reply to

    Re: colliding with ground in 3d world

  • 08-13-2008 11:13 AM In reply to

    Re: colliding with ground in 3d world

    height maps are not an option, never will be.  I have caves and rooms and multiple levels of height.  I have worked with height maps and they work great for one plane of triangles but for this project i need triangle/ray collision functionality with the ground.  it cant be that hard, infact i know i am close, i can move around my world and bounce off the ground, but i want to move smoothly.
  • 08-13-2008 5:50 PM In reply to

    Re: colliding with ground in 3d world

    well i am now moving around my world in rooms and caves and colliding with the floor as smooth as can be... for anyone that cares, here is the code that allows you to do so.  I had my math wrong in the above post but the logic was correct.  here is the code
                if (distanceToTriangle > 0)  
                {  
                    if (oldDistanceToTriangle == 0f)  
                    {  
                        oldDistanceToTriangle = distanceToTriangle;  
                    }  
                    // Going up slope  
                    if (oldDistanceToTriangle > distanceToTriangle)  
                    {  
                        newPlayerPosition.Y += (oldDistanceToTriangle - distanceToTriangle);  
                    }  
                    // Going down slope  
                    else if (oldDistanceToTriangle < distanceToTriangle)  
                    {  
                        newPlayerPosition.Y -= (distanceToTriangle - oldDistanceToTriangle);  
                    }  
                    else if (oldDistanceToTriangle == distanceToTriangle)  
                    {  
                          
                    }  
                }  
                else 
                {  
                    newPlayerPosition.Y -= GRAVITY;   
                } 
  • 08-13-2008 6:21 PM In reply to

    Re: colliding with ground in 3d world

    cpyburn:
    height maps are not an option, never will be.  I have caves and rooms and multiple levels of height.  I have worked with height maps and they work great for one plane of triangles but for this project i need triangle/ray collision functionality with the ground.  it cant be that hard, infact i know i am close, i can move around my world and bounce off the ground, but i want to move smoothly.

    Oh, it can be that hard, and that is why there are physics engines.

    Let us say you have a 3D terrain (not a heightmap), that you're standing on, and let us also say this terrain is roughly 2 million polygons. Do you check all 2 million triangles against your character each frame? No way. You'll need algorithms to cull out sections of models/terrain that you don't want to collide with. This means the terrain will most likely need to be processed when it is loaded into an oct-tree or quad-tree. You'll need to do broadphase/narrowphase checks on it.

    I'm literally just listing the few things that come to mind at first.

    You might look into using something like JigLibX rather than set all of this up on your own, unless your goal is to specifically learn game physics in detail.

    XNA QuickStart Engine | My site
    "I'll be whatever I want to do!", Philip J. Fry
  • 08-13-2008 10:53 PM In reply to

    Re: colliding with ground in 3d world

    I already created the octree, and it was mind boggling... opened my third eye lets say... took my brain to a different level of coding...  I am creating a game engine from scratch, cutting no corners, and using nobody elses code (althought i dont mind learning or knowing how other people did it).  I want the experience and i want the knowledge.  My next goal now that I have ray collision working is to test the angles of mountains to know whether I have to climb them or not.  I start on that next week.
  • 08-14-2008 12:50 AM In reply to

    Re: colliding with ground in 3d world

    If walking on slopes is what you're going for, you could probably use the normal vector of the slope and dot it with the world up vector. This would give you a steepness. 1.0f would be sheer cliff or vertical, 0.0f would be perfectly horizontal. Any negative value would be a surface that was facing down, like a ceiling.

    Based on this value, higher being steeper, you could not let the player walk up past a certain level, like 0.5f (or whatever you wanted).

    XNA QuickStart Engine | My site
    "I'll be whatever I want to do!", Philip J. Fry
  • 09-05-2008 5:30 PM In reply to

    Re: colliding with ground in 3d world

    thanks ikon, it worked like a charm.

     

                Vector3 newPlayerPosition = mCamera.CameraPosition;  
                if (octreeTool.DistanceToTriangleListPlusNormal.Count > 0)  
                {  
                    if (Vector3.Dot(octreeTool.DistanceToTriangleListPlusNormal[0].normal, Vector3.Up) > .5)  
                    {  
                        newPlayerPosition = mCamera.CameraPosition + velocity;  
                    }  
                }  
                else 
                {  
                    newPlayerPosition = mCamera.CameraPosition + velocity;  
                } 

    One comment for me to make is you had it backwards. 0.0f is a wall, and 1.0f is a floor or ceiling depending on sign.

  • 09-05-2008 9:01 PM In reply to

    Re: colliding with ground in 3d world

    Whoops, yes, the 1.0 for wall and 0.0 for floor would be if you dotted against a horizontal vector (Y value of 0.0f). Glad it worked though.
    XNA QuickStart Engine | My site
    "I'll be whatever I want to do!", Philip J. Fry
Page 1 of 1 (9 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG