C++ pseudocode to Unity C#

Hi,

How would the following be translated to C#? this is from the Artifical Intelligence for Games book incase some people recognise it

struct SteeringOutput:

linear #a 2 or 3D Vector
angular #single floating value
struct Kinematic:

position # a 2 or 3D vector
orientation # a single floating point
velocity # another 2 or 3D vector
rotation # a single floating point

def update(steering, time):

position += velocity * time + .5 * steering.linear * time * time;
orientation += rotation * time + .5 * steering.angular * time * time;

velocity += steering.linear * time;
orientation += steering.angular * time

This is kinda as far as i get on my own

private Vector3 pos;
private Quaternion ori;
private Vector3 velocity;
private float rotation;

struct SteeringOutput
{
    Vector3 linear;
    float angular;
}
void Start()
{
  SteeringOutput steering = new SteeringOutput();
  steering.linear = new Vector(0,0,0);
  steering.angular = 20f;

  pos = transform.position;
  ori = transform.rotation;
  velocity = new Vector3(0,0,0);
  rotation = 20f;
}

void OnUpdate()
{
  //anything here i can't figure out
}

What you’ve got looks good so far, but I guess you’re not too sure about

def update(steering, time): 

position += velocity * time + .5 * steering.linear * time * time; 
orientation += rotation * time + .5 * steering.angular * time * time; 

velocity += steering.linear * time; 
orientation += steering.angular * time

What that is, is a function that would be called as part of the game’s update loop.

In Unity, you’d put it in one of the Update calls, or you could have an infinite loop that calls yield when it’s done, meaning it’ll get called once per frame, I think. (You may want to check which Update call or yield is best practice for Unity.)

I’ll just assume your OnUpdate is called once per frame. What you’ll need is code to handle user input, interacting with an instance of the SteeringOutput struct. In the pseudo code, it is passed to the update function and called steering, but if this script handles user input and the update (not ideal, but I think is doable in Unity) you can just have it as a local variable and not worry about passing it. The same goes for the psuedo code’s “time” parameter; can be kept in the single script.

So, what you’d do is something like the following:

private SteeringOutput m_output;
privete Timer m_time; // Not sure what the time class is called in Unity.
// other vars

void Start()
{
       m_steering = new SteeringOutput();
       m_time = Time.Now();
       // etc
}

void OnUpdate(/*pseudo code passes sterring/time here*/)
{
     TimeSpan dT = Time.Now() - m_time;
     
     // Move object
     position += velocity * dT + .5 * m_steering.linear * dT * dT; 
orientation += rotation * dT + .5 * m_steering.angular * dT * dT; 

// Update vel and orientation for next frame.
velocity += m_steering.linear * time; 
orientation += m_steering.angular * time;

    // The above formulae should all work as they are, 
    // but you might want to add brackets for 
    // clarity and fix any syntax errors.

    m_time = Time.Now();
}

void OnUserInput(/*params here*/ )
{
    // Update m_steering based on user input.
}

I think that is pretty much what you’d need to get going. Like I say, though, the syntax of Time is probably wrong (I suspect if there is a timespan class, you’d need to calculate that and then get the milisecond value; dT.miliseconds or some such) and Unity’s vectors might not support operator overloading, so you may have to adjust the formulae accordingly, but I’d be surprised if they didn’t. Also, I’m not sure what is passed for user input event, so that’ll require looking up too. One final note: even though the time and steering data is handled in the same script, this is not good OO design, but that is up to you.

Hopefully at the very least this has given you something to build upon. :slight_smile:

Makes alot of since and i got quite far but where i’m actually stuck on now is how i properly apply the equations, that are applied to orientation and position, to the transform of a Unity3D object in a proper manner.

If you’re looking for the time between frames (I think you are?) then use “Time.DeltaTime” (Unity - Scripting API: Time.deltaTime)

If you have a rigidbody attached to your game object, you have a property called rigidbody.velocity, then just apply the result of your computation to it.

The reoritation you achieve using the Quaternion interpolation, something like this:

yourObject.transform.rotation = Quaternion.Slerp (yourObject.transform.rotation, target.transform.rotation, Time.deltaTime * steering.angular);

This is equivalent to do the computation you read on the book.

thanks for the amazing input, definitely set me in the right direction. However, suppose i don’t want to use a rigidbody?

litle side note regarding something that puzzles me slightly… how do you determine the “front” of an object ?

Without rigidbody transform.Translante(velocity * Time.deltaTime);

Front transform.forward