Simple Game Physics Question

Blitz3D Forums/Blitz3D Programming/Simple Game Physics Question

Ok, well here's what I put together.


; ------------------------------------------------------------------
; GameCore -- support@...
; ------------------------------------------------------------------
; The basics of a frame-limited Blitz 3D game, ready to rock
; Adapted from Mark Sibly's code
; ------------------------------------------------------------------

; ------------------------------------------------------------------
;	Game's frames-per-second setting
; ------------------------------------------------------------------

Global gameFPS = 60

; ------------------------------------------------------------------
;	Open 3D display mode
; ------------------------------------------------------------------

Graphics3D(800,600,32)
MoveMouse(20,20)
HidePointer()

; ------------------------------------------------------------------
; Camera setup
; ------------------------------------------------------------------

Global Cam = CreateCamera()
CameraViewport(Cam,0,0,GraphicsWidth(),GraphicsHeight())
PositionEntity(Cam,0,0,-50)
CameraFogMode(Cam,1)
CameraFogRange(Cam,100,1000)

Light = CreateLight()
LightRange(Light,5000)

; ------------------------------------------------------------------
; General setup
; ------------------------------------------------------------------

SeedRnd(MilliSecs())


;Since I nixed the includes to handle my player data, I'll just
;pop this in here for ease of testing.

Type Entity
Field Entity
End Type

; ------------------------------------------------------------------
; Global setup
; ------------------------------------------------------------------

;Collision data
;For the simplicity of this test, we only need two.

Const CT_MAP = 1
Const CT_ENTITY = 2

Collisions(CT_ENTITY,CT_MAP,2,3)

Global Player.Entity = CreateEntity(0,0,0)

CreateMap()

; ------------------------------------------------------------------
;	Frame limiting code setup
; ------------------------------------------------------------------

framePeriod = 1000 / gameFPS
frameTime = MilliSecs () - framePeriod

Repeat

	; --------------------------------------------------------------
	; Frame limiting
	; --------------------------------------------------------------

	Repeat
		frameElapsed = MilliSecs () - frameTime
	Until frameElapsed

	frameTicks = frameElapsed / framePeriod
	
	frameTween# = Float(frameElapsed Mod framePeriod) / Float(framePeriod)

	; --------------------------------------------------------------
	; Update game and world state
	; --------------------------------------------------------------
	
	For frameLimit = 1 To frameTicks
	
		If frameLimit = frameTicks Then CaptureWorld
		frameTime = frameTime + framePeriod
		
		UpdateEntity()
		
		UpdateWorld()
	
	Next
	
	; --------------------------------------------------------------
	; Draw 3D world
	; --------------------------------------------------------------

	RenderWorld frameTween
	
	; --------------------------------------------------------------
	; Show result
	; --------------------------------------------------------------
	
	Flip

Until KeyHit (1)
End

;These functions are usually placed in includes, but this will do.

;A simple function to create the test entity.

Function CreateEntity.Entity(X,Y,Z)

	e.Entity = New Entity
	e\Entity = CreateSphere()
	EntityAlpha(e\Entity,0.5)
	EntityColor(e\Entity,128,0,0)
	ScaleEntity(e\Entity,10,20,10)
	PositionEntity(e\Entity,X,Y,Z)
	EntityType(e\Entity,CT_ENTITY)
	EntityRadius(e\Entity,10,20)
	
	EntityParent(Cam,e\Entity) ;UGLY HACK FOR TESTING ONLY!
	
	Return(e)

End Function

;Normally this would be my UpdateEntities() function, but since
;we're just working with one, might as well strip it down.

Function UpdateEntity()

	;Basic gravity, not realistic looking at all...
	TranslateEntity(Player\Entity,0,-2,0)
	
	; Simple move commands, will need to be replaced with thrust.
	If KeyDown(200) Then
		MoveEntity(Player\Entity,0,0,5)
	ElseIf KeyDown(208) Then
		MoveEntity(Player\Entity,0,0,-2)
	EndIf
	
	;I use mouselook in my actual game, but for this test this
	;will suffice.
	If KeyDown(203) Then
		TurnEntity(Player\Entity,0,2,0)
	ElseIf KeyDown(205) Then
		TurnEntity(Player\Entity,0,-2,0)
	EndIf

End Function

;This just makes us a map out of cubes to test out the physics.

Function CreateMap()

	MapPart = CreateCube()
	ScaleEntity(MapPart,1000,200,1000)
	PositionEntity(MapPart,0,100,0)
	FlipMesh(MapPart)
	EntityType(MapPart,CT_MAP)
	
	MapPart = CreateCube()
	ScaleEntity(MapPart,100,10,200)
	PositionEntity(MapPart,500,-100,0)
	EntityType(MapPart,CT_MAP)
	
	MapPart = CreateCube()
	ScaleEntity(MapPart,100,20,200)
	PositionEntity(MapPart,600,-100,0)
	EntityType(MapPart,CT_MAP)
	
	MapPart = CreateCube()
	ScaleEntity(MapPart,100,30,200)
	PositionEntity(MapPart,700,-100,0)
	EntityType(MapPart,CT_MAP)
	
	MapPart = CreateCube()
	ScaleEntity(MapPart,100,40,200)
	PositionEntity(MapPart,800,-100,0)
	EntityType(MapPart,CT_MAP)
	
	MapPart = CreateCube()
	ScaleEntity(MapPart,100,50,200)
	PositionEntity(MapPart,900,-100,0)
	EntityType(MapPart,CT_MAP)
	
	MapPart = CreateCube()
	ScaleEntity(MapPart,100,60,200)
	PositionEntity(MapPart,1000,-100,0)
	EntityType(MapPart,CT_MAP)
	
	MapPart = CreateCube()
	ScaleEntity(MapPart,100,10,500)
	TurnEntity(MapPart,20,0,0)
	PositionEntity(MapPart,-500,-100,0)
	EntityType(MapPart,CT_MAP)

End Function



What I'm looking to do is give the player realistic gravity, the ability to give objects like the player a push from outside forces (to knock them around with explosions and such), and the ability to have objects rebound from surfaces. If anyone would like to help out here, major thanks are in order. Free copy of the finished game and name in credits too of course.