Gravity

BlitzPlus Forums/BlitzPlus Beginners Area/Gravity

Hi,

I need a physics routine for throwing a stone in 2D. This means, a person throws a stone, and it flies as real as possible through the air and lands at another point. Can you help me?

thx,
teddy

Without really testing, my estimation is that your stone needs an angle and a decaying speed (in the end, pixelwise, this will be converted to a deltaX and deltaY using sine and cosine) while gravity pulls the stone done, this gravity is then just added or subtracted from the stone's Y coord.

so:

start:
* convert stone angle+speed to deltaX and deltaY (float values!)
* add or sub a gravity amount
* draw stone
* multiply the stone speed with 0.999 or something
loop

Don't have this angle+speed converting formula at hand, it's not difficult tho, but it's probably
deltax=sin(angle)*speed
deltay=cos(angle)*speed

ohwell it probably *is* the fomula anyway.. :P


Perhaps another method is tho change the angle instead of just the y-coord.

I haven't personally tried this (I'm can't load blitz plus at work I tried once and got busted DOH!!!) anyway I think something like this might work I'll try it at home tonight if I think about it and see what I did wrong


Type rock
field V1 ; horizontal velocity you are throwing at
field height; the initial height you are throwing at
field Angle ; angle you are throwing at in radians
field x,y
end type

intiitialize your rock with whatever you are starting with
global rock.rock = new rock
rock\v1 = whatever
rock\angle = whatever
rock\height = whatever
rock\x = whatever

;in your main loop somewhere

if rock\y < rock\height ; if your y is less than your height you are back where you started from vertically

drawimage(rockimage, rock\x, rock\y)
UpdateRock()
end if


Function UpdateRock()
rock\y = rock\x * (Tan(rock\angle)) - (((4.9) * (rock\x ^ 2)) / ((rock\V1 ^ 2) * ((Cos(angle)) ^ 2)))

rock\x = rock\x + 1 ;increment by a pixel or whatever you want

end function

actually looking at this I guess you would want to increment your x value by your initial velocity

rock\x = rock\x + rock\V1

oops, I made code :P

window=CreateWindow("oO",0,0,640,480)

canvas=CreateCanvas(0,0,640,480,window)

timer=CreateTimer(25)



Global stone_angle
Global stone_speed#
Global stone_x#
Global stone_y#
Global gravity_delta#
Global gravity#

reset

Repeat
	WaitEvent()
	If EventID()=$803 End
	
	If EventID()=$4001

		stone_x=stone_x+Sin(stone_angle)*stone_speed
		stone_y=stone_y+Cos(stone_angle)*stone_speed
		stone_y=stone_y+gravity

		gravity=gravity*gravity_delta
		stone_speed=stone_speed*.985
	
		SetBuffer CanvasBuffer(canvas)

			Cls

			Color 255,0,0
			Rect 320,240,4,4,1
			Color 255,255,0
			Rect stone_x,stone_y,4,4,1
			Color 0,255,0
			Rect 0,400,640,4,1
		
		FlipCanvas canvas
	EndIf

	If EventID()=$201 reset ; mouseclick @ canvas
	
Forever

Function reset()
	stone_angle=Rnd(180)+90
	stone_speed#=4
	stone_x#=320
	stone_y#=240
	gravity_delta#=1.06
	gravity#=1
End Function


click mouse on the canvas..