AddForceAtPosition... referencing local direction

Hi all… I’m spankin’ new at all this… Unity as well as JS, so I sure could use a little hand holding from a kind soul. (I know Second Life and lsl, so I’m not clueless… just out of my element :slight_smile:

I think what I need to do is simple, but I don’t know how to tie the pieces together.

I need to apply a force on an object relative to its direction vector. Let’s say the force is ALWAYS (0,-1,0) referenced to its direction of travel. It’s very much like drag!!! I need to use AddForceAtPosition to apply the force as I will have a need to specify exactly where the force is applied.
Lets say this position is at (0,1,0) in LOCAL space.

So here I am confused… I have a reference to direction, position in local space, and functions that (I think) only know world space.

Sounds like I have some converting I have to do… but honestly, I am lost at where to start.

I’d be forever grateful if someone could offer up systactically correct code that I could cut n paste that demonstrates the management of this problem…
(I hate asking for a code snippit, but as JS is new for me as well… I’d have a major case of FAIL just due to syntax)

Thanks ever so much!
Ryder

So… You want the rigidbody to basically move in the direction it’s facing?

No :slight_smile: Far more complex than that, though it will do that if things are “right”.

Here is an example… if I were, in real life, to make an arrow, and stupidly put the feathers by the arrow head and shot it, it would not be pointing the way it was traveling for long. Aerodynamic forces would actually spin the arrow around and cause it to point exactly backward from it’s direction of flight.

What I want do do is apply forces to a rigidbody regardless of its orientation… like drag does, but that requires that I know its direction vector. Also, I need to apply this force to a specific local location… using the aformentioned function, which I believe uses world space.

So the task is complex, but the code should be simple.

If I get you right (And forgive my inability to comprehend… :sweat_smile: ), you want to apply force that stays in a global direction, but from a local position?

Let me offer a sketch of what I think needs to happen to the rigid body.

1 - determine the vector that represents direction of travel.

2 - create a new vector that is in the opposite direction of vector 1, to a predetermied location on the rigid body (another topic, but basically a place that I determine represents the objects “center of pressure”). The degree of force is also a separate topic we need not concern ourselves with at the moment.

3 - convert this new vector to world space

4 - apply this vector to the rigid body with AddForceAtPosition

Hopefully my approach is sound, and the code is not too daunting (should be about 4 lines, yes?)

Thank you!!!

Ryder

Hi Flynn… thanks for hanging in there…

Not quite. A force that constantly changes depending on the direction of travel of the rigid body… acting in precisely the opposite direction of the direction of travel.

This then needs to be applied back to the object to a pre-determined point (I’ll take care of that), which I am given to understand must be done with a function requiring a world frame of reference… so it would need to be converted first.

OH! I see. And you want the specified point to be in local space, right? Oh, and do you want the force to be exactly equal to the speed of the rigidbody, or a specified force amount, just in the opposite direction?

Yes, precisely.

Here is an interesting link on how NASA finds the center of pressure on a simple aerodynamic model…

http://exploration.grc.nasa.gov/education/rocket/Images/rktcp.gif

Cp is the point I need to push… I will figure out how to come up with it…

But the issue is to create the proper vector to push at that point, and that comes from the air hitting the vehicle (which really has nothing to do with where it is pointed, except when things are aerodynamically stable)

Allright, all I need to know is if you want the force applied to be equal to the velocity of the rigidbody, or equal to a specified value.

Am I correct that Rigidbody.angularVelocity is the velocity vector of the body, in world reference?

How do I invert this… so that is points the other way… is it as simple as multiplying it by a negative number?

MyNewSmallerVector = Rigidbody.angularVelocity * -.25

???

Thank you :slight_smile:

The force can be any value… but for sake of discussion… make it 0.25 of the velocity.

Angular Velocity is the rotation speed of the rigidbody.

Per request, done with Rigidbody.AddForceAtPosition:

rigidbody.AddForceAtPosition(rigidbody.velocity * -0.25,transform.TransformPoint(localpoint));

And yes, it is as simple as multiplying by a negative number.

Setting 0.25 to 100 does not even stop the velocity for some reason.

Wow… one line of code :slight_smile: That rocks.

Ok… let’s see what we see… I will try it out now.

THANK YOU!

Interesting gift #1 for your efforts: http://www.youtube.com/watch?v=lBUUOJpFg9Y

Thanks :slight_smile:

I’m here and trolling, so do write back!

Again, my apologies for my lack of response, and overdose of questioning.

Guess what… it works PERFECTLY!

I changed variables for the center of pressure and the center of mass for the object, and it became unstable when the Cp was forward of the Cg… and conversely became stable when the Cp was kept behind the center of mass.

And your questions were necessary… because I really had a hard time explaining it :slight_smile:

I think that this is a really big deal… one line of code added a fundimental aerodynamic component to Unity not present in its physics engine.

That, my friend, deserves a “woot!”

I learned a LOT tonight.

Again, my thanks!

Ryder

phew

No problem, and thanks for the complements, and the woot!

One final thing, if I may…

How can I convert a velocity to a simple “speed”.

I need a float that describes the magnitude (minus the direction) of the rigidbody speed. Not sure how to do that. Can one simply add the vector components? Multiply them?

(This vector stuff hurts my brain)

You mean like instead of multiplying speed by -0.25?

Basically, you set the amount of frce applied, but it sill gets applied in the opposite direction as the rigidbodie’s velcity, right?

Well, this is a separate issue… but yes, I am calculating a quantity as opposed to a fixed .25. That was just for testing. But as you can imagine, pressure increases with speed.

So from the velocity (direction + magnatude), I really need to simply get the magnatude, and I’m not sure if there is a function to do that.