Collision Detection

Started by
19 comments, last by jolyqr 18 years ago
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....
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.

Steven Yau
[Blog] [Portfolio]

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...
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);	}
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.

Steven Yau
[Blog] [Portfolio]

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.
it's a little complicated, but i'm going to explain you.

when i press the keyboard the above functions are called
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');	}
i'm calling the bellow functions...
//******************************************************************************************************************
//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);
}
}
}

This topic is closed to new replies.

Advertisement