Direction of motion vs rotation

This is total n00b stuff… I’m sure it’s been discussed, but I haven’t found it yet… Sorry.

Trying to establish ‘front’ from ‘back’ of a rigid body–in order to know if it is traveling forward or backward

of course the most obvious action for me was–
rigidbody.velocity.x

but that is global, not local–is there an easy way to get local velocity relative to the object?
second try…
var angle = Vector3.Angle(transform.rotation, rigidbody.velocity);

but of course, rotation is in quaternion form, and I haven’t figured out how to generate a vector from rotation yet. I need a ‘I am pointed this way’ vector. Then compare to see if the angle is greater or less than 90 degrees to the direction of motion.

Obviously the faster the calculation the better, so local velocity is probably out unless I just need to call it…

var relativeVelocity = transform.TransformDirection(rigidbody.velocity);

…though I guess in this case a InverseTransformDirection should be used. You want to get velocity from world space into local space.

haha, thanks… I was thinking… local to world…
DOH!

Oops, you got me there.

Hey Brian. Ive just started to use forces as I hope to use the physics engine as part of my scene. Is this similar to what you are doing?

var forwardspeed=100;
var sidespeed=100;
function Update () {
    if  (Input.GetAxis ("Vertical")) {
       rigidbody.AddRelativeForce (0, 0, (forwardspeed));
    }
    if  (Input.GetAxis ("Horizontal")) {
       rigidbody.AddRelativeForce ((sidespeed), 0, 0);
    }
}

I find that using the w or s keys both propell the vessel forward…not forwward or backward. Id be interested to see how you are going to implement your fix? Or even if we’re addressing the same issue…

On another note, my understanding is that the above method is more ideal for physics scenes. So what if we have 1 translation based on force, as well as another based on manipulating the transform? I guess its just going to be messy behaviours?

And rotating has to be a transform manipulation, doesnt it?
To mix or not to mix…
Cheers
AC

To be very performance minded you can test if the velocity points in the same direction as “forward” with:

if (Vector3.Dot(rigidbody.velocity, transform.forward ) > 0.0) {
   // forward
} else {
   // 90 degrees or back
}

The advantage of using “Dot” is that it only uses three multiplications and two additions. “Angle” on the other hand, probably also has to do two square roots and an inverse cosine. Nasty stuff.

@targos

input axis gives a value between -1 and 1. w goes towards 1 and s towards -1 in a standard wasd. you’re saying if the key is pressed, add a force of forwardspeed to my z. in your case, forwardspeed is always positive 100 thus it moves forward regardless of which key. use forwardspeed * input axis and it will react to (w) forwards and (s) backwards. because it’s a relative force, if you’re already moving forward the s key will slow you down before moving you backward. also you might want to add forces in fixed update or they’ll react differently on different machines due to frame rate.
anyway, oops! sorry!

That’s because you’re just adding “forwardspeed” to the Z axis of the object whenever the vertical input axis is used, whether it’s positive or negative. Maybe use something like “rigidbody.AddRelativeForce (0, 0, (forwardspeed * Input.GetAxis(“Vertical”)))” instead. (And the same thing for horizontal, of course.)

Nope, use AddRelativeTorque for that (or AddTorque, depending on what you’re doing).

Do not mix. Ever. :wink: Oh, and it’s better to do physics stuff in FixedUpdate rather than Update.

–Eric

Here is what I have done–if you followed my other post…

steer amount is almost the same as in the car version,
turnforce is based on
current velocity in km/h in the forward direction, 7000 made the function balance, don’t ask…

Basically, because of the loop structure, it takes the momentum away, turns the ship, then reapplies the same force.

I needed front from back direction, because obviously, if i subtract negative force, it goes faster :slight_smile:

Does that answer your question Targos?
this is probably a terrible way to do what seemed very simple, but it works like a charm.

I haven’t tried rotating the velocity component yet-- is that preferable? Seems like it should be.

Also, as you can see, I mixed. Maybe I’ll play with torque for turning, but i never feel completely in control while using torque.
Once static friction is broken, all hell brakes loose.
If it means making the controls feel right, fake it.

    //define force equation to preserve momentum
    var forcebalance =Mathf.Abs(steerAmount) * 7000 * turnforce;
    
    //remove speed
    rigidbody.AddRelativeForce (-forcebalance,0,0);
    
    //turn the sled
    transform.Rotate(Vector3.up, steer);
    
    //add speed back
    rigidbody.AddRelativeForce (forcebalance,0,0);