Author Topic: How to program a trajectory  (Read 5785 times)

Zito

  • Newbie
  • *
  • Posts: 5
How to program a trajectory
« on: September 16, 2011, 09:18:31 PM »
I'm having a go at modifying the Lunar Lander demo so that 2 guns fire projectiles at one another.

There is an example here of moving projectiles using a trajectory calculation but I don't know how to convert it into Hypernext.

http://www.blitzbasic.com/Community/posts.php?topic=85396

How can I convert this to fit into the sprite event game loop?



Malkom

  • Administrator
  • Sr. Member
  • *****
  • Posts: 289
Re: How to program a trajectory
« Reply #1 on: September 17, 2011, 01:56:06 PM »
Here is that section in blue text converted to HyperNext. The part inside the FOR loop would go in your sprite movement section of the Animation event script.

Just place the script in a button and it will plot the trajectory on a canvas. I used a 300x380 canvas.

Code: [Select]
Local angle,v,x,y,dx,dy,p,gravity

CanvasClear(1,50,50,50)

@ initial conditions
Put 10 into x
Put 330 into y
Put -80 into angle
Put 8 into v
Put 0.1 into gravity

@ convert degrees to radians
Divide angle by 180
Multiply angle by PiFN

@ Calc initial x and y
Put CosFN(angle) into dx
Multiply dx by v
Put SinFN(angle) into dy
Multiply dy by v

@ Plot trajectory
CanvasSetColor(1,255,255,255)
for p=1 to 200
    @ x
    Add dx to x
    @ y
    Add dy to y
    Add gravity to dy
    CanvasPlot(1,x,y)
endfor


I am sorry but I do not have time to answer questions by PM or email.
If you post your questions in this forum then it might help others.

Zito

  • Newbie
  • *
  • Posts: 5
Re: How to program a trajectory
« Reply #2 on: September 21, 2011, 07:55:43 AM »
This works very well and is easy to follow, thank you.

Its strange about the Y axis being upside down as pen paper graphs don't work this way.