Sprite Rotation XNA - c#

I'm trying to rotate a sprite to face the mouse by incrementing and decrementing the Rotation using a turning radius. It works fine up until the point where the mouse is top-left and moves to the top-right. I have it so that if the angle difference is greater than the current Rotation value, the sprite's Rotation gets incremented, otherwise it gets decremented. So when it goes from 6.5 radians to 0, it rotates counterclockwise by 350-some degrees, instead of clockwise by 15-some degrees. Me and others have been working on this all day and have no idea how to solve this. My code is below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.IO;
namespace Physics
{
public class Ship
{
public Texture2D Texture { get; set; }
public Vector2 Position { get; set; }
public double Rotation { get; set; }
MouseState prevState, currState;
Vector2 A, B;
public const double NINETY_DEGREES = 1.57079633;
double turningRadius = 2 * (Math.PI / 180);
double targetRotation, rotationDifferential;
public Ship(Texture2D texture, Vector2 position)
{
Texture = texture;
Position = position;
A = new Vector2(Position.X, Position.Y);
B = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
Rotation = 0;
}
public void Update()
{
currState = Mouse.GetState();
A.X = Position.X;
A.Y = Position.Y;
B.X = currState.X;
B.Y = currState.Y;
if (B.Y > A.Y)
if (B.X > A.X) //Bottom-right
targetRotation = Math.Atan((B.Y - A.Y) / (B.X - A.X)) + NINETY_DEGREES;
else //Bottom-left
targetRotation = (Math.Atan((A.X - B.X) / (B.Y - A.Y))) + (NINETY_DEGREES * 2);
else
if (B.X > A.X) //Top-right
targetRotation = Math.Atan((B.X - A.X) / (A.Y - B.Y));
else //Top-Left
targetRotation = Math.Atan((A.Y - B.Y) / (A.X - B.X)) + (NINETY_DEGREES * 3);
if (Rotation > targetRotation)
Rotation -= turningRadius;
else
Rotation += turningRadius;
prevState = currState;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, Position, null, Color.White, (float)Rotation, new Vector2(Texture.Width / 2, Texture.Height / 2), 0.5f,
SpriteEffects.None, 0.0f);
}
}
}

Let me make sure I understand. You have a vector (Cos(Rotation), Sin(Rotation) representing which direction your object is facing; another vector (B.X-A.X, B.Y-A.Y) representing the direction you want your object to face; and you want to know whether to rotate your object (the first vector) clockwise or counter-clockwise to face it in the
direction of the second vector?
Simple, just treat them both as 3D vectors (set Z = 0) and take their cross product.
If the resulting vector has a positive Z component, rotate counter-clockwise
If it has a positive Z component, rotate clockwise
If the Z component is 0, the two vectors are parallel, so just check if they are facing opposite directions (and rotate either way) or the same direction (nothing to do!)
This works because of the right-hand rule which defines cross-products.

This should just reverse direction to whichever is faster.
spin = targetRotation - Rotation;
if (abs(spin) > Math.PI)
spin *= -1;
if (spin > 0
Rotation += turningRadius;
else
Rotation -= turningRadius;
Edit: Changed 180 to Math.PI

Try the following (This is some code from a tank game I made, not sure if it still applies in this scenario). I trimmed it down to the essentials:
using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace Physics
{
public class Ship
{
public Texture2D Texture { get; set; }
public Vector2 Position { get; set; }
public double Rotation { get; set; }
private MouseState currState;
public Ship(Texture2D texture, Vector2 position)
{
Texture = texture;
Position = position;
Rotation = 0;
}
public void Update()
{
currState = Mouse.GetState();
var mouseloc = new Vector2(currState.X, currState.Y);
Vector2 direction = Position - mouseloc;
Rotation = Math.Atan2(direction.Y, direction.X) + MathHelper.PiOver2;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, Position, null, Color.White, (float) Rotation,
new Vector2(Texture.Width/2, Texture.Height/2), 0.5f,
SpriteEffects.None, 0.0f);
}
}
}
I think you just need a little more math to modify the rate of the rotation, I'll come back if I come up with a solution.
Also, this code will vary based on the default rotation of the sprite. In my example, it assumes that the sprite is facing downwards (6 o'clock). Hope this points you in the right direction!

Edit my apologies I removed my head from my posterior and actually read the question this time. So here's a nice little method that I spent ~3 hours writing up tonight.
private float AngleLerp(float nowrap, float wraps, float lerp) {
float c, d;
if (wraps < nowrap) {
c = wraps + MathHelper.TwoPi;
//c > nowrap > wraps
d = c - nowrap > nowrap - wraps
? MathHelper.Lerp(nowrap, wraps, lerp)
: MathHelper.Lerp(nowrap, c, lerp);
} else if (wraps > nowrap) {
c = wraps - MathHelper.TwoPi;
//wraps > nowrap > c
d = wraps - nowrap > nowrap - c
? MathHelper.Lerp(nowrap, c, lerp)
: MathHelper.Lerp(nowrap, wraps, lerp);
} else { return nowrap; } //Same angle already
return MathHelper.WrapAngle(d);
}
It lerps the nowrap towards the wraps value. The wraps angle can be the one that jumps from 0 to TwoPi and back so in this case the angle of the mouse. nowrap should be your objects angle.
Hope I was actually helpful this time.

Related

Trying to create enter the gungeon type weapons however stuck on making weapons rotate to face mouse

Im making a game similar to Enter The Gungeon and am working in a similar space to them in unity. I'm using unity 3D and making a 2.5D game to give it the same perspective as gungeon. However I'm trying to make it so the weapon will face the mouse and its not working. Ive tried multiple tutorials and none of them have worked.
The issue is that it changes the rotation of all other axes to 0 when the game is in a top down perspective so I rotated the X of the quad to 90. From my experimenting I need the Y axis to follow the mouse. Unless there is a better way to go about this how can I do this with my current setup?
here's the code ive been using that somewhat worked but it resets all axes but Z.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseFollow : MonoBehaviour
{
private void Start()
{
}
private void Update()
{
var dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
The simplest solution I can think of is to create an empty object and make it the parent of the gun model. This will ensure that the gun (child GameObject) can keep its own rotation without resetting it while the parent GameObject can be rotated on the y axis using the method you described above.
If this is not an option, you could create another Quaternion for all of the rotations that you want to keep, which are independent of the mouse direction. Then, for each frame, multiply the direction you generate by it to receive the rotation you want.
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward) * thePersistingQuaternion
I created this function once while trying to accomplish something similar to this.
The function takes in 2 Vector3 points and returns the Quaternion required for the object to 'Look At' the other point.
p1 is the origin point.
p2 is the point you want to look at.
For your solution you could use the transform of the gun object as the origin point, and use your mouse position as the point you want to look at.
public static Quaternion LookAt(Vector3 p1, Vector3 p2)
{
float angle = 0;
float opp;
float adj;
if (p1.x == p2.x)
{
//Looking down
if (p1.y > p2.y)
{
angle = 0;
}
//Looking up
else
{
angle = 180;
}
}
else if (p1.y == p2.y)
{
//Looking right
if (p1.x < p2.x)
{
angle = 90;
}
//Looking left
else
{
angle = 270;
}
}
else
{
if (p1.x < p2.x)
{
//First quadrant angle +0
if (p1.y > p2.y)
{
angle = 0;
opp = Mathf.Abs(p1.x - p2.x);
adj = Mathf.Abs(p1.y - p2.y);
}
//Second quadrant angle +90
else
{
angle = 90;
adj = Mathf.Abs(p1.x - p2.x);
opp = Mathf.Abs(p1.y - p2.y);
}
}
//if (p1.x > p2.x)
else
{
//Third quadrant angle +180
if (p1.y <= p2.y)
{
angle = 180;
opp = Mathf.Abs(p1.x - p2.x);
adj = Mathf.Abs(p1.y - p2.y);
}
//Forth quadrant angle +270
else
{
angle = 270;
adj = Mathf.Abs(p1.x - p2.x);
opp = Mathf.Abs(p1.y - p2.y);
}
}
float a = Mathf.Atan(opp / adj) * 180f / Mathf.PI;
angle += a;
}
Quaternion rotation = Quaternion.Euler(0, 0, angle);
return rotation;
}

Ball is getting more and more energi for each collision

Im trying to write some simple 2d physics in monogame.
I release a ball from a given start position with a given velocity and I want it to bounce back up when it is colliding with the floor.
My problem is that I seem to give the ball more energi for each bounce i.e. it bounces higher and higher for each collision with the floor. It should be the other way around.
I have:
float get_VelocityX(float _speed, double _angle)
{
return velocity_x = velocity_x +_speed * (float)Math.Cos(_angle);
}
public float get_VelocityY(float _speed, double _angle, float _t, float gravity)
{
return velocity_y = velocity_y + _speed * (float)Math.Cos(_angle); // - (float)(-gravity * _t);
}
And in my Update function I have this:
if (speed > 0)
{
timeCount += (float)gameTime.ElapsedGameTime.TotalSeconds;
t += timeCount;
}
else
{
return;
}
Vx = ball.get_VelocityX(speed, angle);
Vy = ball.get_VelocityY(speed, angle, t, gravity);
if (posX >= windowMAX)
{
posX = posX + -Vx * friction * t;
}
if (posY > windowMIN)
{
posY = posY + -Vy * friction * t;
}
else
{
posY += gravity;
}
ballRect.X = (int)posX;
ballRect.Y = (int)posY;
Where posX, posY and speed are user inputs for start position and velocity.
Gravity is just a float = 9.82f;
Right now Im not doing anything with the posX except setting the balls starting position. Next step will be to implement a throwing motion.
EDIT:
Friction = 0.001f;
t is deltatime.
I went through your logic and have prepared a sample code. Please read the following before you go through it.
In order to simulate real-life motion, you need to implement the physics accurately. Although your implemented velocity and position seems mostly correct, the gravity needs to be treated as acceleration, and therefore adding its value to the position (as done in your code) is incorrect. I assume that this is the reason why you aren't getting your expected result since the value of increment on the Y-component of position is far greater than it should be.
Instead of keeping PosX, PosY for the position, Velocity_X..(), Velocity_Y..() for velocity, I would advise you to use struct Vector2 as shown below in my code, which is included in the Monogame framework and has a lot more helping functions built-in. This will help in making your code shorter and cleaner.
I did not understand why you used the Cosine of the given angle in your implementation of Velocity for both its X and Y components. My code below is ignoring this.
You can see my code below. Here the bouncing object Box is of the type struct PhyObj with all the needed Physics of motion implemented within it.
public class Game1 : Game
{
private SpriteBatch _batch;
internal Texture2D Texture;
public static Vector2 GravityAcceleration => new Vector2(0, 9.8f);//Constant accerleration along Y-Axis
internal Rectangle Ground;
internal PhyObj Box;
public Game1()
{
_ = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void LoadContent()
{
Point center = Window.ClientBounds.Center;
Box = new PhyObj(new Vector2(center.X, 0), Vector2.Zero, 30, 30);
Ground = new Rectangle(0, center.Y, center.X * 2, 10);
_batch = new SpriteBatch(GraphicsDevice);
Texture = new Texture2D(GraphicsDevice, 1, 1);
Texture.SetData(new[] { Color.White });
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
Box.Accelerate(GravityAcceleration, gameTime);
if (Box.Pos.Y > Ground.Top - Box.Dest.Height)//Check if bounce needed
{
Box.Pos.Y = Ground.Top - Box.Dest.Height;//Clipping
Box.Vel.Y *= -1; //Bouncing
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_batch.Begin();
_batch.Draw(Texture, Ground, Color.Black);
_batch.Draw(Texture, Box.Dest, Color.White);
_batch.End();
base.Draw(gameTime);
}
}
public struct PhyObj
{
internal static float friction => 0.005f;
public PhyObj(Vector2 x, Vector2 v, int width, int height)
{
Pos = x;
Vel = v;
Dest = new Rectangle(0, 0, width, height);
(Dest.X, Dest.Y) = ((int)Pos.X, (int)Pos.Y);
}
internal Vector2 Pos, Vel;
internal Rectangle Dest;
public void Accelerate(Vector2 acc, GameTime time)
{
Vel += acc - Vel * friction;
Pos += Vel * (float)time.ElapsedGameTime.TotalSeconds;
(Dest.X, Dest.Y) = ((int)Pos.X, (int)Pos.Y);
}
}
As shown in the Update() function, the PhyObj Box is being accelerated externally (In this case gravity, but you can add your custom external force), and the velocity/position it needs to attain is calculated internally.
The bouncing logic is simple: The Y-component of velocity is inverted.
The "Clipping" process here makes sure that the Box does not cross the Ground object even when the downward acceleration is acting upon it.
The next subsequent bounces have their height reduced due to the friction value (Done internally by the struct PhyObj).

Controlling both angles of a Ray

I'm new to Unity and have a problem I can't figure out. I want objects to spawn randomly at a distance of 20 from a FPS player. You could say the objects need to spawn on the surface of a half sphere with the player as the center. But: not all of that sphere can be used. The "highest" part is too high for objects to spawn, so basically it's a sphere with the top cut off.
What I tried:
thePosition = Random.onUnitSphere * 20 + object2.transform.position;
Obviously, this takes into account the whole sphere (should be only half a sphere) and doesn't take into account the "cut off" part.
So I thought: I basically want to make a ray that can pivot on the ground (so the max angle is 360°), and can go up and down, with a max angle of 90°. Think of it like a canon that can turn (pivot) and go up/down with an angle. Here's an image of what I mean:
So I tried:
Vector3 raydirection = new Vector3 (1f, 1f, 0);
raydirection = Quaternion.Euler (45, 0, 0) * raydirection;
Ray ray = new Ray (player.transform.position, raydirection);
thePosition = ray.GetPoint (20);
But that doesn't allow me to control the pivot angle (angle 1) and the "up-down" angle (angle 2) separately.
So my question is: how can I make it so that I can control both angles of this ray? Because if I can do that, I can just take a random number between 0 and 360 for the pivoting part, and between 0 and 90 for the up/down part.
Any help is much appreciated!
Coincidentally, I needed something very similar to this. The following Behavior will spawn a certain prefab (objectToSpawn) exactly spawnCount times within the set parameters.
The helper class (bottom code) generates a Vector from Yaw, Pitch and a Vector (basically the distance in your case).
What it does:
Pick a random direction (yaw and pitch) within set parameters
Pick a random distance (sounds like you can omit this step)
Calculate the vector
Spawn object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomSpawner : MonoBehaviour {
public GameObject objectToSpawn;
public int spawnCount;
public float minDistance = 2;
public float maxDistance = 10;
public float minPitchDegrees = 0;
public float maxPitchDegrees = 45;
public float minYawDegrees = -180;
public float maxYawDegrees = 180;
void Start ()
{
for (int i = 0; i < spawnCount; i++)
{
float distance = minDistance + Random.value * (maxDistance - minDistance);
float yaw = minYawDegrees + Random.value * (maxYawDegrees - minYawDegrees);
float pitch = minPitchDegrees + Random.value * (maxPitchDegrees - minPitchDegrees);
Vector3 position = RotationHelper.ConvertYawPitch (Vector3.forward * distance, yaw, pitch);
Instantiate (objectToSpawn, position, Quaternion.identity);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class RotationHelper {
public static Vector3 ConvertYawPitch(Vector3 vector, float yaw, float pitch)
{
Quaternion yawRotation = Quaternion.AngleAxis (yaw, Vector3.up);
Vector3 yawedZAxis = yawRotation * Vector3.left;
Quaternion pitchRotation = Quaternion.AngleAxis (pitch, yawedZAxis);
Vector3 yawedVector = yawRotation * vector;
Vector3 position = pitchRotation * yawedVector;
return position;
}
}
In your specific case, the parameters should be:
minDistance = 20
maxDistance = 20
minPitchDegrees = 0
maxPitchDegrees = 0-90, whatever the angle is after you "cut off the top"
minYawDegrees = -180
maxYawDegrees = 180
I want objects to spawn randomly at a distance of 20 from a FPS
player.
What I understood from this is that you want to spawn objects on the ground, distant 20 units away from the player, in random directions.
You could say the objects need to spawn on the surface of a half
sphere with the player as the center.
Now, this is just another way to make things complex. No need to use the sphere to solve this.
If you want to spawn objects on the surface, easiest solution will be to get a random angle in relation with Vector3.up and walk for 20 units to find the desired point.
Script:
public class Spawner : MonoBehaviour
{
public Transform player;
public Transform prefab;
[Range(10,50)]
public float distance = 20f;
IEnumerator Start()
{
while (true)
{
yield return new WaitForSeconds(0.05f);
Spawn();
}
}
[ContextMenu("Spawn")]
public void Spawn()
{
Vector3 spawnPoint = FindPoint(player.position, distance, Random.Range(0, 360));
Instantiate(prefab, spawnPoint, Quaternion.identity, transform);
}
[ContextMenu("Clear")]
public void Clear()
{
foreach (var item in transform.GetComponentsInChildren<Transform>())
{
if (item != transform)
DestroyImmediate(item.gameObject);
}
}
Vector3 FindPoint(Vector3 center, float radius, int angle)
{
return center + Quaternion.AngleAxis(angle, Vector3.up) * (Vector3.right * radius);
}
}
Result:
Calculates random point based on player's position:
Hope this helps :)

Click based movement not working

I have the player moving towards where i have clicked. But when i arrive at the position of where i clicked, the sprite just flickers back and forth. I assume its because the sprite is going past this point, going back to it, going past it again, then going back constantly creating a 'flickering'. Any ideas why?
****SOLVED********
***UPDATED CODE***
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace AsteroidAvoider
{
class Player
{
public Vector2 position, distance, mousePosition;
public float speed;
public float rotation;
public Texture2D playerImage;
public MouseState mouseState;
public Player(Texture2D playerImage, Vector2 position, float speed)
{
this.playerImage = playerImage;
this.position = position;
this.speed = speed;
}
public void Update(GameTime gameTime)
{
mouseState = Mouse.GetState();
float speedForThisFrame = speed;
if (mouseState.LeftButton == ButtonState.Pressed)
{
mousePosition.X = mouseState.X;
mousePosition.Y = mouseState.Y;
}
if ((mousePosition - position).Length() < speed)
speedForThisFrame = 0;
if ((mousePosition - position).Length() > speed)
speedForThisFrame = 2.0f;
distance = mousePosition - position;
distance.Normalize();
rotation = (float)Math.Atan2(distance.Y, distance.X);
position += distance * speedForThisFrame;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(playerImage, position, null, Color.White, rotation, new Vector2(playerImage.Width / 2, playerImage.Height / 2), 1.0f, SpriteEffects.None, 1f);
}
}
}
The normalized vector , if I remember correctly should always have a total length of 1 (Vector2.Length() returns the length of the entire vector2 by the way)
A simple solution would be making your unit reduce the speed of movement if within the range of the mouse , for example
float speedForThisFrame = speed;
if((mousePosition-position).Length() < speed) speedForThisFrame = (mousePosition-position).Length();
and use speedForThisFrame for the position offsetting.
You need a collider, a Rectangle that represents your player, so when this collider contains your mousePosition or the click coordinate (you can easily detect it using Rectangle.Contains method) you simply set player's speed as 0, avoiding the flickering.
Of course, when the clicked coordinate changes, you have to set the previous player's speed.

Rotation over time in 3D

Im working with Xna 4, doing a game where i have a game object (spaceship) that moves in a 3D world on the Y axis = 0 plane.. Aka 2.5D..
Until now i used a very complex angle calculation to create a rotation between 2 vectors, yet that algorithm lacks the ability to take into account that the object already is rotated. so the results get funkey..
Therefore i was hoping that someone, could show me a smart and easily implementable way to use Matrices and vector math, to do such a rotation over time thingy.
What i noticed in previous searches, is that people have the following variables in their object classes:
- Position vector3
- right vector3
- up vector3
- Rotation matrix
- transformMatrix matrix
- velocity vector3
- etc..
often i ask myself why its needed to have that many variables for a simple current position.. or maybe im not understanding.. anyways..
I have the position, rotation and transformsMatrix currently, and would like to know what else i need and HOW to calculate it, and then how you would implement JUST the rotation over time.
The method that is called by my right-click movement command trig sends a vector3 position on the Y = 0 plane of where the click happened.
public void MoveCommand(Vector3 pos){ }
ive tested this, and the "pos" given is accurate. Any help will be apreciated ..
You should check the Matrix.CreateRotationX Y or Z acording to the rotation that you want.
X,Y or Z is the axis of the rotation,
If you choose Y you will see a "2D" rotation (yaw) because that is the axis that you are using as depth.
If you choose X or Z axis you will see "3D" rotations (pitch and roll)
The code should look like this:
WorldMatrix = Rotations * Translation
where
Rotations = Matrix.CreateRotationX(angleRadians)
and
Translation = Matrix.CreateTranslation(Position);
The world matrix is the matrix that is affecting your model, the view and projection depends on the camera
Now if you want to know the angle between vectors you should check the dot product or the atan2 function because you are in 2D
Vector3 Position;
float Rotation;
Matrix World
{
get
{
return Matrix.CreateRotationZ(Rotation) * Matrix.CreateTranslation(Position);
}
}
public void RotateInstantly(Vector3 position)
{
Rotation = Math.Atan2(Position.Y - position.Y, Position.x - position.x);
}
public void RotateIncremently(Vector3 position, float maxStep)
{
float targetRotation = Math.Atan2(Position.Y - position.Y, Position.x - position.x);
float diff = targetRotation - Rotation;
if (Math.Abs(diff) > maxStep)
{
if (targetRotation > Rotation)
Rotation += maxStep;
else
Rotation -= maxStep;
}
else
Rotation = targetRotation;
}
You can use the RotateIncremently like this:*
float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
float maxRotationVelocity = Math.TwoPi; //2* Pi is one revolution.
RotateIncremently(target.Position, maxRotationVelocity * dt);
Thanks to Stig-Rune Skansgård's reply (if your danish hi5), i fixed my old Angle calculation and got it to work in every case.. So i thought i would answer my own question with the solution that works for me, so that future visitors can benefit of it.. This is a snippet of a very large Ship class + a helper method to calculate the angle:
public static float CalculateAngleFromVectors(Vector3 v1, Vector3 v2) {
float x = 0;
float X = v2.X - v1.X,
Z = v2.Z - v1.Z;
if (Z == 0) {
x = (X < 0 ? 90 : 270);
} else if (X == 0) {
x = (Z < 0 ? 180 : -180);
} else {
float temp = MathHelper.ToDegrees((float)Math.Atan(X / Z));
if (X < 0) {
x = (Z < 0 ? Math.Abs(temp) : 180 - Math.Abs(temp));
} else {
x = (Z < 0 ? 360 - Math.Abs(temp) : Math.Abs(temp) + 180);
}
}
return x;
}
this method gets you the float angle (in degrees) to rotate your ship (from the standard 0 degrees starting point)..
to use it, simply make some update / animation method in your class that does something like this:
float desiredRotation = 0, currentRotation = 0, totalElapsed = 0, timePerFrame = 0.05f;
if (desiredRotation != 0) {
totalElapsed += elapsed;
if (totalElapsed > timePerFrame) {
if (isRotationComplete()) {
rotX += MathHelper.ToRadians(desiredRotation);
currentRotation = desiredRotation = 0;
} else if (desiredRotation > currentRotation) {
currentRotation += shipTurnSpeed;
} else if (desiredRotation < currentRotation) {
currentRotation -= shipTurnSpeed;
}
totalElapsed -= timePerFrame;
}
}
EDIT: and the completion check:
private bool isRotationComplete() {
bool b = false;
if (desiredRotation > currentRotation && currentRotation + shipTurnSpeed > desiredRotation) {
b = true;
} else if (desiredRotation < currentRotation && currentRotation - shipTurnSpeed < desiredRotation) {
b = true;
}
return b;
}
essentially what this does is to always check wether or not DesiredRotation is bigger than 0.. if it is, then that means the player has given the command to rotate (or the AI).. CurrentRotation in my example is ofc how much has been rotated since a rotation command was last given, and is set to 0 once the rotation is complete.. You should have a Rotations matrix that uses a different variable to display the rotation with.. mine is this:
public float rotX { get; set; }
public float rotY { get; set; }
public Vector3 position { get; set; }
public Matrix Transform {
get {
return (Matrix.Identity *
Matrix.CreateScale(scale) *
Matrix.CreateRotationY(MathHelper.Pi) *
Rotation *
Matrix.CreateTranslation(position));
}
}
public float ShipCurRotation { get { return (rotX + MathHelper.ToRadians(currentRotation)); } }
public Matrix Rotation { get { return (Matrix.CreateRotationY(ShipCurRotation) * Matrix.CreateRotationX(rotY)); } }
The rotX variable is set in my animation when the rotation is complete, and also at Init.. And here is how i use the rotation angle that my first code snippet generates for me:
public void MoveToPosition(Vector3 pos) {
desiredRotation = (CalculateAngleFromVectors(position, pos) - MathHelper.ToDegrees(rotX));
isMoving = true;
}
.. this makes a smooth customizable rotation, transforms and movement setup.. In a XZ plane ofc.. the Y axis is UP and always 0..
Feel free to comment on this, if you have suggestions or changes or ideas to make things even better.. Im always open for improvements.. Thanks for the replies, and hope this helps alot of new developers, took me forever to gather this stuff from the web..
PS. the rotation can be applied directly to rotX for an instant rotation, bypassing the animation and turnspeed..
WithRegards
MatriXz

Categories

Resources