Ballistics / Curves

Arakair

  • Posts: 13
Hi!

Im putting together the finishing touches on my first project in Stencyl, and ive run into a problem ive been avoiding for a few weeks now. The game is your basic tower defence type of game, where towers shoot projectiles from point A to point B. As this looks very stale, and im trying to add a nice curve between the two points. However, being terrible at math i cant seem to figure it out!

I guess the easiest way would be to just animate the projectile itself, so it looks like theres a curve when there is none, but i would rather do it the "right" way.. :)

Any ideas or pointers how i could solve this?

Cheers!

shotfrost

  • Posts: 221
well, i suggest that you make a point C between A and B,but not the middle of AB.make the projectile go to C first then go to B.that way doesn't require any math but i don't know for sure whether it will work.

Rexasul

  • Posts: 147
Are you wanting to draw a curve between point A and B, or just make the projectile turn itself as if it were following that curve? If it's the latter, you could try this:
my website |>>| shadowlabs.net

Arakair

  • Posts: 13
Thanks for the input! Not quite what im after tho!

Making a point C is probably a good idea, and then curve the movement with those points, i still have no clue how to do the actual curve tho.. :)

I drew a quick picture in paint to clear up any misunderstandings! :) .. Trying to do a curve like the second illustration, arcing the bullet from point A to B, instead of it going in a straight boring line!

Cheers!

Rexasul

  • Posts: 147
Ah, then I'm afraid I'm stuck on this one too. It seems that the only way to actually "draw" a curve is by using code. I think this was available as a block at one point but it has since been taken out (my guess is that it didn't work properly). There's even some old documentation for it.

If I can get anywhere with it, I'll let you know. But I really hope someone else has a decent answer -_-

edit* the comments in the code are vague and/or wrong. it's just what I have so far

Code: [Select]
/* starting point */
g.beginDrawCurve(_playerweapon.getXCenter(), _playerweapon.getYCenter());

/* curve point */
g.addPointToCurve(0,0,0,0);

/* end point */
g.addPointToCurve(0,0,0,0);

/* end drawing */
g.endDrawingCurve();


« Last Edit: June 18, 2012, 11:12:02 am by Rexasul »
my website |>>| shadowlabs.net

froz

  • Posts: 250
I'm not sure if it would work, but I just thought of something you could try.

In the update event for bullet, set it's velocity towards [x of enemy], and ([y of enemy] - [absolute value of ([x of self] - [x of enemy])].

What I think it will achieve - at start the bullet will go higher then enemy is, then it will gradually set bullet movement direction lower and lower, untill it hits enemy. The biggest curve (45 starting degree) will be when enemy has the same y value as your tower. There will be no curve if enemy has the same x value as your tower (meaning it's straight below or on top of tower).

Since it would update every tick, it should create smooth curve.

Rexasul

  • Posts: 147
Agh, I was caught up in trying to do my own thing that I misunderstood you (again). Could you show me your current shooting behaviour?
my website |>>| shadowlabs.net

Rexasul

  • Posts: 147
Anyway, what froz said should work. The general jist of it is that you have to constantly update the direction of the bullet towards the target.
my website |>>| shadowlabs.net

froz

  • Posts: 250
Yes, though I'm assuming he already need to do that (otherwise bullet could miss moving target). Ofcourse the code I wrote could take little bit more time to process.

Rexasul

  • Posts: 147
I meant facing towards its direction, if that's even a proper description of it. My brain isn't functioning correctly -_-
my website |>>| shadowlabs.net

Arakair

  • Posts: 13
Ive been experimented a bit, so far i managed to get a nice looking downwards curve, its not hitting the target tho!

In the update event for bullet, set it's velocity towards [x of enemy], and ([y of enemy] - [absolute value of ([x of self] - [x of enemy])].
I dont think i understand this "and" correctly, there is only one field in the "set velocity" block, and when using 2 blocks only the last one runs. Im probably just too tired, ill sleep on it and try again first thing tomorrow! :)

Cheers again guys!

froz

  • Posts: 250
Oh, I'm sorry, I forgot that velocity takes degrees, not coordinates. In such case you would have to calculate angle (using atan2), something like this:

Code: [Select]
atan2[([y of enemy] - [absolute value of ([x of self] - [x of enemy]) - [y of self]) ([x of enemy] - [x of enemy])]
meaning - it takes distance in x and distance in y and decrement y by the absolute value of distance in x.

This may indeed need more time to calculate. How do you calculate bullets motion currently (I mean without the curve)?

Arakair

  • Posts: 13
Ah, ofcourse, i should have been able figured that out myself haha! Thanks alot for clearing it out!

Code: [Select]
atan2[([y of enemy] - [absolute value of ([x of self] - [x of enemy]) - [y of self]) ([x of enemy] - [x of enemy])]
meaning - it takes distance in x and distance in y and decrement y by the absolute value of distance in x.

This may indeed need more time to calculate. How do you calculate bullets motion currently (I mean without the curve)?

I tested it out, and it works like a charm! Cheers!

Would it be possible to alter the initial angle to be higher, say, -90 degrees (straight up?). I played around with the numbers a bit, but cant seem to solve it.

My old calculation was basically the same (without the curving ofcourse), setting the velocity between the two points using atan2. I guess its somewhat heavy to calculate every frame, but i cant find a more lightweight approach.

Thanks again! You saved me from the headaches! :)

froz

  • Posts: 250
You should be able to alter the angle by multiplying the part [absolute value of(blahblah)]. The more you multiply it, the higher angle it will be, though it will never be 90 (it would require x to be set to 0). Moreover, if the angle would be 90, the bullet would never corrected it's velocity toward the enemy.

Since the last post I have figure out it will probably be a little bit better if you put distance in attributes. Easier to read and only calculated once (x distance was calculated twice). So, together with the multiplier it would be something like that:

Code: [Select]
set xDistance to [x of enemy] - [x of self]
set yDistance to [y of enemy] - [y of self]
then the velocity block with atan2(yDistance - ([absolute value of xDistance] * multiplier), xDistance)
You can use whatever number as multiplier, 1 will get you the same result as without it (obviously), higher number should make the initial angle higher and number between 0 and 1 should make it lower.

« Last Edit: June 19, 2012, 09:03:02 am by froz »