Code archives/Algorithms/Basic Cannonballs Physics 3D
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
. | |||||
. |
Comments
| ||
This code snippet could be useful for creating a game like the world famous qbasic's: Gorillas. Adjusting the angle and speed of the cannonball would be required. |
| ||
Nice Work! Short and OK. Look also to my Prg, it may help for more. http://www.blitzbasic.com/codearcs/codearcs.php?code=1164 feel inspired. |
| ||
"theduck" - here is a more general equation for what you are doing. new y = -0.5*gravity*time*time + initial y velocity * time + initial y position new x position = initial x position + time* intial velocity in x direction new z position = initial z position + time* initial velocity in z direction If you use a scale of 1 blitz unit = 1 metre then gravity should be (for Earth) 9.8. Time is usually measured in seconds elapsed since cannon ball fired. initial x, y ,z positions are the position of the ball prior to being fired. This assumes a constant acceleration due to gravity as well as no air resistance. initial x,y,z velocities are the velocities of the cannon ball in those directions as it is fired. |
| ||
For posterity, I found this code that was deleted above on archive site. Type cannonball Field x Field y Field cube End Type Graphics3D 640,480,16,2 SetBuffer(BackBuffer()) cam=CreateCamera() MoveEntity cam,35,10,-70 c.cannonball=New cannonball c\cube=CreateSphere() While True If KeyHit(57) c.cannonball=New cannonball:c\cube=CreateSphere():c\x=0:c\y=0 For c.cannonball=Each cannonball c\y=(-0.01347*c\x*c\x+0.9325*c\x+5.5)*2 c\x=c\x+1 PositionEntity c\cube,c\x,c\y,0 Next RenderWorld Flip Wend |
| ||
The code does not free the Sphere/Type when it goes offscreen. Here is a fix for this: Type cannonball Field x Field y Field cube End Type Graphics3D 640,480,32,2 SetBuffer(BackBuffer()) cam=CreateCamera() MoveEntity cam,35,10,-70 c.cannonball=New cannonball c\cube=CreateSphere() While True If KeyHit(57) c.cannonball=New cannonball:c\cube=CreateSphere():c\x=0:c\y=0 p=0 For c.cannonball=Each cannonball p=p+1 c\y=(-0.01347*c\x*c\x+0.9325*c\x+5.5)*2 c\x=c\x+1 PositionEntity c\cube,c\x,c\y,0 If c\y<0 FreeEntity c\cube Delete c EndIf Next RenderWorld Text 0,0,"Active Cannonballs="+p Flip (1) Wend |
Code Archives Forum