Advertisement

Path for a boomerang

Started by October 14, 2014 03:34 AM
2 comments, last by Marscaleb 10 years, 4 months ago

I am trying to write the code for a boomerang the character can use as a weapon, and it is giving me far more trouble than I thought it would

First of all, I'm only working in 2D, and it is a side view, so Y is up.

I'm not trying to create a wide arching path; for the most part I just want the boomerang to go out, hit a target point, and come straight back to the player.

This should be easy.

But there are problems with getting these minute details right. First of all, the boomerang shouldn't be moving at top speed the whole time; it needs to slow down gradually and then speed up as it returns. I would like (and its easy) to have a max speed, so for the most part it will be moving at its optimal speed, but when I get to that first return point there needs to be (even if its not really noticeable) slow down and speed up. Without this it looks cheap and unnatural.

So my first plan was to simply have the boomerang, each frame, calculate the direction to reach its target, and add a little bit of velocity in that direction. However, this created problems where the boomerang could easily just loop around its target because it can't nudge the velocity enough. But when I push that nudge value up too high, the path looks stiff because there is not enough gradual motion to it.

By the way, its target is first a set point in front of the player, and once it reaches that, the target becomes the player itself.

I was studying some old retro games, and I noticed their patterns suggest they move each axis individually rather than compute an angle and vector, so I thought I would try that.

I set it so each frame the boomerang subtracted the location of itself from the location of the target, and whether that was positive or negative it would either add or subtract the nudge value to the velocity. It did that for both of its axis.

But this caused problems where sometimes the boomerang became erratic on one axis, presumably because it overshoots its destination and repeatedly overshoots itself trying to hit that target.

I tried to reduce this by having it reduce the nudge amount proportionally to how far away it is from its target, once it was within a certain range, but this only made the erratic corrections follow a larger arc.

I've been thinking this over and I just can't figure out how to arrange this. What sort of logic should I be following? What sort of design should use?

I greatly welcome any other ideas I could try.

Read my webcomic: http://maytiacomic.com/
Follow my progress at: https://eightballgaming.com/

Physics is the best way. Slow the targets down when the boomerang is thrown, or speed the boomerang up. Missing is always a problem, so if you can't accept misses and you don't want an artificial looking path, you have to make missing less likely by reducing the chance of the target avoiding its natural path.
Advertisement

Usually modifying the acceleration of something tends to make it look a little smoother, you might also try tweaking how much acceleration/velocity can be changed on an axis. For example, the Y axis could be 'stiffer', and have a smaller maximum change amount.

Another idea is to cheat, use an ease-in-out equation to interpret between the start/destination -- though I'm not sure how well that will work with a moving player. Another thread, I mentioned a catmull-rom, but I think it might be overkill for your case.

Finally, you could keep your current method, but then put a timer on it, if the time to return is too great, then cheat and just do a linear interpolation to return it to the player. For cases where the boomerang went really far, it shouldn't matter, as the return would look linear anyway, and it will stop any constant orbiting.

Oh and lastly, you might want to make the boomerang 'smart', and have it try to reach where it thinks the player will be, by looking at the players velocity. Though that might look funny.

Usually modifying the acceleration of something tends to make it look a little smoother, you might also try tweaking how much acceleration/velocity can be changed on an axis. For example, the Y axis could be 'stiffer', and have a smaller maximum change amount.

...

Finally, you could keep your current method, but then put a timer on it, if the time to return is too great, then cheat and just do a linear interpolation to return it to the player. For cases where the boomerang went really far, it shouldn't matter, as the return would look linear anyway, and it will stop any constant orbiting.

Oh and lastly, you might want to make the boomerang 'smart', and have it try to reach where it thinks the player will be, by looking at the players velocity. Though that might look funny.

I really like those ideas.
I want to keep this as simple as possibly, because I can easily have several boomerangs on the screen at once. but I am going to start tooling with those ideas.

Read my webcomic: http://maytiacomic.com/
Follow my progress at: https://eightballgaming.com/

This topic is closed to new replies.

Advertisement