Concretely, I am trying to implement the “make the selected box float” part of the exercise at the end of chapter 6. For the description, I understand I should do that by “applying forces”. But I find it rather quite difficult to even get the box at the desired height in the air, without strongly oscillating up and down. And keeping it in front of the player at a fix distance seems even harder.
So my question is: given a physics-enabled spatial’s current state (position, orientation, velocity, …) and a “target position”, is there some example somewhere, or maybe an API method, that computes the “ideal” force vector to move the object there (but makes sure it’s velocity reduces such that it is near zero when it gets there)?
Well for starting, you need to disable the gravity, by applying a reverse force. So assuming standard values around 10~
a little extra based on how floating your object is.
Now what greatly helps is to calculate not only in water outside water, but how much inside the water the object is, and use that as a scale for that above force. A simple approximation would be to use the boundingbox and rough estimate how much of it is below the waterplane.
This will help a bit with the bouncing, then what you can also do, is setting the dampening to a very somewhat high value, which alone will reduce the bouncing noticeably (and makes sense, as to move water out of the way a force itself is required, dampening normal ships somewhat similar.)
Note that this does not yet fix the rotation(a possible way to do this, is to do the calculation not for the object center, but for each boundingbox corner. (At least this works fine enough if your ship can be approximated as a box for swimming purposes)
Thanks, this is a very detailed answer, but unfortunately, that wasn’t what I meant by “float”. What the exercise says is, we are meant to simulate the player avatar “picking up a box”, and holding it in the air. While I assume the “usual” way to do that would be to move the box up first and then somehow hard-link the box physics body to the player physics body (compound body or something), so that the physics engine treat the both as one single (complex) object, the exercise actually ask us to use “forces” to achieve that effect.
So I was instead trying to do something more like the “half-life 2 gravity gun”. I already worked out that applying a “central” force does not achieve the desired effect, because soon the box starts to rotate randomly. So I probably have to apply 1/4 of the force to each corner.
But my main issue was with the “yoyo” effect; possibly, the “dampening” is what I need to use. My intuitive understanding (I took physics course about 25 years ago … so forgotten most of it now) is that I have to apply a force greater then gravity, so the box at all move up. And when the box is on the ground, it seems I have to overcome the “friction” too, although it does not immediately make sense to me why friction would be involved when moving up. Reducing the friction makes “taking off” easier (requires less force), so that is why I think friction has an effect here. Since I have to use a force greater then the gravity for the box to take off, it builds up momentum, and once it reaches the desired height, and I switch to using just the same force as gravity, it carries on going up and up, because of the momentum. So I have to use a bit less then gravity, so it stops and falls back again, and then I end up with the “yoyo” effect.
I have no direct experience with bullet but I’ve seen enough posts to know that where and when you apply the force (in code) is important to how well it will work for you.
It would also let me know if “force” is being used in the conventional f = ma way or some other definition. The most common mistake I see with forces is forgetting to include mass.
Ultimately, you will end up with some oscillation anyway as it’s unavoidable but if the math is right it should be unnoticeably small.
@monster said:
But my main issue was with the "yoyo" effect; possibly, the "dampening" is what I need to use. My intuitive understanding (I took physics course about 25 years ago ... so forgotten most of it now) is that I have to apply a force greater then gravity, so the box at all move up. And when the box is on the ground, it seems I have to overcome the "friction" too, although it does not immediately make sense to me why friction would be involved when moving *up*. Reducing the friction makes "taking off" easier (requires less force), so that is why I think friction has an effect here. Since I have to use a force greater then the gravity for the box to take off, it builds up momentum, and once it reaches the desired height, and I switch to using just the same force as gravity, it carries on going up and up, because of the momentum. So I have to use a bit less then gravity, so it stops and falls back again, and then I end up with the "yoyo" effect.
Yeah, that’s physics The friction I don’t know about but I use this to create a hovercraft - it works reasonably but far from perfect (and I don’t mind a little yoyo effect):
[java]
// minHeight is the current height of the object (I measure the corners and see which one is closest to the ground i.e. has the smallest height), avoid 0 at all costs
// currentDesiredHeight - Where you want to be
float agravForce =
Constants.GRAVITY
* ((currentDesiredHeight * currentDesiredHeight) / (minHeight * minHeight))
* this.vehicleMass;
// This is just a clamp so the force does not become to high, which it does when minheight approaches 0
agravForce = Math.min(agravForce, this.vehicleMass * 100 * Constants.GRAVITY) ;
// ANTI_GRAVITY_VECTOR usually points up
final Vector3f antigravity = Constants.ANTI_GRAVITY_VECTOR.mult(agravForce);
[/java]
i implemented something like that some time ago (for the character control actually) and i found that when you use the current height to improve the strenght when the height is low you get a better effect. In short, the closest your are from the ground, the strongest is the “push-back” effect. also, as you know the current speed and the gravity, you can perform each frame the “good” strenght to stabilize at the height you want.
Do some test with this idea, you should find the correct value quickly.