Applying Force 'forward'?

Sorry if this is a too simple question but I was wondering how to apply a force so it always applies it forward (relative to the collision object it is being applied to). Basically allowing for movements like steering where it will always movie in the direction you point it. (I’ve read the official tutorials and pages and nothing seemed to help much).

What do you have so far?

If you have a direction, then multiply the direction with the magnitude needed to move the object. Do this each frame, while also keeping the direction up-to-date.

local force = direction * magnitude"#collisionobject", "apply_force", {force = force, position = go.get_world_position()})

And, depending on where the position is, it will apply rotation to the object.
E.g. if it is off-center to the object

How would I get the direction since the direction is not towards any specific place , just generally forward?

To get a direction from one point to another:

local from = ... -- vmath.vector3
local to = ... -- vmath.vector3
local dir = to - from -- not normalized. i.e. length != 0
dir = vmath.normalize(dir) -- length of vector is now 1.0

I’m not sure if you understand? There isn’t any ‘to’. I just want the object to have the force applied in the direction it is pointing, like a car or something that moves depending on its direction. There isn’t a specific location it is traveling towards. Sorry if I’m not understanding something?

The question you’re asking comes up frequently, together with “How do I move a game object towards a point?”.

I think we’ll have to create to examples for these two questions. I’ll put something together!

1 Like

Sorry, I got stuck on the “force” part of the question.

Well, it’s up to you to decide what “forward” means in your scenario.
It differs from case to case.

E.g. you can choose (1,0,0) as forward.
If you also wan to account for rotation of your object, you need to rotate it as well.
E.g. something like that should work

local r = go.get_rotation()
local forward = r * vmath.vector3(1,0,0)
1 Like

I’ve tried this bellow but I get the error (quat expected, got userdata) for the second line. (don’t worry about thrust, that is just to control how much force is applied)

local r = go.get_rotation()
local forward = r * vmath.vector3(0,thrust,0)"#collisionobject", "apply_force", {force = forward, position = go.get_world_position()})

change this to
local forward = vmath.rotate(r, vmath.vector3(0,thrust,0))


New examples: