Floating/Flying and Movement

simple solution: have the body touch the ground as normal, but render the bottom of it blank.
Yesterday when I went to bed I came up with some ideas...here:
thesleepless wrote:
simple solution: have the body touch the ground as normal, but render the bottom of it blank.

It's not the best because when your body fall from some height it hit the ground and bounce in normal way.

We want our body to be flying some distance above the ground, so when it fall from bigger height it will smoothly slow and finally stop at the specified height. It's kind of buoyancy, but in this case it can be simplified to force = distance_to_target * some_constant just like in spring.

Quote:
cast a ray down from the player to figure out what the distance from the ground and what force to adjust by

some example(ray is red, and resulting force is violet):
Image

It could be good, but ray is very thin line, so we have problem with cases like that:
Image
Ray won't detect a hole in ground.

Of course we can cast multiple rays, and each of them will add its own upward force:
Image
Of course added force should be proportional to distance between ray_starting_point and point_of_intersection_with_ground.


Quote:
using a sensor box to detect ground; if there is ground add some upwards force

We create sensor under our body, and when it detect a collision we add upward force:
Image
Here yellow rectangle is the sensor.

In this method you can add constant force when you detect ground, or calculate distance based on contact points and then calculate force(like in ray cast method). But you have to filter contact points in case you got too many of them(when ground is made of many shapes)

If you want to avoid this calculations you can create many overlapping sensors, and then when each of them detects ground you add some force(it can be the same for all sensors or different):
Image
You can replace this with using many different-lenght rays, but then you still have hole-in-ground problem(also when using rays you really need only one because you can easily calculate distance).

Finally you can create something like hovercraft with air turbines:
Image
As you can see when ground is near the turbine you get more force, in case of a hole just under the turbine you won't get full force(as in previous example) because the smallest sensor won't detect anything.
I think that this is the most realistic, and can be easily done only with sensors(or if you want to create many rays in different directions with different lenghts).

Quote:
use a mouse joint to try to pin the player to a goal point

Here you have the problem of choosing this goal point - you still need to use raycasting or something to determine where ground is.


I hope this help somebody and of course please correct me if something with my thinking is wrong(this methods were not tested).

post was edited to expand some places
I think your multiple sensors idea is pretty good, and rather than your trapezoidal shapes, concentrically placed circles would work great. It would also allow for having the smaller ones exert a stronger force, allowing some pretty fine tuning of the forces.

This sensor method is really interesting because it allows a lot more realistic behavior for simulating something like hovercraft jets, without needing some sort of complicated code to apply custom forces, since you can just use the separation and penetration values on the sensor shapes, given by box2d to apply forces. At least I think it's possible to do this, and I have not tested it.
dc443 wrote:
you can just use the separation and penetration values on the sensor shapes

I didn't know about this...I thought that you must get contact point data and calculate this by yourself.

Using that you can only create one sensor and have a function which calculate force based on penetration(it can be non-linear if you want very big force near the jet).

But creating many sensors gives more control...also too many of them will slow down simulation...so the best should be something between - few sensors and using penetration values...


Jets can be very useful, here you have a horizontal one, which gives you a boost when near the wall:
Image
this wasn't clarified and I think it should be:
irresistible force wrote:
I have been trying out method No.2 using TestSegment/ApplyForce and it is working out pretty well. The biggest problem is that since the character doesn't actually collide with the world to walk on it, you have to do all those things that Box2D would normally do for you, ie:
-friction
-transfer the weight/impact of the character to what he is standing on
-slide down slopes
-move the character when what he is standing on moves

You want your character to fly above the ground, so its obvious that friction should be zero.
And with transfering the weight - you simply apply the same force to the ground which is applied to the character(if you are raycasting it can be at point of intersection with ground*). and of course when body is moving very fast it can collide - box2d will solve it in normal way.

*what happen when due to rounding error force will be applied outside the body? can you specify any point in space where you are applying force?
max wrote:
can you specify any point in space where you are applying force?


Yes.
max wrote:
You want your character to fly above the ground, so its obvious that friction should be zero.
And with transfering the weight - you simply apply the same force to the ground which is applied to the character(if you are raycasting it can be at point of intersection with ground*). and of course when body is moving very fast it can collide - box2d will solve it in normal way.

If you are doing this for something like a hovercraft which doesnt actually touch the ground, then zero friction might be ok, but for a human type character it's not. You will have to look at what the ground underneath is, and decide how quickly your character is able to speed up or slow down.
For transferring the weight, applying the same force to the ground is correct. Bear in mind that your character uses forces to move and 'brake', so you would need to transfer any of these sideways forces involved too.
The trickiest part is making the character slide down slopes correctly. Since the main shape of the character doesn't touch anything, you need to look at the slope of the ground being stood on, and add a sideways force to the character depending on the slope angle and friction of the surface.

Of course if you are making a hovercraft none of this matters, but I didn't get that impression from the original posters question.
unless it's a space game, you'll probably want to apply some drag force also otherwise without friction from touching the ground your object will keep moving indefinitely

just apply the inverse of the object's movement multiplied by the density of the medium (air density = ~0.00125 at sea level) multiplied by the drag coefficient which depends on the aerodynamicness of your object. some examples can be found: http://en.wikipedia.org/wiki/Automobile ... oefficient

DRAG = -VELOCITY * MEDIUM_DENSITY * DRAG_COEFFICIENT

Original thread: https://box2d.org/forum/viewtopic.php?t=1053