🎉 Celebrating 25 Years of GameDev.net! 🎉
Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
Psuedo code for projectile.
Hi I am trying to create a Cannon Fodder type game, (where two guns are placed to fight against each other) and you enter the Power and Angle of your shot. I was wondering if anyone had some simple to understand psuedo code for the projectile motion.
Okay...I''ll try to keep this simple as one could easily overcomplicate it...
Just so we are on the same ''page'' I''m assumeing that the game is a 2D side view Worms type thing...game world cordinates are X and Y with X=0,Y=0 being the upper left corner...
here is the basic data structure for projectiles:
projectile:[
locationX = horizontal screen cord of bullit
locationY = vertical screen cord of bullit
vectorX = horizontal vector value
vectorY = vertical vector value
projectileID = indicates what type of bullit this is (nuke, etc.)
Safety = integer value, if equal to zero then projectile explodes on impact]
From the player inputed angle and power settings you calculate a VECTOR...this is the initial pathway the projectile will take.
So as the player fires the weapon...you make the projectiles game location the same as the player (locationX=playerX, etc..)...calculate the projectile vector...set the projectileID...and safety value to something greater then zero...
so here would be the projectile in the game loop:
process projectile:
add the vectorX/Y to the location X/Y
check for collisions (useing locationX/Y and projectileID)
if collision
if safety < 1 then BOOM!
if safety > 0 then projectile is a dud
end if
calculate new vector by modifying it with a gravity/wind/other vectors
if safety > 0 then reduce safety by 1
end process
Just trying to simplify it a bit...so the above is more a guide then real pseudocode...
about modifying vectors:
first lets say we have a gravity vector
gravityX=0
gravityY=.25
if our projectile vector is this at the start of the bullits travel
vectorX=1
vectorY=-1
and the bullit is at
locationX=100
locationY=100
the first frame the bullit will move to:
locationX=101 (locationX + vectorX)
locationY=99 (locationY + vectorY)
and the vector will be modified as such:
vectorX=1 (vectorX + gravityX)
vectorY=-.75 (vectorY + gravityY)
so in the second frame we would find the bullit at:
locationX=102 (locationX + vectorX)
locationY=98.25 (locationY + vectorY)
eventualy the bullit will start ''dropping'' in an arc because it is being modified by gravity...
Just so we are on the same ''page'' I''m assumeing that the game is a 2D side view Worms type thing...game world cordinates are X and Y with X=0,Y=0 being the upper left corner...
here is the basic data structure for projectiles:
projectile:[
locationX = horizontal screen cord of bullit
locationY = vertical screen cord of bullit
vectorX = horizontal vector value
vectorY = vertical vector value
projectileID = indicates what type of bullit this is (nuke, etc.)
Safety = integer value, if equal to zero then projectile explodes on impact]
From the player inputed angle and power settings you calculate a VECTOR...this is the initial pathway the projectile will take.
So as the player fires the weapon...you make the projectiles game location the same as the player (locationX=playerX, etc..)...calculate the projectile vector...set the projectileID...and safety value to something greater then zero...
so here would be the projectile in the game loop:
process projectile:
add the vectorX/Y to the location X/Y
check for collisions (useing locationX/Y and projectileID)
if collision
if safety < 1 then BOOM!
if safety > 0 then projectile is a dud
end if
calculate new vector by modifying it with a gravity/wind/other vectors
if safety > 0 then reduce safety by 1
end process
Just trying to simplify it a bit...so the above is more a guide then real pseudocode...
about modifying vectors:
first lets say we have a gravity vector
gravityX=0
gravityY=.25
if our projectile vector is this at the start of the bullits travel
vectorX=1
vectorY=-1
and the bullit is at
locationX=100
locationY=100
the first frame the bullit will move to:
locationX=101 (locationX + vectorX)
locationY=99 (locationY + vectorY)
and the vector will be modified as such:
vectorX=1 (vectorX + gravityX)
vectorY=-.75 (vectorY + gravityY)
so in the second frame we would find the bullit at:
locationX=102 (locationX + vectorX)
locationY=98.25 (locationY + vectorY)
eventualy the bullit will start ''dropping'' in an arc because it is being modified by gravity...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement