Jump to content
  • Advertisement
Sign in to follow this  
jolyqr

Collision Detection

This topic is 4595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've got a collision detection system which I think work a bit, but not in the way that i want it to. basically, my game environment is composed of simple objects: cubes: all these cubes are surrounded by some squares delimiting the colliding zone. if the player gets in one of these zones, there is a collision. everything works, except that when there is a collision, my character sticks on the colliding zone so i can't move it anywhere. I would like a system which allow the character to move in the sides that are not collided....

Share this post


Link to post
Share on other sites
Advertisement
Sounds like you are having multiple internal collisions. When the first collision happens, you should have a collision repsonse to move both objects apart from each other so they are no longer colliding.

Share this post


Link to post
Share on other sites
i can't move a wall of the environment. i have tried to move the player when there is a collision, but some wierd behaviours happen, like instead of getting to the left side the player goes to the right side... Maybe it comes from my collision detection. I'm going to put some samples of my code lines...

Share this post


Link to post
Share on other sites
this is the method which tests the collision.


//*************************************************************************
//Desc: collision detection function
//xMin,xMax,zMin and zMax represent a collision zone
//In: xPos, yPos, zPos coordinates of the player
//Out: true if there is a collision, false otherwise.
//***************************************************************************
bool Collision::TestCollision(float xPos, float yPos, float zPos)
{
if((xPos>=xMin && xPos<= xMax)&&(yPos>=yMin && yPos<= yMax)&&(zPos>=zMin && zPos<= zMax))
{
return(true);
}
return(false);
}


Share this post


Link to post
Share on other sites
Quote:
Original post by jolyqr
i can't move a wall of the environment. i have tried to move the player when there is a collision, but some wierd behaviours happen, like instead of getting to the left side the player goes to the right side... Maybe it comes from my collision detection. I'm going to put some samples of my code lines...

That's correct, if you are getting odd behaviour then you should find the cause of the bug.

The simplest method is when the player collides, move the player in the opposite of the current direction till it stops colliding with the wall.

Share this post


Link to post
Share on other sites
I don't think that code is going to help much here. It all seems fine, but your problem is probably in what you do when TestCollision returns true rather than when you check (especially since you say that the collsion is detected). Try posting the code for the reaction.

Share this post


Link to post
Share on other sites
it's a little complicated, but i'm going to explain you.

when i press the keyboard the above functions are called

Share this post


Link to post
Share on other sites

if(GetKeyState(VK_UP) & 0x80)
{
Collision_test(&player1,&myPlayer::Player::MoveForward,'f');
}

if(GetKeyState(VK_DOWN) & 0x80 )
{
Collision_test(&player1,&myPlayer::Player::MoveBackward,'b');
}

if(GetKeyState(VK_LEFT) & 0x80)
{
Collision_test(&player1,&myPlayer::Player::MoveLeft,'l');
}

if(GetKeyState(VK_RIGHT) & 0x80)
{
Collision_test(&player1,&myPlayer::Player::MoveRight,'r');
}


Share this post


Link to post
Share on other sites
//******************************************************************************************************************
//Desc: collision testing
//In:pObj a player object, pSetFunc a function that moves the player on x or z axis and buff contains the movement direction
//Out: nothing
//****************************************************************************************************************
void Collision_test(myPlayer::Player* pObj,void (myPlayer::Player::*pSetFunc)(float),char buff)
{
if(north.col.TestCollision(player1.GetX(),player1.GetY(),player1.GetZ())==true)
{
Switch(buff);
}
else
{
if(north_out.col.TestCollision(player1.GetX(),player1.GetY(),player1.GetZ())==true)
{
Switch(buff);
}
else
{
(*pObj.*pSetFunc)(timer1.frametime);
}
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!