- Home /
Rotate Local Space Vector to World Space with heading vector
I'm implementing an 2D obstacle avoidance behaviour where I will calculate a steering force relative to a target within collision range.
My agent does not rotate but has a heading vector which is the unit vector of the current velocity.
The steering force is calculated in the local space of the agent where the the heading vector lies on the x-axis.
I believe I have obtained the steering force that lies in the local space, how can I rotate it so that it's relative to the heading vector?
public void ObstacleAvoidanceSteering(Collider2D other){
float closestDist = 0f;
//check if closestCollider exist
if(closestCollider2D != null){
//compare distance
closestDist = Vector2.Distance(transform.position, closestCollider2D.transform.position);
float newColliderDist = Vector2.Distance(transform.position, other.transform.position);
if(newColliderDist < closestDist){
closestCollider2D = other;
}
}else{
closestCollider2D = other;
}
closestDist = Vector2.Distance(transform.position, closestCollider2D.transform.position);
//calculate steering force
float angle = Vector3.Angle((other.transform.position - transform.position), vHeading);
float localXLength = Mathf.Cos( angle * Mathf.Deg2Rad ) * closestDist;
float localYLength = Mathf.Sin( angle * Mathf.Deg2Rad ) * closestDist;
float multiplier = 1.0f + (currentRadarLength - localXLength) / currentRadarLength;
float steeringForceY = (other.bounds.extents.y - localYLength) * multiplier;
float breakingWeight = 0.2f;
float steeringForceX = (other.bounds.extents.x - localXLength) * breakingWeight ;
Vector3 steeringForce = new Vector3(steeringForceX, steeringForceY, 0f);
//TODO: Rotate vector
rb2D.AddForce(steeringForce) ;
}
Your answer
Follow this Question
Related Questions
Why the object being moved is faster than the rest? 1 Answer
Why does this code not work? 1 Answer
Access 2 of the 3 components of Vector3 (Vector3.xy?) 2 Answers
2D Enemy chasing player not at a constant speed,Enemy following player at constant speed 0 Answers
What is the kEpsilon field on Vectors? 2 Answers