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 click mouse on the canvas.. |