Jump to content
  • Advertisement
Sign in to follow this  
Subliminalman

Collision Detection Problem

This topic is 2616 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

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







Share this post


Link to post
Share on other sites
Advertisement
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)
{
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.currPos)
{
if(colliding(obj1, objects)
{
// Set new position for collided object
objects.lastPos = objects.currPos;

// Get objects new position based off angle objects are colliding
objects.currPos += obj1.currPos - objects.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, wall)
{
obj1.currPos = obj1.lastPos;
objects.currPos = objects.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, objects))
{
obj1.currPos = obj1.lastPos;
//objects.currPos = objects.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!

Share this post


Link to post
Share on other sites
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

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!