Gravity on a Mesh Terrain

Blitz3D Forums/Blitz3D Beginners Area/Gravity on a Mesh Terrain

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.


Unfortunately it's still shaking abit when you stand still on an almost-flat slope... Is there a better way to simulate 1st person player gravity and air acceleration?



Here's a simplified (in that you'll have to build your original control scheme/mouselook back in... curs keys only from me!) model that won't shake but will simulate gravitational acceleration along the y axis (toddle off the edge to see)...

Const TERMINAL_Y_VELOCITY#=2.0
Const TERRAIN_SIZE=16
Const TERRAIN_SCALE=8

Const PLAYER_GEOMETRY=1
Const LEVEL_GEOMETRY=2

Graphics3D 800,600,0,2
SetBuffer BackBuffer()
cam=CreateCamera(): PositionEntity cam,(TERRAIN_SIZE*.5)-1,10,-TERRAIN_SIZE
light=CreateLight(1,cam): PositionEntity light,40,40,-40
worldPiv=CreatePivot(): PositionEntity worldPiv,(TERRAIN_SIZE*.5),0,(TERRAIN_SIZE*.5)

terrain=CreateTerrain(TERRAIN_SIZE)
ScaleEntity terrain,TERRAIN_SCALE,TERRAIN_SCALE/2,TERRAIN_SCALE
TerrainShading terrain,True
For x=0 To TERRAIN_SIZE-1
For z=0 To TERRAIN_SIZE-1
	ModifyTerrain terrain,x,z,Rnd(1,10)
Next
Next

terrainTex=CreateTexture(64,64)
Color 50,100,50
Rect 0,0,64,64,True
Color 70,140,70
For x=0 To 127
	Plot Rand(0,63),Rand(0,63)
Next
CopyRect 0,0,64,64,0,0,BackBuffer(),TextureBuffer(terrainTex)
EntityTexture terrain,terrainTex,0,0



Type player
	Field model
	Field gravityY#
	Field incGravity
End Type
currentPlayer.player=Null

currentPlayer=New player
currentPlayer\model=CreateCube()
currentPlayer\gravityY=0.0
currentPlayer\incGravity=True
PositionEntity currentPlayer\model,(TERRAIN_SIZE/2)*TERRAIN_SCALE,50,(TERRAIN_SIZE/2)*TERRAIN_SCALE
EntityType currentPlayer\model,PLAYER_GEOMETRY : EntityType terrain,LEVEL_GEOMETRY

Collisions PLAYER_GEOMETRY,LEVEL_GEOMETRY,2,2




While Not KeyHit(1)
	
	If KeyDown(203) Then TurnEntity currentPlayer\model,0, 1,0
	If KeyDown(205) Then TurnEntity currentPlayer\model,0,-1,0
	If KeyDown(200) And currentPlayer\incGravity=False Then MoveEntity currentPlayer\model,0,0,.1
	If KeyDown(208) And currentPlayer\incGravity=False Then MoveEntity currentPlayer\model,0,0,-.1
	
	
	
	If currentPlayer\incGravity=True
		currentPlayer\gravityY#=currentPlayer\gravityY#+.02
		If currentPlayer\gravityY#>TERMINAL_Y_VELOCITY Then currentPlayer\gravityY#=TERMINAL_Y_VELOCITY
	EndIf	
	TranslateEntity currentPlayer\model,0,-currentPlayer\gravityY#-.1,0 : UpdateWorld
	If EntityY(currentPlayer\model)<-50 Then PositionEntity currentPlayer\model,(TERRAIN_SIZE/2)*TERRAIN_SCALE,50,(TERRAIN_SIZE/2)*TERRAIN_SCALE
	PointEntity cam,currentPlayer\model: RenderWorld :	Flip
	
	For currentCollision=0 To CountCollisions(currentPlayer\model)
		If currentCollision>0
			If GetEntityType(CollisionEntity(currentPlayer\model,currentCollision))=LEVEL_GEOMETRY
				currentPlayer\gravityY=0
				currentPlayer\incGravity=False
			EndIf
		Else
				currentPlayer\incGravity=True
		EndIf
	Next
Wend


Now if you want inertia along with that, that's another system and should be handled separately I think. I notice in your original that you apply air acceleration to cx and cz... I would restrict it to affecting y.

Also, if you want the player to slide only at certain (extreme) inclines then that will involve a 'pick' downwards, interpreting the vector of the 'picked' geometry and setting the collision rules accordingly (sliding or not). I have only concentrated on the shaking issue.