Collision Detection

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

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 on other sites
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 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 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 on other sites
Quote:
 Original post by jolyqri 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 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 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 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 on other sites
i'm calling the bellow functions...

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 on other sites

//******************************************************************************************************************
//Desc: choose the collision response direction. If there is a collision forward, move of 0.5 units backward, etc...
//In: buff contains the movement direction: 'f':forward, 'b':back, 'l':left and 'r':right
//Out: nothing
//****************************************************************************************************************
void Switch(char buff)
{
oldPlayerXPosition=player1.GetX();
oldPlayerZPosition=player1.GetZ();
switch(buff)
{
case 'f':
{
player1.SetPosition(oldPlayerXPosition-0.5f,player1.GetY(),player1.GetZ());
}
break;
case 'b':
{
player1.SetPosition(oldPlayerXPosition+0.5f,player1.GetY(),player1.GetZ());
}
break;
case 'l':
{
player1.SetPosition(player1.GetX(),player1.GetY(),oldPlayerZPosition+0.5f);
}
break;
case 'r':
{
player1.SetPosition(player1.GetX(),player1.GetY(),oldPlayerZPosition-0.5f);
}
break;
}
}

Share on other sites
i'm sorry for the above line codes, but i've tried to put them inside the "source" mark up, but it does not work...

Share on other sites
I don't even know how to put stuff into a source thingy, I just use the

[ Code ] [ / code ]

Share on other sites
i have noticed that the wierd behaviours happen when i press two button of the keyboard at the same time.

for example, if i press the forward button of the keyboard when i'm colliding a and if at the same time i press the right button, the colliding responses becomes crazy!!! The player gets in the colliding zone and stays inner. therefore, instead of moving backward or sliding on the right, it moves on the left, because the right button has caused another collision ...

Share on other sites
A simple solution to this problem is to remember the location of movable objects (which includes the player) from the previous frame. If a collision is detected after attempting to move, just revert to the positions from that previous frame. If objects are moving at high speed (viz, a large proportion of their own bounding-box per frame) this can be unsatisfactory, leaving large gaps between 'in contact' objects, but it is OK for simple purposes.

If you want to know all about the 'proper' way to do collision detection – more than you could ever want to know, I suspect – do a search on the Game Programming or Maths and Physics forums.

Share on other sites
could someone explain me how do insert a picture on my post ?

i'm going to draw you a picture. it'll be easier to understand.

Share on other sites
<img src="http://www.your.image.com/image.jpg" alt="text for those who can't view images" />

Share on other sites
excuse me, i don't understand. where should i put the name of my picture ?
do i have to replace the "image.jpg" or "your.image.com"... ?

how to indicate that my picture is on my desk top ?

Share on other sites
You can get free image hosting here. Then just use the url they give you.

For example, upload an image called hover.bmp, they will give you a link that you can paste here.

< a href="http://imageshack.us">< img src="http://img530.imageshack.us/img530/2001/hover1uq.png" border="0" width="128" alt="Image Hosted by ImageShack.us" /></a >

Share on other sites
that's very small. hope you can see something.

If i go forward (on the direction of the red arrow). The collision is detected, the player (blue cube) responds by going backward (by following the green arrow).

Now if I go forward and if at the same time I press the right direction (by following the black arrow), the collision is detected as well, but it’s detected on the right side (not forward). Therefore the player goes on the left side (by following the yellow arrow) till the brown cross, even I stop pressing the right key. Basically, the player stays on collision with the wall, but instead of going backward, he goes on the left side (because I had pressed the right key, but the collision was caused by the forward key. I know it’s a little bit confusing, even for me).

To summarize my colliding responses work very well only if I press one key. If I try to press two keys at the same time, it does not know how to respond, and responds by the colliding caused by the second key even the colliding has been caused first by the first key.