# XNA Tower Defense game, shots rotation problem

This topic is 2774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey Guys!

Im making a tower-defense like game where I ofc. have towers that shoots at the enemies.

I have no problem hitting my enemies, but when it comes to rotation the arrows so the tip is whats hits the enemies I have nothing but trouble.

For hitting the enemies (this works) I use:

 boundingSphere = new BoundingSphere(position.Translation, 5); position *= velocity; acceleration = position.Translation; acceleration -= enemy.position.Translation; acceleration *= -0.5f; velocity *= Matrix.CreateTranslation(acceleration); Vector3 newVelocity = velocity.Translation; newVelocity.Normalize(); newVelocity *= 2; velocity = Matrix.CreateTranslation(newVelocity);

For rotating the arrow so it should hit correctly I use the formula: v1 DOT v2 = |v1| |v2| cosØ and as I understand it Acos in XNA returns in radians. Whenever a tower fires an arrow I makes sure that the velocity is set to shoot up in to the air to begin with, so the arrow is shot straight up and should move and rotate toward the enemy. Here is my rotation code:

 v1 = velocity.Translation; v2 = enemy.position.Translation; Vector2 xV1 = new Vector2(v1.Y, v1.Z); Vector2 xV2 = new Vector2(v2.Y, v2.Z); Vector2 yV1 = new Vector2(v1.X, v1.Z); Vector2 yV2 = new Vector2(v2.X, v2.Z); Vector2 zV1 = new Vector2(v1.X, v1.Y); Vector2 zV2 = new Vector2(v2.X, v2.Y); xV1.Normalize(); xV2.Normalize(); yV1.Normalize(); yV2.Normalize(); zV1.Normalize(); zV2.Normalize(); xRotation = (float)Math.Acos(Vector2.Dot(xV1, xV2)); yRotation = (float)Math.Acos(Vector2.Dot(yV1, yV2)); zRotation = (float)Math.Acos(Vector2.Dot(zV1, zV2)); rotationMatrix = Matrix.CreateRotationX(xRotation) * Matrix.CreateRotationY(yRotation) * Matrix.CreateRotationZ(zRotation); 

Yes I choose to move all the calculations from 3D space to 2D space, I tried to rotate using an arbitary axis doing :

 arbitaryAxis = Vector3.Normalize(Vector3.Cross(v1, v2)); rotationMatrix = Matrix.CreateFromAxisAngle(arbitaryAxis, (float)Math.Acos(Vector3.Dot(v1, v2))); 

But that works like poop as well
Anyways, the rotations suck! It looks like poop, and it doesnt rotate correctly, any advise ?

Thx guys!

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 9
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633679
• Total Posts
3013300
×