Managing Player fall and gravity.

Blitz3D Forums/Blitz3D Beginners Area/Managing Player fall and gravity.

RemiD(Posted 2011) [#1]
Hi, :)

I have made a small demo of tomb raider like controls and gameplay in the past and it was working well, watch this video :
http://www.youtube.com/watch?v=_dk0hrtgquo
The animations are bad, i know...

My problem is that the code i'm using is working because the terrain is quite flat.

Here is the concept of the code :

Each loop :
->The program launches a linepick below the player feet so that i know if the player is on the ground or not.
->If the player is on the ground, the player can move, jump.
->If the player is not on the ground, and is not grabbing a ledge, the player falls and the gravity is activated.

The problem with my code is that if the terrain has many irregularities, the player keeps falling when moving.
I can't really use a variable to check if player is in jump mode or not before activating the gravity because i also want player to fall when he is moving away from a platform.

Do you have any idea on how to proceed ?

Thanks,


stayne(Posted 2011) [#2]
Not sure if it matters but is the start point of your linepick somewhere around the middle of your player?


RemiD(Posted 2011) [#3]
Hi stayne,

The linepick starts at player feet Y# + 10 and goes to -100 on the Y axis.

I don't have a problem with the linepick.

My problem is to know when to enable the gravity, knowing that player can jump and fall or simply fall from a platform.

Let me give you two examples :

If i enable the gravity each time the Y# coordinate of player feet is higher than the Y# coordinate of the picked ground (terrain or platform), the gravity is working properly, but you understand that the player is always falling when the picked ground is going downward.

If i enable the gravity only when player press the jump key, i can easily position player on the Y# coordinate of the picked ground (terrain or platform) when player is not jumping, but imagine if player moves away from a platform without jumping, he will stay in the air because the gravity is not activated.

That's my problem. I don't know how to activate gravity so the player can fall but without making him fall when he is walking on a ground which is going downard.

I hope my explanations are more clear this time.

Thanks for your ideas :)

Last edited 2011


Yasha(Posted 2011) [#4]
Have you experimented with, or taken a look at, the collision system?

This is pretty much what B3D's collisions, especially sliding collisions, were built for. Collision detection checks when one object is intersecting with another (well, not quite - for speed, one of them always has to be a sphere) and will interrupt the entity movement commands so that they can't pass through one another. If the second object (collision is one-way - there are tricks for two-way collision which you can learn later) is slanted relative to the first, it's also possible to choose "sliding" collisions, that move the first object along the surface of the second without penetrating it.

So the classic way to handle gravity in simple games is actually to never turn it off; you can simply have your player get move downward by a constant amount each frame, and with collision against the level turned on, it won't move through it.

Sliding collision is also handy; you can simply move your character in X and Z, and if there's a hill, it will walk up the hill as it slides up the surface. You can prevent gravity from dragging your character down the side of the hill again with a special option designed to help with that.

Then deciding when to jump becomes a matter of using the collision test functions to see if a collision happened between your player and the ground; if it did, that's your OK flag for jumping and movement. As long as gravity is applied before the collision check, the player will have moved down and be contacting the ground that frame (or maybe they ran off the edge too fast, and count as jumping!).

Collisions are powered by UpdateWorld, so you may need to rearrange things a bit, but they're designed to be very simple to use; they're Blitz3D's prebuilt system for doing what you've been trying to accomplish with LinePick, and will be much faster and more robust for it.


RifRaf(Posted 2011) [#5]
What I usually do is have two gravity values, one is applied when the entity is colliding with the ground, the other is applied when the player is not in touch with the ground.

The ground enabled gravity value is very low, while the in air gravity would be much greater

You can have a counter that increments while the entity is not in touch with the ground to time how long its in the air, and use that as you see fit. I have used it to increase the air gravity the longer the entity is in the air up to a max gravity value.

Using these two values can help with players going slower up hills as well.

Last edited 2011


RemiD(Posted 2011) [#6]
Thanks for your suggestions, i'm going to code a small demo with a sphere collider instead of using linepick and see if it works better.


RemiD(Posted 2011) [#7]
Just to let you know that i have resolved my problem by using a sphere collider rather than a linepick (thanks Yasha :)) and by applying 2 kinds of movements to the character :

First, i check if the character collider is colliding with the level collider (terrain, buildings, rocks)

If the character collider is colliding with the level collider,
->The character can move according to the input keys (forward, backward, left step, right step)
->Then the character moves downward with a constant gravity of a length of half the previous vector (Thanks RifRaf :))
->Then the player is able to jump.

Else the character is falling
Depending if Player has jumped or not, a force is added or not to the movement vectors
Finally i calculate an increasing gravity speed to pull the character toward the ground.

I avoid the previous jerky movements i had, by always moving the character in a direction when he is on the ground or when he is falling or when he is jumping.

If it can inspire someone :)


RemiD(Posted 2015) [#8]

because i also want player to fall when he is moving away from a platform.


Ok i answer to past me :
one way to do this is to first check if the character is OnAir or OnGround, (with a linepick from the middle of the character to down)
If the character feet is near enough the picked point
Position the character feet at this picked position
YVelocity = 0
Player is OnGround
Player can use the controls to turn move the character or to jump
Turn Move the character
Animate the character
Else if the character feet is too far the picked point
Player is OnAir
Calculate the YVelocity of the character (YVelocity - GravityForce)
Translate the character with the YVelocity
Animate the character

Also please note that my problem with linepick on terrain was due to what seem to be a bug see : http://www.blitzbasic.com/Community/posts.php?topic=101836


Matty(Posted 2015) [#9]
Even though technically it should work (but obviously it doesn't) I would never rely on a line pick with a terrain and would always use the terrainy function (as you mentioned in the bug report) simply because a simple calculation as terrainy does (interpolate the height inside a triangle) is always going to be an order of magnitude faster than calculating the intersection point between a ray and a triangle.


RemiD(Posted 2015) [#10]

a simple calculation as terrainy does (interpolate the height inside a triangle) is always going to be an order of magnitude faster than calculating the intersection point between a ray and a triangle.


Maybe, but if you use irregular shapes for obstacles (buildings, rocks, trees, containers, devices), there is no way to know if an obstacle is between the character and the terrain, so the obstacles must be considered before considering the terrain.
A way to speed up the calculation is to use low tris pickables and also to first calculate which obstacles are near enough the character, and only set these obstacles as pickable. In this way, only the near enough obstacles and the terrain will be considered for the calculation.


RemiD(Posted 2017) [#11]
5 years later and still the same problem, caused by a bug of Blitz3d : camerapick/linepick sometimes fails on a blitz3d terrain... I have found another workaround : have a transparent (alpha 0.0) plane at the level of the ground and set it as pickable instead of the terrain) but this works only for flat enough terrains.


Kryzon(Posted 2017) [#12]
Use the X and Z coordinates of the pick with TerrainHeight.

http://www.blitzbasic.com/b3ddocs/command.php?name=TerrainHeight&ref=3d_cat


RemiD(Posted 2017) [#13]
not possible if the linepick/camerapick is not from top to down (this is the case for the video game i am working on, since i use an isometric view), but yes, this is a workaround if the linepick/camerapick is from top to down, i already mentioned it here : http://www.blitzbasic.com/Community/posts.php?topic=101836 (#2)

But having an invisible plane (entityalpha/brushalpha 0.0) at the level of the ground if the terrain is rather flat, (no hills/deeps), and picking the plane instead of the terrain is a workaround which works well.

Another workaround that i am looking for is a way to get the mesh produced by the ROAM algorithm and use this as the pickable (it must exist somewhere since it is rendered), or code my own terrain ROAM algorithm, or create a high details version of the terrain (with one vertex each 1x1zunit and 2 triangles for each cell) and then use a triangles reduction algorithm such as : http://www.blitzbasic.com/codearcs/codearcs.php?code=1285
to produce a low details version that i can use as the pickable)


gpete(Posted 2017) [#14]
far be it from me to offer solutions to Remi- ;-) .. but I always add a low value "downward" translate to the player..forward movement is greater to allow climbing ramps etc....


BlitzMan(Posted 2017) [#15]
.