Original Post
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]
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]