what is the best way to add gravity

Blitz3D Forums/Blitz3D Tutorials/what is the best way to add gravity

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.

Gravity causes accelleration. To properly model accelleration, you need to keep track of the velocity of your entities. To keep track of additional information for an entity, use a TYPE:

Const GRAVITY# = 0.02

Type physical_body
	Field entity        ; each physical_body represents one entity
	Field vx#, vy#, vz# ; keep track of velocity
End Type

; here's a simple constructor to make it easier to create objects
Function create_physical_body.physical_body(entity)
	this.physical_body = New physical_body
	this\entity = entity
	Return this
End Function

; don't work with entities directly:
;player = createcube()
;positionentity(player, 1, 2, 3)

; instead, create objects to store the entities:
player.physical_body = create_physical_body( CreateCube() )
PositionEntity(player\entity, 1, 2, 3)

; game loop
While Not(KeyHit(1))
	; update all physical_bodies
	For pb.physical_body = Each physical_body
		pb\vy = pb\vy + GRAVITY                    ; add gravity
		TranslateEntity(pb\entity, pb\vx, pb\vy, pb\vz) ; move entity
	Next
	; update graphics
	RenderWorld
	Flip
Wend


Don't call TranslateEntity() directly anymore. Instead, modify the velocity of your physical_bodies and let the physics updating code in the loop move the entity for you.

P.S. Yes, there is a way to find the physical_body object that represents an entity, given only the entity (e.g. from a Pick.) To do so, store the object's Handle() in the entity's EntityName.