Problems determining similarity of angles - c#

I'm working on implementing Context Steering movement for the enemy AI in my unity project and running into problems when it comes to determining the interest of each angle.
I have two classes: ContextMap and DirectionContext.
DirectionContext is a data storage class. It contains an angle, a Vector3 that relates that angle into a direction to move in, and a float ranging from 0 to 1 depending on how interesting that angle is to move in.
ContextMap contains a list of DirectionContext depending on the "detail" of the contextmap. So if I set the detail to 4, it will generate 4 DirectionContexts, and if I set the detail to 8, it will generate 8 DirectionContexts. The angles go from -180, to +180.
What I want to do is write a function that determins the angle between the enemy gameobject and the player gameobject, then compare it to each DirectionContext in the context map. I want it to set the interest float for each DirectionContext depending on the similarity of the angle between the objects and the DirectionContexts angle. I want it to set interest to 1 if the directions are identical, and to set interest to 0 if the difference between the angles is a high as they can be (360.)
The issue I'm having, is my scripts aren't setting the interest for the angles correctly. If the Player is directly above the enemy, it works as expected, but if the player is to the diagonals or sides it starts working opposite.
Here's a picture of what's happening: The black object is the enemy, the red is the human. The lines are longer depending on how high the interest is.
Link to the image
As you can see in the image, if the directions are up/down then it works fine, but things get weird when it goes diagonal or to the sides.
Here's what I've come up with in code:
public class DirectionContext
{
[SerializeField] private Vector3 direction;
[SerializeField] private float interest;
[SerializeField] private float angle;
[SerializeField] private bool isExcluded = false;
public Vector3 GetDirection() { return direction; }
public float GetInterest() { return interest; }
public void SetInterest(float value) { interest = value; }
public void CheckInterestAndSet(float newInterest)
{
if (newInterest > interest)
interest = newInterest;
}
public float GetAngle() { return angle; }
public DirectionContext(float angle)
{
this.angle = angle;
this.direction = new Vector3(Mathf.Sin(angle * Mathf.Deg2Rad), Mathf.Cos(angle * Mathf.Deg2Rad), 0).normalized;
}
}
Context map:
public class ContextMap
{
[SerializeField] private List<DirectionContext> contextMap = new List<DirectionContext>();
public List<DirectionContext> GetContext()
{
return contextMap;
}
public void GenerateMap(int detail)
{
contextMap.Clear();
for (int i = 0; i < detail; i++)
{
float angle = (360 / detail * i) - 180;
contextMap.Add(new DirectionContext(angle));
}
}
public void UpdateContext(int index, float interest)
{
contextMap[index].CheckInterestAndSet(interest);
}
public void DebugDrawMap(Vector3 position, Color color)
{
foreach (DirectionContext direction in contextMap)
{
Debug.DrawLine(position, (position + direction.GetDirection()).normalized * direction.GetInterest());
}
}
public void Clear()
{
foreach (DirectionContext dir in contextMap)
dir.Clear();
}
}
Seek: (I left out SteeringContext as it's a super simple class. It just contains variables that may be interesting. The two used in seek are parent and targetDestination. TargetDestination is the players location, and parent is a reference to the enemy.
public class Seek : SteeringBehavior
{
public override ContextMap AddContext(ContextMap contextMap, SteeringContext steeringContext)
{
float angleToTarget = AngleBetween(Vector3.up, steeringContext.targetDestination - steeringContext.parent.transform.position);
//Debug.Log(angleToTarget);
for (int i = 0; i < contextMap.GetContext().Count; i++)
{
float delta = Mathf.Abs(Mathf.Max(angleToTarget, contextMap.GetContext()[i].GetAngle()) - Mathf.Min(angleToTarget, contextMap.GetContext()[i].GetAngle()));
if (delta > 180)
{
delta = 360f - delta;
}
float interest = 1f - delta / 180f;
contextMap.UpdateContext(i, interest);
}
return contextMap;
}
private float AngleBetween(Vector3 vector1, Vector3 vector2)
{
float sin = vector1.x * vector2.y - vector2.x * vector1.y;
float cos = vector1.x * vector2.x + vector1.y * vector2.y;
return Mathf.Atan2(sin, cos) * (180) / Mathf.PI;
}
}
And I make the call to Seek using this function:
private void PopulateInterestMap()
{
interestMap.Clear();
foreach (SteeringBehavior behavior in interestBehaviors)
{
interestMap = behavior.AddContext(interestMap, steeringContext);
}
}
Thank you for any help!

I solved it. I'm going to answer my own question as there's no source code online that really goes into Context Steering behaviors, so maybe the code posted here will save somebody else the trouble I went through.
Now that I have it up and working, anybody messing around with steering behaviors I highly recommend looking into Context Steering (If you google there's some good blog posts and papers about it's benefits.) It totally fixed the jitter issues with my steering behaviors, and fixed all of the issues of blended behaviors cancelling themselves out in oddball situations.
The problem wasn't in determining the weight, but the part of DirectionContext where I convert the angle into a direction vector.
this.direction = new Vector3(Mathf.Sin(angle * Mathf.Deg2Rad), Mathf.Cos(angle * Mathf.Deg2Rad), 0).normalized;
Should have been
this.direction = new Vector3(Mathf.Cos(angle * Mathf.Deg2Rad), Mathf.Sin(angle * Mathf.Deg2Rad), 0);
The normalized isn't what ruined it, it was Cos vs Sin. Normalized was just unneccessary.

Related

Instantiate predefined number of object along a raycast in Unity

I have a raycast that's being rendered every frame based on 2 points, and those 2 points change position each frame.
What I need is a system that doesn't need a direction, or a number of objects, but instead takes in 2 points, and then instantiates or destroys as many objects as necessary to get the instantiated objects from one side to another minus spaceBetweenPoints. If you wanted you could think of it as an Angry Birds Style slingshot HUD, except without gravity, and based on raycasts.
My Script:
public int numOfPoints; // The number of points that are generated (This would need to chnage based one the distance in the end)
public float spaceBetweenPoints; // The spacing between the generated points
private GameObject[] predictionPoints; // The prefab to be gernerated
private Vector2 firstPathStart; // The starting point for the raycast (Changes each frame)
private Vector2 firstPathEnd; // The ending point for the raycast (Changes each frame)
void start()
{
predictionPoints = new GameObject[numOfPoints];
for (int i = 0; i < numOfPoints; i++)
{
predictionPoints[i] = Instantiate(predictionPointPrefab, firePoint.position,
Quaternion.identity);
}
}
void Update
{
Debug.DrawLine(firstPathStart, firstPathEnd, UnityEngine.Color.black);
DrawPredictionDisplay();
}
void DrawPredictionDisplay()
{
for (int i = 0; i < numOfPoints; i++)
{
predictionPoints[i].transform.position = predictionPointPosition(i * spaceBetweenPoints);
}
}
Vector2 predictionPointPosition(float time)
{
Vector2 position = (Vector2)firstPathStart + direction.normalized * 10f * time;
return position;
}
The current system simply takes in a starting position, a direction, and then moves a preset number of objects in that direction based on time. This way of doing it also causes problems because it's endess instead of only going till the end of the raycast: (Pardon my drawing skills)
Blue line = raycast
Black dots = instantiated prefab
Orange dot = raycast orange
Green dot = end of raycast
Notes:
direction is the momentum which I set in the editor, I needed it to put together what I currently have working, but it shouldn't be necessary when running based on points.
If you ask me I would say it is kinda easy if you know little bit of Math trickery. I'm not saying that I'm very good at Math, but once you get it it's kind of easy to pull off next time. Here if I try to explain everything, i won't be able to explain clearly. Take a look as the code below I've commented the whole code so that you can understand easily.
Basically I used a Method called Vector2.Lerp() Liner Interpolation, which means that this method will return value between point1, and point2 based on the value of 3rd argument t which goes from 0 to 1.
public class TestScript : MonoBehaviour
{
public Transform StartPoint;
public Transform EndPoint;
public float spaceBetweenPoints;
[Space]
public Vector2 startPosition;
public Vector2 endPosition;
[Space]
public List<Vector3> points;
private float distance;
private void Update()
{
startPosition = StartPoint.position; //Setting Starting point and Ending point.
endPosition = EndPoint.position;
//Finding the distance between point
distance = Vector2.Distance(startPosition, endPosition);
//Generating the points
GeneratePointsObjects();
Debug.DrawLine(StartPoint.position, EndPoint.position, Color.black);
}
private void OnDrawGizmos()
{
//Drawing the Dummy Gizmo Sphere to see the points
Gizmos.color = Color.black;
foreach (Vector3 p in points)
{
Gizmos.DrawSphere(p, spaceBetweenPoints / 2);
}
}
private void OnValidate()
{
//Validating that the space between two objects is not 0 because that would be Raise an exception "Devide by Zero"
if (spaceBetweenPoints <= 0)
{
spaceBetweenPoints = 0.01f;
}
}
private void GeneratePointsObjects()
{
//Vlearing the list so that we don't iterate over old points
points.Clear();
float numbersOfPoints = distance / spaceBetweenPoints; //Finding numbers of objects to be spawned by dividing "distance" by "spaceBetweenPoints"
float increnment = 1 / numbersOfPoints; //finding the increment for Lerp this will always be between 0 to 1, because Lerp() takes value between 0 to 1;
for (int i = 1; i < numbersOfPoints; i ++)
{
Vector3 v = Vector2.Lerp(startPosition, endPosition, increnment * i); //Find next position using Vector2.Lerp()
points.Add(v);//Add the newlly found position in List so that we can spwan the Object at that position.
}
}
}
Update: Added, How to set prefab on the positions
I just simply Destroyed old objects and Instantiated new Objects. But remember instantiating and Destroying object frequently in your game in unity will eat-up memory on your player's machine. Os I would suggest you to use Object-Pooling. For the reference I'll add a link to tutorial.
private void Update()
{
startPosition = StartPoint.position; //Setting Starting point and Ending point.
endPosition = EndPoint.position;
//Finding the distance between point
distance = Vector2.Distance(startPosition, endPosition);
//Generating the points
GeneratePointsObjects();
//Update: Generating points/dots on all to location;
InstenciatePrefabsOnPositions();
Debug.DrawLine(StartPoint.position, EndPoint.position, Color.black);
}
private void InstenciatePrefabsOnPositions()
{
//Remove all old prefabs/objects/points
for (int i = 0; i < pointParent.childCount; i++)
{
Destroy(pointParent.GetChild(i).gameObject);
}
//Instantiate new Object on the positions calculated in GeneratePointsObjects()
foreach (Vector3 v in points)
{
Transform t = Instantiate(pointPrefab);
t.SetParent(pointParent);
t.localScale = Vector3.one;
t.position = v;
t.gameObject.SetActive(true);
}
}
Hope this helps please see below links for more reference
OBJECT POOLING in Unity
Vector2.Lerp
I hope I understood you right.
First, compute the A to B line, so B minus A.
To get the number of needed objects, divide the line magnitude by the objects' spacing. You could also add the diameter of the prediction point object to avoid overlapping.
Then to get each object position, write (almost) the same for loop.
Here's what I came up with, didn't tested it, let me know if it helps!
public class CustomLineRenderer : MonoBehaviour
{
public float SpaceBetweenPoints;
public GameObject PredictionPointPrefab;
// remove transforms if you need to
public Transform start;
public Transform end;
private List<GameObject> _predictionPoints;
// these are your raycast start & end point, make them public or whatever
private Vector2 _firstPathStart;
private Vector2 _firstPathEnd;
private void Awake()
{
_firstPathStart = start.position;
_firstPathEnd = end.position;
_predictionPoints = new List<GameObject>();
}
private void Update()
{
_firstPathStart = start.position;
_firstPathEnd = end.position;
// using any small value to clamp everything and avoid division by zero
if (SpaceBetweenPoints < 0.001f) SpaceBetweenPoints = 0.001f;
var line = _firstPathEnd - _firstPathStart;
var objectsNumber = Mathf.FloorToInt(line.magnitude / SpaceBetweenPoints);
var direction = line.normalized;
// Update the collection so that the line isn't too short
for (var i = _predictionPoints.Count; i <= objectsNumber; ++i)
_predictionPoints.Add(Instantiate(PredictionPointPrefab));
for (var i = 0; i < objectsNumber; ++i)
{
_predictionPoints[i].SetActive(true);
_predictionPoints[i].transform.position = _firstPathStart + direction * (SpaceBetweenPoints * i);
}
// You could destroy objects, but it's better to add them to the pool since you'll use them quite often
for (var i = objectsNumber; i < _predictionPoints.Count; ++i)
_predictionPoints[i].SetActive(false);
}
}

Strange outputs for a moving platform in Unity

First off, sorry it this isn't written very well, I've spend hours debugging this and I'm very stressed. I'm trying to make a moving platform in unity that can move between way-points, I don't want to have to have tons of gameobjects in the world taking up valuable processing power though so I'm trying to use something I can just add to the script through the editor.
The only problem is that it seems to be doing this at an incredible speed:
Black = The Camera View, Blue = The platform and where it should be going based on waypoints, Red = What it is currently doing.
I've spend hours trying to find a fix but I have no idea why it's doing this.
My Script on the Platform:
public Vector3[] localWaypoints;
Vector3[] globalWaypoints;
public float speed;
public bool cyclic;
public float waitTime;
[Range(0, 2)]
public float easeAmount;
int fromWaypointIndex;
float percentBetweenWaypoints;
float nextMoveTime;
void Start()
{
globalWaypoints = new Vector3[localWaypoints.Length];
for (int i = 0; i < localWaypoints.Length; i++)
{
globalWaypoints[i] = localWaypoints[i] + transform.position;
}
}
void Update()
{
Vector3 velocity = CalculatePlatformMovement();
transform.Translate(velocity);
}
float Ease(float x)
{
float a = easeAmount + 1;
return Mathf.Pow(x, a) / (Mathf.Pow(x, a) + Mathf.Pow(1 - x, a));
}
Vector3 CalculatePlatformMovement()
{
if (Time.time < nextMoveTime)
{
return Vector3.zero;
}
fromWaypointIndex %= globalWaypoints.Length;
int toWaypointIndex = (fromWaypointIndex + 1) % globalWaypoints.Length;
float distanceBetweenWaypoints = Vector3.Distance(globalWaypoints[fromWaypointIndex], globalWaypoints[toWaypointIndex]);
percentBetweenWaypoints += Time.deltaTime * speed / distanceBetweenWaypoints;
percentBetweenWaypoints = Mathf.Clamp01(percentBetweenWaypoints);
float easedPercentBetweenWaypoints = Ease(percentBetweenWaypoints);
Vector3 newPos = Vector3.Lerp(globalWaypoints[fromWaypointIndex], globalWaypoints[toWaypointIndex], easedPercentBetweenWaypoints);
if (percentBetweenWaypoints >= 1)
{
percentBetweenWaypoints = 0;
fromWaypointIndex++;
if (!cyclic)
{
if (fromWaypointIndex >= globalWaypoints.Length - 1)
{
fromWaypointIndex = 0;
System.Array.Reverse(globalWaypoints);
}
}
nextMoveTime = Time.time + waitTime;
}
return newPos - transform.position;
}
struct PassengerMovement
{
public Transform transform;
public Vector3 velocity;
public bool standingOnPlatform;
public bool moveBeforePlatform;
public PassengerMovement(Transform _transform, Vector3 _velocity, bool _standingOnPlatform, bool _moveBeforePlatform)
{
transform = _transform;
velocity = _velocity;
standingOnPlatform = _standingOnPlatform;
moveBeforePlatform = _moveBeforePlatform;
}
}
void OnDrawGizmos()
{
if (localWaypoints != null)
{
Gizmos.color = Color.red;
float size = .3f;
for (int i = 0; i < localWaypoints.Length; i++)
{
Vector3 globalWaypointPos = (Application.isPlaying) ? globalWaypoints[i] : localWaypoints[i] + transform.position;
Gizmos.DrawLine(globalWaypointPos - Vector3.up * size, globalWaypointPos + Vector3.up * size);
Gizmos.DrawLine(globalWaypointPos - Vector3.left * size, globalWaypointPos + Vector3.left * size);
}
}
}
UPDATE: Upon further testing I found that if the first object in my localWaypoint array is set to 0,0,0 and my 2nd object is set to 1,0,0 then the platform will spiral to the right, making sure to hit the waypoints as it's spiraling, and then spiraling out into nowhere like in the image above. But if I set my first object to 0,0,0 and my second object to -1,0,0 then the object will act the same way as before, but will spiral to the left as displayed in this image. (The second image has also bee updated to display how the platfrom makes sure to hit both waypoints before is spirals out into nowhere).
I've also noticed that if I set both waypoints to 0,0,0 then the platform stays still, these 2 things prove that it has somthing to do with the way the waypoints are being handled and not some other script or parent object interfering.
Using the updated numbers ([0,0,0], [1,0,0]) works in my test app. However, if I put a rotation on the object's Y axis, then I see behavior like you are seeing. In Update, if you change:
transform.Translate(velocity);
to
transform.Translate(velocity, Space.World);
You should see your desired behavior. Note that "transform.Translate(velocity)" is the same as "transform.Translate(velocity, Space.Self)". Your translation is being rotated.
If you are curious, take a look at this for more information on how the values in the transform are applied:
https://gamedev.stackexchange.com/questions/138358/what-is-the-transformation-order-when-using-the-transform-class

Coupling navigation and animation in unity

So I've implemented this tutorial: https://docs.unity3d.com/Manual/nav-CouplingAnimationAndNavigation.html almost to the letter. Almost means that I'm using the 2d freeform directional blend type instead of the simple directional they are using. The problem is that the values for velx and vely (mostly this one) are fluctuating. So for example when the vely is rising it will at some point reach 1, but before that it will go like this:
..., 0.5, 0.6, 0.5, 0.7, 0.4, 0.6, 0.8
Hopefully you get my point - the trend is rising but it occasionally goes down. This makes my animation jitter because the blend tree is jumping between states very rapidly. After some time of experimentation I found out, that in my case using number 7 as the divisor in the expression calculating the smooth variable, so like this:
var smooth = Mathf.Min(1.0f, Time.deltaTime / 7f);
Kinda works. This means that it jitters only 70% of the time, instead of always. Anyone knows a better method of accomplishing the same effect?
Sooo... To whoever is interested:
I wasn't abble to solve the jittering without some modifications to the script from the docs and my changes are as follows:
using UnityEngine;
using UnityEngine.AI;
[RequireComponent (typeof (NavMeshAgent))]
[RequireComponent (typeof (Animator))]
public class LocomotionSimpleAgent : MonoBehaviour {
private const float SmoothingCoefficient = .15f;
[SerializeField] private float _velocityDenominatorMultiplier = .5f;
[SerializeField] private float _minVelx = -2.240229f;
[SerializeField] private float _maxVelx = 2.205063f;
[SerializeField] private float _minVely = -2.33254f;
[SerializeField] private float _maxVely = 3.70712f;
public Vector3 Goal {
get { return _agent.destination; }
set {
_agent.destination = value;
_smoothDeltaPosition = Vector2.zero;
}
}
private NavMeshAgent _agent;
private Animator _animator;
private Vector2 _smoothDeltaPosition;
public void Start() {
_animator = GetComponent<Animator>();
_agent = GetComponent<NavMeshAgent>();
_agent.updatePosition = false;
_smoothDeltaPosition = default(Vector2);
Goal = transform.position;
}
public void FixedUpdate() {
var worldDeltaPosition = _agent.nextPosition - transform.position;
var dx = Vector3.Dot(transform.right, worldDeltaPosition);
var dy = Vector3.Dot(transform.forward, worldDeltaPosition);
var deltaPosition = new Vector2(dx, dy);
var smooth = Time.fixedDeltaTime / SmoothingCoefficient;
_smoothDeltaPosition = Vector2.Lerp(_smoothDeltaPosition, deltaPosition, smooth);
var velocity = _smoothDeltaPosition / (Time.fixedDeltaTime * _velocityDenominatorMultiplier);
var shouldMove = _agent.remainingDistance > .1f;
var x = Mathf.Clamp(Mathf.Round(velocity.x * 1000) / 1000, _minVelx, _maxVelx);
var y = Mathf.Clamp(Mathf.Round(velocity.y * 1000) / 1000, _minVely, _maxVely);
_animator.SetBool("move", shouldMove);
_animator.SetFloat("velx", x);
_animator.SetFloat("vely", y);
if (worldDeltaPosition.magnitude > _agent.radius / 16 && shouldMove) {
_agent.nextPosition = transform.position + 0.1f * worldDeltaPosition;
}
}
public void OnAnimatorMove() {
var position = _animator.rootPosition;
position.y = _agent.nextPosition.y;
transform.position = position;
}
}
There were also some bugs, like if you allowed the velocity to constantly rise or go down, at some point after clicking on the plane, the agent would start behaving weirdly and the above fixes it.
As for the values up top - they are matching what I could find in the example project, so if you have similar problems to mine, but different animations, you'll probably have to adjust the numbers there.

Unity C# Limit Orbit RotateAround Position

So this is my code I've made so far, its for a camera orbiting around a point in space.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Lean.Touch;
public class CameraOrbit : TopClass
{
[Tooltip("Ignore fingers with StartedOverGui?")]
public bool ignoreGuiFingers = true;
[Tooltip("Ignore fingers if the finger count doesn't match? (0 = any)")]
public int requiredFingerCount = 1;
[Tooltip("The sensitivity of the movement, use -1 to invert")]
public float sensitivity = 0.25f;
public Vector3 target = Vector3.zero;
protected void LateUpdate()
{
// Get the fingers we want to use
List<LeanFinger> fingers = LeanTouch.GetFingers(ignoreGuiFingers, requiredFingerCount);
// Get the scaled delta of all the fingers
Vector2 delta = LeanGesture.GetScaledDelta(fingers);
transform.RotateAround(target, Vector3.up, delta.x * sensitivity);
transform.RotateAround(target, Vector3.right, delta.y * sensitivity);
transform.LookAt(target);
}
}
This works great so far, however there are 2 things which are annoying and I'm at loss how to fix
The biggest one is when the camera reaches the top things can get weird if the user keeps moving up. The camera starts to kind of turn around, the world kind of spins etc... I want the camera to kind of stop when it gets near the top or bottom.
The camera's rotation can get weird, I want it to always be pointing up and never rotated to the right or left slightly or fully and especially upside down. Just always pointing up.
I've tried these links after lots of Google searching
http://wiki.unity3d.com/index.php?title=MouseOrbitImproved
https://answers.unity.com/questions/363353/how-to-limit-a-transform-movement-in-x-axis.html
https://answers.unity.com/questions/438836/limit-camera-rotation-with-rotatearound.html
https://answers.unity.com/questions/1087351/limit-vertical-rotation-of-camera.html
https://answers.unity.com/questions/1370422/limit-y-axis-transformrotatearound.html
But none of them worked right or I couldn't get them adapted to what I needed
Any help would be great, thanks in advance
So I found the answer so far, I think. I also had someone play test it and they liked it so I may have found the solution.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Lean.Touch;
public class CameraOrbit : TopClass
{
[Tooltip("Ignore fingers with StartedOverGui?")]
public bool ignoreGuiFingers = true;
[Tooltip("Ignore fingers if the finger count doesn't match? (0 = any)")]
public int requiredFingerCount = 0;
[Tooltip("The sensitivity of the movement, use -1 to invert")]
public float sensitivity = 0.25f;
public float min = 45f;
public float max = 315f;
public Vector3 target = Vector3.zero; //this is the center of the scene, you can use any point here
protected void LateUpdate()
{
// Get the fingers we want to use
List<LeanFinger> fingers = LeanTouch.GetFingers(ignoreGuiFingers, requiredFingerCount);
// Get the world delta of all the fingers
Vector2 delta = LeanGesture.GetScaledDelta(fingers);
transform.RotateAround(target, Vector3.up, delta.x * sensitivity);
transform.RotateAround(target, Vector3.right, delta.y * sensitivity);
Vector3 angles = transform.eulerAngles;
angles.x = Mathf.Clamp(angles.x, min, max);
angles.y = Mathf.Clamp(angles.y, min, max);
angles.z = 0;
transform.eulerAngles = angles;
transform.LookAt(target);
}
}
If I want the camera to always have the up point up, in my case, its keeping the z-axis at 0. The x and y axis just needed to be kept in check.
If it helps anyone, the only thing I modified from above is
...
public float min = 45f;
public float max = 315f;
...
Vector3 angles = transform.eulerAngles;
angles.x = Mathf.Clamp(angles.x, min, max);
angles.y = Mathf.Clamp(angles.y, min, max);
angles.z = 0;
transform.eulerAngles = angles;
Thanks to #yes for pointing me in the right direction

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