Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Any reason why my collision doesn't work?


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.

  • You cannot reply to this topic
8 replies to this topic

#1 BluePhase   Members   -  Reputation: 131

Like
0Likes
Like

Posted 21 October 2012 - 07:55 PM

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[i].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]

Edited by BluePhase, 22 October 2012 - 01:03 PM.


Ad:

#2 phil_t   Members   -  Reputation: 1019

Like
0Likes
Like

Posted 21 October 2012 - 10:08 PM

What happens when you step through the code with the debugger? Do the values look right?

#3 Krohm   GDNet+   -  Reputation: 1710

Like
0Likes
Like

Posted 22 October 2012 - 01:03 AM

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
  • Your CD checker... which is n^2... ouch!
  • Anyway, I have no idea who sets up collision.HasCollided but I speculate it's the return value from CheckFromCollision.
  • Suppose object A collided with object B. Its internal flag gets set.
  • From now on, Update 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? ModelPositionHeight is numerically equivalent to model.Position.Y so this is actually a NOP.

Edited by Krohm, 22 October 2012 - 01:05 AM.


#4 BluePhase   Members   -  Reputation: 131

Like
0Likes
Like

Posted 22 October 2012 - 01:02 PM


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
  • Your CD checker... which is n^2... ouch!
  • Anyway, I have no idea who sets up collision.HasCollided but I speculate it's the return value from CheckFromCollision.
  • Suppose object A collided with object B. Its internal flag gets set.
  • From now on, Update 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? ModelPositionHeight is numerically equivalent to model.Position.Y so this is actually a NOP.


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.

#5 phil_t   Members   -  Reputation: 1019

Like
0Likes
Like

Posted 22 October 2012 - 02:43 PM

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.

#6 BluePhase   Members   -  Reputation: 131

Like
0Likes
Like

Posted 22 October 2012 - 04:53 PM

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.

#7 phil_t   Members   -  Reputation: 1019

Like
0Likes
Like

Posted 22 October 2012 - 05:51 PM

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 :-)

#8 BluePhase   Members   -  Reputation: 131

Like
0Likes
Like

Posted 23 October 2012 - 03:55 PM

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

#9 phil_t   Members   -  Reputation: 1019

Like
0Likes
Like

Posted 24 October 2012 - 01:55 PM

Which thing specifically are you having trouble with? The debugging? Look for a tutorial on this, such as this one.




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