What is the best way to calculate the force to use for applyimpulse in order to impulse in a given direction?

Hi everyone,

I’m fairly new to 3D and the math that are used to calculate certain type of behaviors.
For example I’m trying to find a way to use applyImpulse with a force (first param) that will throw a sphere in the direction of a goal (a mesh) knowing that my camera is locked (using camera.target) on this goal.

I also want to be able to control the “power” of the impulse (don’t know what is the dedicated physics word). For that matter I use the time between space bar press and release to get a number that I will use as a multiplier when I will be able to calculate the force mentionned above.

Currently I think I know which data to start with (maybe camera.position and camera.getTarget()) but, as I’m really weak in math, I can’t figure out what to do with those data.

If someone has an idea or an hint to share that would be really helpful :blush:

I’m not sure it will help but here is the current state of my project of a basket ball minigame for which I want to implement the calculation explained above Babylon Template

My question in few words : How to calculate the force (first parameter of applyImpulse) in order to throw an object towards another ? ( and how to apply a variable “power” to the resulting vector )

1 Like

Hi PB! The “direction” used in physics applyImpulse (and applyForce?)… can be gotten by subtracting starting position… from target position (or vise-versa).

So, in your case, forceVector = camera.position.clone().subtract(camera.target) should give you something useable… or maybe forceVector = camera.getPosition().subtract(camera.target) … something similar to that, or switch the values around… or use forceVector.negate() as needed.

The result of those subtractions of two vector3’s… is a vector3. Vector3’s have a handy scale() method that is an amplifier or attenutator (up-scaler or down-scaler)… and that is your AMOUNT of power that you will be impulsing along the force vector.

Sometimes… the scale of the direction-type vector3… is called its magnitude. Feel free to read about magnitudes of direction vectors on the web.

There is also a vector3.normalize() method that could be read-about, too. As best I understand it, normalize() makes all three values of the vector3… as tiny as possible… and still be pointing in the same direction. SO… it is a de-magnituder, right? You might want to start with de-magnituded force vectors… just before you do forceVector = forceVector.scale(somePowerLevel)… or maybe forceVector.scaleInPlace(somePowerLevel).

Do you know about scale() vs. scaleInPlace() vs. scaleToRef() stuff? All just different ways of up/down-scaling a vector3. The same options exist for that subtraction mentioned earlier… ie. subtractToRef(), subtractInPlace(), etc. They exist for other math stuff, too.

All in all, I would FIRST do the subtraction of target position from source position, or vise-versa. Then do thatResult.normalize(). Then do thatResult.scale(yourPowerLevel)… the magnitude adjuster. That should give you all the tools you need. I hope this helps. Holler if ya got problems… we have some applyImpulse demo playgrounds nearby.


Point of Order Sir - normalize() makes the vector into a unit vector in the same direction.

That is given a Vector3 V = BABYLON.Vector3(a, b, c) then

V.normalize() changes the value of V to (x, y, z) where x2 + y2 + z2 = 1

Note if you want to retain V as (a, b, c) you need to do

W = V.clone();

It is then true that V = W.scale(V.length()) where V.length() gives the magnitude of V

in this sense

is true.


First thanks a lot for all the time you took to write me this really detailed and comprehensive answer. Really great people and community here :smile:

Yes I think I’ve got all the tools I need to start ! I was just droping by to thank you, I’ll let you know if I made it work right for my needs :slightly_smiling_face:

Thanks a lot again !

1 Like