what is the best way to add gravity

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

yes i know i have been posting alot but i'm just starting and i can't figure out some of these things but anyway what is the best way for gravity u could give me an example or if there is a tutorial i don't know about that will work too, woops wrong forum

simple gravity:
TranslateEntity camera, 0, -1, 0


Have a variable called gravity. when the player isn't touching the ground, decrease this variable by 0.1 everyloop, untill he is touching. Make sure the variable doesn't go below -70 though.

gravity# = 0


while not keyhit(1)

   if player not touching ground then; pseudo code
      gravity = gravity - 0.1
      if gravity <-70 then
         gravity = -70
      end if
   else
      gravity = 0
   end if

   moveentity camera,0,sin(gravity),0

Wend


Some rough code. Basically use Sin to create simple acceleration using the sin wave. :o) Works pretty well.

W
 #4
Im having troubles getting it to work, how are you testing to see if your player has collided with the level??

This is somethign that I wrote ages ago as a tutorial that might help

; Thrust type example by Rob Farley (http://www.mentalillusion.co.uk)
; Use the cursor keys to move it block around the screen

Graphics 640,480,16 ; set graphics mode
SetBuffer BackBuffer() ; set back buffer to enable screen flipping
Cls


; set up some varibles all of the ones below have a #
; this means they are floating point numbers ie they have a decimal place
x#=320 ;x position
y#=200 ;y position
xspeed#=0 ; xspeed
yspeed#=0 ; yspeed

; these are the keys for control Const sets them up as constant varibles
; this means they can't be changed but it makes everything a bit faster
Const k_left=203, k_right=205, k_up=200 , k_down=208


; start the main loop
Repeat ; note the loop is indented for easy to read code


	Cls ;clear the screen

	Color 0,255,0
	Line 0,470,639,470 ;draw a green line at the bottom of the screen

	If KeyDown(k_left) Then xspeed#=xspeed#-.1 ;this adds acceleration in the correct direction
	If KeyDown(k_right) Then xspeed#=xspeed#+.1
	If KeyDown(k_up) Then yspeed#=yspeed#-.2

	x#=x#+xspeed# ;this adds the acceleration to the current x,y position
	y#=y#+yspeed#

	xspeed#=xspeed#*.99 ;this makes the xspeed 99% of what is was ie adds friction and slows you down
	yspeed#=yspeed#+.05 ; here we are adding gravity acceleration to the y direction so you're always being pulled down


	If y#>460 Then y#=460:yspeed#=-yspeed#/2 ; this is hitting the bottom of the screen and bouncing you up at half the speed you hit it
	If y#<0 Then y#=0:yspeed#=0 ; stopping you at the top of the screen
	If x#>650 Then x#=-10 ; wrapping you from right to left
	If x#<-10 Then x#=650 ; wrapping you from left to right

	Color 255,255,0
	Rect x#-10,y#-10,20,20 ; draw a yellow rectangle at your position

	Flip ; flip the back buffer to the front so you can see what's been drawn

Until KeyHit(1) ; back to the beginning of the main loop until ESC is hit

End ; End the program


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.

A sidenote: you maybe need to add a pivot right between the feet of the characater to check if he collided with the ground. Checking for ground contact with the entire mesh or the shoes is not a good idea since it/they can touch walls as well, so you would allow him to climb up walls as if he was standing on the ground. Using a pivot between the feet, with an EntityRadius that is big enough to detect the ground and ONLY the ground can fix this.

An other solution is to use a linepick against the ground.