I hate to keep beating this thread to death but the matrix math is driving me insane!
I have this event handler for when an arrow first hits a target:
Code: Select all
void Events_InitialCollisionDetected(BEPUphysics.Collidables.MobileCollidables.EntityCollidable sender, BEPUphysics.Collidables.Collidable other, BEPUphysics.NarrowPhaseSystems.Pairs.CollidablePairHandler pair)
{
sender.Entity.LinearVelocity = Vector3.Zero;
sender.Entity.AngularVelocity = Vector3.Zero;
Arrow arrow = pair.EntityA.Tag as Arrow;
Cylinder cylinder = pair.EntityB as Cylinder;
if (arrow == null)
{
arrow = pair.EntityB.Tag as Arrow;
cylinder = pair.EntityA as Cylinder;
}
if (arrow != null)
{
if (cylinder != null)
{
Target target = cylinder.Tag as Target;
if (target != null)
{
arrow.StuckTo = target;
arrow.Offset = arrow.PhysObj.WorldTransform.Translation - target.PhysObj.WorldTransform.Translation;
arrow.Orientation = Matrix.Identity;
arrow.Orientation = Matrix.Transform(arrow.Orientation, arrow.PhysObj.Orientation);
}
}
}
if (arrow.PhysObj.Space != null)
{
arrow.PhysObj.Space.Remove(arrow.PhysObj);
}
}
Then I have this for rendering my arrow after its "stuck" to a target:
Code: Select all
foreach (ModelMesh mesh in ArcheryGame.arrowModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.View = viewMatrix;
effect.Projection = projectionMatrix;
if (StuckTo != null)
{
effect.World = Matrix.CreateScale(8.0f) * Orientation * Matrix.CreateTranslation(Offset) * StuckTo.PhysObj.WorldTransform;
}
else
{
effect.World = Matrix.CreateScale(8.0f) * PhysObj.WorldTransform;
}
}
mesh.Draw();
}
This works great as long as I don't apply any manual change to the orientation of the target (using a cylinder prefab). If when I create the target I do this:
Code: Select all
PhysObj = new Cylinder(pos, 1.0f, radius, 100.0f);
PhysObj.Orientation = PhysObj.Orientation * Quaternion.CreateFromYawPitchRoll(0.0f, -MathHelper.PiOver2, 0.0f);
Then my arrows stick but when they hit they get all wonky in their location and rotation. I didn't notice this before because I was always shooting stationary targets. What am I doing wrong?