How to calculate a perfect counter force

am trying to figure out how to apply a counter force to an existing force to completely cancel it, like applying a counter force to the gravity force for an object to stay midair, how do i do that ?

the same amount of your gravity just in the opposite direction?

gravity is just an example, and how would you apply that “same amount”

Depends on how you apply your current forces. Are we talking about a rigidbody or a characterbody?

rigid body

Dont know if it works, but you can try to use the linear_velocity-property

and how would i do that ?
i can’t simply set the linear velocity to 0 as am only looking to cancel certain forces applied to the body, not all, which in my current situation am trying to cancel a specific percentage of the slide velocity in a car

apply_central_force? In your gravity example you could lock the Y axis, or set linear_velocity.y = 0 each frame.

2 Likes

as i just said, i cant simply set the velocity to 0, i wanna apply a counter force, but i dont know how much the force should be, in the gravity example, i tried applying -linear_velocity as a counter force, it didn’t work, then after looking it up in google, i tried -lineaer_velocity / delta, which was close but not perfect

am doing this with apply_force in a physics process btw

overriding _integrate_forces and using it’s state variable should be more accurate; you will have to know what force to cancel out, again with gravity +9.8m/s to cancel out the usual -9.8m/s. Once a force is applied it’s a enigmatic vector3, it can’t be decomposed back into forces on it’s own.

is there a way i could do this with linear velocity ? since gravity is just an example

I wouldn’t recommend editing linear velocity directly, but yes using gravity as an example you could subtract from linear_velocity

linear_velocity -= gravity * delta

no no no, what i mean is since gravity is just an example, i need a way to create a counter force for it using linear velocity only or something similair

if the “it” in this statement is gravity then my sample is a way to do that using linear_velocity. The “counter force” is negative gravity.

If you do not have a specific force in mind, that you can track, then I can’t help much more.

then lets forget the gravity, lets say i wanna create a counter force that stops an object from going left or right, and said left or right can change since the object can spin so i cant just set the z velocity to 0 or something

linear_velocity does not affect angular_velocity, spinning will be a separate force

i dont want it to stop from spinning, i want it to cancel any force that would move the object left or right, and said left or right will change when the object spins

Keep spinning, No movement? Your example should work, linear_velocity.z = 0. Again angular_velocity controls rotation, if that’s kept the same your rigid body will keep it’s spin.

linear_velocity.z = 0 will stop the movement according to the world’s left and right, not the object’s local left and right

I suppose you would take the object’s right vector transform.basis.x then invert it? Vector3.ONE - right_vector or would that be Vector3.ONE / right_vector? Then you could multiply that with your linear_velocity to cancel out the axis.

var right_vector = transform.basis.x
var right_plane_mask = Vecotr3.ONE - right_vector

linear_velocity *= right_plane_mask

Though I’m sure a spinning object, without any rotational locks, will look quite strange. What shape do you hope to apply this on? What is your end goal in general?