Skip to main content
gamedev.net
🔒 Locked

Collision question.

Started by X5-Programmer 2 replies 650+ views
Original Post
X5-Programmer
X5-Programmer
Hi, I have some problem with the collision between players -> enemys and player -> player.. I have try to use an simple sphere collision like this to get the collision..

bool Collision(float EnemyX, float EnemyZ, float PlayerX, float PlayerZ, float range)
{
	float dx, dz;

	dx=PlayerX-EnemyX;
	dz=PlayerZ-EnemyZ;

	if (dx*dx + dz*dz < (range*range))
		return true;

	return false;
}
then how can I push the players away from the enemys? and playres from each other? point me somewere :S
-[ thx ]-
SoulSpectre
SoulSpectre
Well dx and dz will give you the delta to the center of each object which can also be used as a direction vector. A simplistic position correction would be:

len = sqrt(dx*dx + dz*dz);
//assert that len not zero.
dirx = dx/len;
dirz = dz/len;

movedist = distancewanted - len;

object.x += dirx * movedist;
object.z += dirz * movedist;
SoulSpectre
SoulSpectre
an even better approach:
change:
object.x += dirx * movedist;
object.z += dirz * movedist;

to:

obj1.x -= dirx * movedist/2;
obj1.z -= dirz * movedist/2;

obj2.x += dirx * movedist/2;
obj2.z += dirz * movedist/2;

note this is in reference of delta going from obj1 to obj2
X5-Programmer
X5-Programmer
Hi SoulSpectre, thx for your replays I understand now.. but I have one more question..

if I use an loop to load the players how can I make collision between them?

can I do something like this?

for(int t = 0; t < num_players; t++)
{
if(Collision(player[t].xPos, player[t].xPos, player[t+1].xPos, player[t+1].xPos )
{
//collision
}
}

this dosent work but you understand what I meen?

im tired :S
-[ thx ]-

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.