Example

Tanks

6

SP4CEBAR 2021-04-14 21:22

A side-viewed tank shooter game with parabolic projectile trajectories.

Tank.nx | Open in app
2021-04-14 21:22

Starfighter 2021-04-15 04:24

Oh nice, I’m a big fan of these ^_^. Pretty solid, I definitely can’t wait to see more ^_^.


Starfighter 2021-04-15 04:31

Would you (or anyone) explain the math behind this? I didn’t pay attention in math classes, I was too busy programming the calcs lol. I’d greatly appreciate it, cos it’d help me learn now what I ignored then. Like, I know what pi is and sine and cosine make waves but that’s about it lol.


SP4CEBAR 2021-04-15 07:17 (Edited)

@starfighter same, discovering new features of TI basic is way more interesting than paying attention to a math lesson. Either way, I've used the standard formula for a parabola "ax^2+bx+c" and I discovered that I can use "b" for the direction of the projectile (b=tan(r*0.5*pi)) and "a" for the fire power at which the projectile is launched, and then I offset the sprite of the bullet by the position of the tank (so the projectile starts at the tank)
I also made this desmos graph to experiment with it.
https://www.desmos.com/calculator/lakcuskpre


G-9 2021-04-15 08:17 (Edited)

Very nice collision !!!! There is just a problem : the ball follows the tank’s coordinates !
In other terms, if you throw a ball while retreating , the ball stays in his x position .
Well it’s still nice :3


SP4CEBAR 2021-04-15 15:26 (Edited)

@G-9 thanks, this version is just a test, I made it in a little over two hours, I've taken some shortcuts just to see if it works


nathanielbabiak 2021-04-27 05:24 (Edited)

You could use trigonometry rather than a parabola with these formulas:

velocity's horizontal component = ( total velocity ) * COS( degrees * PI / 180 )

velocity's vertical component = ( total velocity ) * SIN( degrees * PI / 180 )

They calculate the velocity at the instant the projectile is fired. Movement to the right (meaning all velocity is converted into positive horizontal velocity) occurs when DEGREES=0. Movement up (all velocity converted to negative vertical velocity) when DEGREES=90. Movement left when DEGREES=180, and movement down occurs when DEGREES=270. When DEGREES=360, it actually goes right again, and the cycle repeats.


nathanielbabiak 2021-04-27 05:32 (Edited)

To use those formulas, your game loop needs to keep track of both the projectile's location (e.i., sprite coordinates) and velocity components separately. Every iteration of your loop should just update the location by adding the velocity to it.

If you use those formulas the projectile travels in a straight line - it ignores gravity! To model gravity, every iteration of your game loop should increase the velocity's vertical component.

Maybe create a test program using total velocity of 2 and then, inside the projectile loop, increasing the vertical component of velocity by 0.05 each iteration. That's a good starting point for modeling. :-)


nathanielbabiak 2021-04-27 05:35 (Edited)

https://en.wikipedia.org/wiki/Projectile_motion


SP4CEBAR 2021-04-27 09:45 (Edited)

@nathanielbabiak thanks!, I could use the trigonometric trajectories together with my gun mechanics system: in my gun mechanics system an array stores the X position, Y position, Y velocity(spread), X velocity(bullet speed), and the projectile type for each projectile. It uses a function to update all projectiles every game loop. I can add the trigonometric trajectory to this function.


SP4CEBAR 2022-01-28 22:43 (Edited)

I just realized, the best way (for me) to make this practical is to fuse my gun mechanics (array for [x,y,dx,dy]) and my model of gravity rocket launch simulator together and Just use dx and dy as the velocity which is affected by the acceleration (proportionate to the forces of gravity and drag)


nathanielbabiak 2022-01-29 01:29

Exactly - excellent idea!


SP4CEBAR 2022-01-29 09:53

Thanks!


SP4CEBAR 2022-03-17 17:18 (Edited)

I've just implemented that system here


Log in to reply.