• Advertisement
Sign in to follow this  

XNA Tower Defense game, shots rotation problem

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

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 *= 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);


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! :o It looks like poop, and it doesnt rotate correctly, any advise ? :)

Thx guys!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement