Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Any reason why my collision doesn't work?

Started by BluePhase Oct 22, 2012 at 1:55 AM 7 replies 1.5k views
Original Post
BluePhase
BluePhase
I can't seem to get my collision detection to work at all. Hopefully someone can. Is there a better way to test if it's colliding? The object should free fall until it collides with the object then stops but it doesn't. It just free falls continuously. Thanks in advanced!


This is my Collision Detector:
[source lang="csharp"] public bool CheckForCollision(CustomModel c1, CustomModel c2)
{
for (int i = 0; i < c1.Model.Meshes.Count; i++)
{
// Check whether the bounding boxes of the two cubes intersect.
BoundingSphere c1BoundingSphere = c1.Model.Meshes.BoundingSphere;
c1BoundingSphere.Center += c1.Position;

for (int j = 0; j < c2.Model.Meshes.Count; j++)
{
BoundingSphere c2BoundingSphere = c2.Model.Meshes[j].BoundingSphere;
c2BoundingSphere.Center += c2.Position;

if (c1BoundingSphere.Intersects(c2BoundingSphere))
{
return true;
}
}
}
return false;
}[/source]

This is my physics that I use to test it:

[source lang="csharp"] public void Update(GameTime gameTime, CustomModel model, float Mass, float Velocity)
{
if (collision.HasCollided == true)
CollidedWithObject = true;
ModelPositionHeight = model.Position.Y;
if (CollidedWithObject == false)
{
ModelPositionHeight = (Velocity) - (0.5f * Mass * Time * (Time * Time));
model.Position = new Vector3(model.Position.X, ModelPositionHeight, model.Position.Z);
Time += (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (CollidedWithObject == true)
{
model.Position = new Vector3(model.Position.X, ModelPositionHeight, model.Position.Z);
}
}[/source]

In my Main (game1) class:


[source lang="csharp"] customModel.Update(gameTime, models[0], 120, 250);
collisionController.CheckForCollision(models[1], models[0]);[/source]
Krohm
Krohm

Currently, the object free falls until it collides with the object then stops but it's not working
So it works... or not?
I guess you mean to say the two objects get stuck to each other.
This is because

  1. Your CD checker... which is n^2... ouch!
  2. Anyway, I have no idea who sets up [font=courier new,courier,monospace]collision.HasCollided[/font] but I speculate it's the return value from [font=courier new,courier,monospace]CheckFromCollision[/font].
  3. Suppose object A collided with object B. Its internal flag gets set.
  4. From now on, [font=courier new,courier,monospace]Update [/font]LN 14 will always be executed. The object, according to the code you posted (and the code I have speculated) cannot exit from the collided state, and gets no updates ever. Therefore, it sticks to its collider.

That said. I strongly suggest to look for a C# binding of a physics library. It's never too early to use one.

BTW, what's the real point of LN14? [font=courier new,courier,monospace]ModelPositionHeight [/font]is numerically equivalent to [font=courier new,courier,monospace]model.Position.Y[/font] so this is actually a NOP.
Previously "Krohm"
BluePhase
BluePhase

[quote name='BluePhase' timestamp='1350870942' post='4992653']
Currently, the object free falls until it collides with the object then stops but it's not working
So it works... or not?
I guess you mean to say the two objects get stuck to each other.
This is because

  1. Your CD checker... which is n^2... ouch!
  2. Anyway, I have no idea who sets up [font=courier new,courier,monospace]collision.HasCollided[/font] but I speculate it's the return value from [font=courier new,courier,monospace]CheckFromCollision[/font].
  3. Suppose object A collided with object B. Its internal flag gets set.
  4. From now on, [font=courier new,courier,monospace]Update [/font]LN 14 will always be executed. The object, according to the code you posted (and the code I have speculated) cannot exit from the collided state, and gets no updates ever. Therefore, it sticks to its collider.

That said. I strongly suggest to look for a C# binding of a physics library. It's never too early to use one.

BTW, what's the real point of LN14? [font=courier new,courier,monospace]ModelPositionHeight [/font]is numerically equivalent to [font=courier new,courier,monospace]model.Position.Y[/font] so this is actually a NOP.
[/quote]

It won't let me modify the model.Positiion.Y file because it basically doesn't have a value until initialization. And I used a bad use of grammar. The object just FREE FALLS. It never stops even when the models are right on top of each other.
phil_t
phil_t
So your bounding sphere sizes might be incorrect. Do they look correct to you if you step through the code?

Are you scaling your models at all when drawing them? In your collision code you're taking into account their position but not their scale.
BluePhase
BluePhase

So your bounding sphere sizes might be incorrect. Do they look correct to you if you step through the code?

Are you scaling your models at all when drawing them? In your collision code you're taking into account their position but not their scale.


Does it really matter if I include the scale or not? The bounding spheres are congruent to the mesh so I assume it is taking the size in as well. Stepping through the code takes me a very long time. I have a lot of code written.
phil_t
phil_t
When you draw the mesh, you generally apply a world matrix. If that world matrix includes a scaling component, then you also need to include that when generating your bounding sphere.

It should only take a few seconds to check this in the debugger. Just set a breakpoint on that section of the code and take a look at the values. The bounding spheres are obviously not intersecting, so what do the values look like?

You can take a few seconds to figure it out yourself, or you can keep wasting time guessing and hoping someone will figure it out for you.... your choice :-)
BluePhase
BluePhase

When you draw the mesh, you generally apply a world matrix. If that world matrix includes a scaling component, then you also need to include that when generating your bounding sphere.

It should only take a few seconds to check this in the debugger. Just set a breakpoint on that section of the code and take a look at the values. The bounding spheres are obviously not intersecting, so what do the values look like?

You can take a few seconds to figure it out yourself, or you can keep wasting time guessing and hoping someone will figure it out for you.... your choice :-)

Still having a difficult time understanding how to implement that :3

Topic Locked

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

Sign in to reply to this topic.