# Rotate object in space to direction [Simulation]

**1. The problem statement, all variables and given/known data**

—————————————————————————————–

**CONTEXT:**

I’m writing an AI simulation for am upcoming assessment in my computer programming course, and I’m having some trouble with the physics and calculations.

The simulation is of a dogfight in space, in 3D. I am calculating each objects’ position as a vector, by adding velocity to its position. The velocity is based off of its acceleration, which is based off of its force divided by mass, like so (everything but the mass is a 3D vector):

ACCELERATION = FORCE / MASS

VELOCITY = VELOCITY + ACCELERATION

POSITION = POSITION + VELOCITY

When I want to move the object, I call an ‘AddForce’ function, which requires another 3D vector – if I want to move forward, I pass in a vector such as (0, 0, 50), which provides 50N along the z axis.

This is the same as my rotation:

ANGULAR_ACCELERATION = TORQUE / MOMENT_OF_INERTIA

ANGULAR_VELOCITY = ANGULAR_VELOCITY + ANGULAR_ACCELERATION

ANGLE = ANGLE + ANGULAR_VELOCITY

Same deal, using an AddTorque function, where the amounts for each number of the vector represent the rotation around that vector, i.e. pitching forwards by 50 units of torque (don’t know the term), might be (50, 0, 0), around the X axis.

—————————————————————————————–

PROBLEM:

I’m trying to write the AI systems for flight, and need to calculate some information. For example, if I tell the AI to fly to position A, it needs to know when to turn around and counter-thrust (i.e. halfway).

More importantly, though, I need to be able to rotate the craft to face a certain direction. I’m currently trying to break it down axis-by-axis, where if the craft’s X axis of rotation is above the desired direction vector’s X component, it adds a torque to try to correct it. But this is clumsy, and results in rubber-banding.

**How can I calculate the required torque to rotate the craft to face a direction, including the counter-thrusting to reach said rotation with a final angular velocity of 0. I.e. it can’t over-shoot.**

I don’t have the maths chops to solve the required equations simultaneously.

**2. Relevant equations**

Not sure.

**3. The attempt at a solution**

In C++:

`glm::vec3 angle = glm::eulerAngles(physics->GetRotation());`

glm::vec3 diff = angle - glm::normalize(a_targetPosition - position);
if (diff.x > 0)

{

physics->AddTorque(glm::vec3(-0.1, 0, 0));

}

else if (diff.x < 0)

{

physics->AddTorque(glm::vec3(0.1, 0, 0));

}

if (diff.y > 0)

{

physics->AddTorque(glm::vec3(0, -0.1, 0));

}

else if (diff.y < 0)

{

physics->AddTorque(glm::vec3(0, 0.1, 0));

}

` if (diff.z > 0)`

{

physics->AddTorque(glm::vec3(0, 0, -0.1));

}

else if (diff.z < 0)

{

physics->AddTorque(glm::vec3(0, 0, 0.1));

}

Basically, calculating the direction between the craft’s position, and the desired position, as a vector, and reacting to each component one by one. This is insane, though, and fails to reach the desired heading.

Can anyone offer any advice? This is crucial to my assessment, but not an actual part of it, so it’s not so bad to ask for assistance.

Regards,

– HateDread.

**1. The problem statement, all variables and given/known data**

**2. Relevant equations**

**3. The attempt at a solution**

**1. The problem statement, all variables and given/known data**

**2. Relevant equations**

**3. The attempt at a solution**

http://ift.tt/1hka7dR

## Leave a comment