• Create Account

Banner advertising on our site currently available from just \$5!

# Collision Detection Problem

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1Subliminalman  Members   -  Reputation: 109

Like
0Likes
Like

Posted 24 September 2011 - 06:23 PM

Hey all,

I'm currently working on a game with XNA.

I'm having trouble modeling how two (or more) objects are supposed to react to each other when they collide against a wall. The best example of what I'm trying to do is if you have two boxes and you're trying to push them against a wall.

-> [][]|

What happens with mine is that one goes right through the other and they take up the same space. How would I go about modeling this in my code without having to use an outside physics library.

Heres a picture to better illustrate of what I want.

Thank you for any help

### #2iedoc  Members   -  Reputation: 1233

Like
0Likes
Like

Posted 24 September 2011 - 10:42 PM

So what you have right now is that when you push one box against the other, then they both move, you have it that far right? is the problem that when you push the boxes against the wall, the box you are pushing just goes through the other boxes instead of just stopping?

ok, so when your doing collision detection with all the objects, you will need to check every object against every other object. If an object is colliding with another object, check for a collision with the second object and the walls. If there was a collision with the walls, don't move either object, if there was no collision with the wall, make the second object move in the direction they are colliding at (this works really well for spheres). so you create a vector from the center of the first object to the center of the second object that its colliding with, and add that vector to the second objects position. When it's the second objects turn to detect collisions, do the same thing, but if its against the wall, don't move it

so something like this maybe

// have a current position and a last position for each object

box.lastPos = box.currPos

// move object by setting obj1.currPos then call the function

collisionDetection(box, boxes);

// We'll have the function return true if there was a collision with the wall
bool collisionDetection(object obj1, vector objects)
{
// first check for a collision with the wall
for(int i = 0; i < numWalls; i++)
{
if(colliding(obj1, wall[i])
{
obj1.currPos = obj1.lastPos;
return true;
}
}

// Now check objects for collision with obj1 if obj1 wasn't against a wall
if(obj1.moved)
{
for(int i = 0; i < numObjects; i++)
{
// Make sure we don't test object with itself
if(obj1.currPos != objects[i].currPos)
{
if(colliding(obj1, objects[i])
{
// Set new position for collided object
objects[i].lastPos = objects[i].currPos;

// Get objects new position based off angle objects are colliding
objects[i].currPos += obj1.currPos - objects[i].lastPos;

// Make sure the box thats being collided with isn't against the wall
for(int i = 0; i < numWalls; i++)
{
// check for collision with wall
if(colliding(objects[i], wall[i])
{
obj1.currPos = obj1.lastPos;
objects[i].currPos = objects[i].lastPos;
return true;
}
else
{
// Now we can call this function again but for the collided object instead
// If it returns true, there was a collision with the wall, so we know
// Not to move the objects
if(collisionDetection(objects[i], objects))
{
obj1.currPos = obj1.lastPos;
//objects[i].currPos = objects[i].lastPos;	// We don't need this line since the collisionDetection() function will do it for us
return true;
}
}
}
}
}
}
}
return false;
}

I know, thats really sloppy code, but i hope it still gives you an idea of how you could solve your problem

FINALLY! IT WORKED! I have never got that darn code or source tag to work, and now its working!
Braynzar Soft - DirectX Lessons & Game Programming Resources!

### #3Subliminalman  Members   -  Reputation: 109

Like
0Likes
Like

Posted 11 October 2011 - 10:47 PM

Sorry for the late reply another project took over me for a little while!

I'm finding that I have a problem with the two objects pushing each other around horizontally. I know what code is doing it and its causing them to overlap and

not perform behavior I intend. The problem is I dont have the slightest clue on how to perform the pushing of objects around properly.

I want the two objects to be able to push each other around screen when bumping into each other. It somewhat does this with the code below but not

as intended behavior. They overlap and they cannot fight over domination of who pushes based on their velocity because the one with the lesser velocity

gets set by the greater so it can never actually win to push the opposite direction.

Here's the code that I currently have for it.

foreach (FallingMan otherCat in cats)
{
#region test
if (!otherCat.Equals(cat))
{

if (Math.Abs((otherCat.position.X + (cat.texture.Width /2)) - (cat.position.X + (cat.texture.Width /2))) < 60
&& Math.Abs((otherCat.position.Y + (cat.texture.Height / 2)) - (cat.position.Y + (cat.texture.Height / 2))) < 60)//Checking if bouncing into each other
{
//This is where it needs to be changed
/*
if (Math.Abs(otherCat.velocity.X) > Math.Abs(cat.velocity.X))
{
cat.velocity.X = otherCat.velocity.X;
}
*/

if (Math.Abs(otherCat.velocity.Y) > Math.Abs(cat.velocity.Y))
{

cat.velocity.Y = otherCat.velocity.Y + 1;

}

}

//FIGURE OUT HOW TO KEEP THEM FROM OVERLAPPING!

if (otherCat.attackCollision.Intersects(cat.collision))//Checking for attacking
{
if (otherCat.position.X < cat.position.X)
{
cat.velocity.X += 10;
}
else
{
cat.velocity.X -= 10;
}
}
}
#endregion



Again thank you so very much for any help

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS