x


Preserving Velocity during rotations

Essentially, what I want is when the Rigidbody turns (performed by rigidbody.AddRelativeTorque) it will correct the velocity so that the velocity is relative to the transform.forward. So if I have a Global Velocity of Vector3(0.0f, 0.0f, 100.0f) with a transform.forward of Vector3(0.0f, 0.0f, 1.0f) which then changes to Vector3(1.0f, 0.0f, 0.0f) which would mean the Velocity would be updated to be Vector3(100.0f, 0.0f, 0.0f).

I've been using this to do it so far, simply using the global transform.forward * Velocity.magnitude:

rigidbody.velocity = new Vector3(transform.forward.x * rigidbody.velocity.magnitude,
                                 transform.forward.y * rigidbody.velocity.magnitude,
                                 transform.forward.z * rigidbody.velocity.magnitude);

This works well except if there is a negative velocity involved, for instance a Velocity of Vector3(0.0f, 0.0f, -100.0f) when transformed with this process will loose the signed value as the magnitude is absolute so instead of (using the same case as before forward = Vector3(1.0f, 0.0f, 0.0f)) the Velocity being Vector3(-100.f, 0.0f, 0.0f) it's Vector3(100.0f, 0.0f, 0.0f). There's probably a simple fix for this but I just can't see it today...

more ▼

asked Nov 11 '10 at 04:32 AM

Some Programmer Type Person gravatar image

Some Programmer Type Person
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You probably want to have an if, then -

if ( Vector3.Angle( rigidbody.velocity, transform.forward ) > 90 ) { // if thing moving backwards
  // multiply by negative rb.vel.magnitude
} else {
  // use current code
more ▼

answered Nov 11 '10 at 05:40 AM

Loius gravatar image

Loius
10.6k 1 11 41

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2159
x1787
x318

asked: Nov 11 '10 at 04:32 AM

Seen: 855 times

Last Updated: Nov 11 '10 at 04:32 AM