Jump to content
  • Advertisement
Sign in to follow this  
mitko29

Bounding Sphere collision is not working

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

Hi guys I am trying to apply the bounding sphere on my models , but when I try to detect collision nothing happened.....

Here how am I drawing and applying the bounding sphere:
In enemy class :

if ((texture != null) && (texture.Meshes.Count > 0))
{
Matrix[] transforms = new Matrix[texture.Bones.Count];
texture.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh meshh in texture.Meshes)
{
int a = texture.Meshes.Count;
if (sphere.Radius == 0)
sphere = meshh.BoundingSphere;
else
sphere = BoundingSphere.
CreateMerged(sphere, meshh.BoundingSphere);
DebugShapeRenderer.AddBoundingSphere(meshh.BoundingSphere, Microsoft.Xna.Framework.Color.Red);
foreach (BasicEffect effect in meshh.Effects)
{
effect.EnableDefaultLighting();
enemymat = effect.World = transforms[meshh.ParentBone.Index] *
Matrix.CreateRotationY(modelRotation)
* Matrix.CreateTranslation(position);
effect.View = Matrix.CreateLookAt(new Vector3(0.0f, 50.0f, 5000.0f),
Vector3.Zero, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45.0f), aspectRatio,
1.0f, 10000.0f);
view1 = Matrix.CreateLookAt(new Vector3(0.0f, 50.0f, 5000.0f),
Vector3.Zero, Vector3.Up);
projection1 = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45.0f), aspectRatio,
1.0f, 10000.0f);
}
meshh.Draw();
DebugShapeRenderer.AddBoundingSphere(sphere, Microsoft.Xna.Framework.Color.Pink);
DebugShapeRenderer.Draw(gameTime, view1, projection1);
}
sphere.Transform(ref enemymat,out sphere);

}
}

and in Update method:

sphere.Transform(ref enemymat, out sphere);

and here how I load the enemy texture in load method:

eninit = new Enemy(secondModel, position, 100, 100, 0.5f,secondModel.Meshes[0].BoundingSphere);

and here how I draw the player:

if ((myModel != null) && (myModel.Meshes.Count > 0) && transformation == true)
{
Matrix[] transforms = new Matrix[myModel.Bones.Count];
myModel.CopyAbsoluteBoneTransformsTo(transforms);
Matrix rotation = Matrix.CreateFromYawPitchRoll(-yaw, -pitch, roll);
Matrix translation = Matrix.CreateTranslation(
modelTransformaton.V03, modelTransformaton.V13, -modelTransformaton.V23);
Matrix scaling = Matrix.CreateScale(100);
foreach (ModelMesh mesh in myModel.Meshes)
{

if (modelsp.Radius == 0)
modelsp = mesh.BoundingSphere;
else
modelsp = BoundingSphere.
CreateMerged(modelsp, mesh.BoundingSphere);
DebugShapeRenderer.AddBoundingSphere(mesh.BoundingSphere, Microsoft.Xna.Framework.Color.Green);
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
world = effect.World = transforms[mesh.ParentBone.Index] *
Matrix.CreateScale(1 / mesh.BoundingSphere.Radius)
* scaling * rotation * translation;
effect.View = Matrix.CreateLookAt(
new Microsoft.Xna.Framework.Vector3(0, 0, 3),
Microsoft.Xna.Framework.Vector3.Zero,
Microsoft.Xna.Framework.Vector3.Up);
effect.Projection = Matrix.CreatePerspective(
1, 1 / GraphicsDevice.Viewport.AspectRatio, 1f, 10000);
view1 = transforms[mesh.ParentBone.Index] *
Matrix.CreateScale(1 / mesh.BoundingSphere.Radius)
* scaling * rotation * translation;
projection1 = Matrix.CreatePerspective(
1, 1 / GraphicsDevice.Viewport.AspectRatio, 1f, 10000);
//pos = Matrix.Invert(translation).Translation;
}
mesh.Draw();
DebugShapeRenderer.AddBoundingSphere(modelsp, Microsoft.Xna.Framework.Color.Beige);
DebugShapeRenderer.Draw(gameTime, view1, projection1);
}
modelsp.Transform(ref world,out modelsp);
}

and in the update method:

modelsp.Transform(ref world,out modelsp);

and in LoadContent():

modelsp = myModel.Meshes[0].BoundingSphere;

The result:
c1c37aa3e1c1a5b6.png

And the meshes on both model are equal to 1;

And this way I am trying to detect collision:

//enemies is a List<Enemy>
if (enemies.Sphere.Intersects(modelsp))
{
// only for test
enemies.Speed = 0.0f;
}

Share this post


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