I’m currently prototyping a 2d side scroller and I’m looking for some advice on how to plot the path of a boomerang projectile.
For the prototype, the projectile will move in a simple straight line away from and back to the thrower (I know not very realistic). I was thinking of using A* pathfinding to plot the route of the boomerang, but I though it could be possible to create a line object (not rendered) dividing that up equally into nodes and using that to plot the projectiles course.
I know both are possible, but I wondered which is easier to implement and relatively speaking, which is a better implementation?
I think it would be even easier to just forget about pathing the boomerang. Just shoot it out like a bullet and have a timer that will expire (or when it collides with a wall/enemy) and tell the boomerang to a “go back to player” state. In this state, it will stop moving forward and instead, track back towards the player (just like a follow missile).
No, pathfinding is too expensive an operation for projectiles (especially if you have lots).
To backtrack to the source, you simply get position of source and the projectile and then move projectile closer towards source (at desired speed). You repeat this every frame until projectile reaches the source. To get a more smoother curvey move back just slowly subtract new direction from projectiles current direction.
What if the player moves though? That would throw of the distance between the player and the thrown boomerang. Unless the player has to stand still while the boomerang is in the air.
I’d probably go for steering behaviours for this ( http://www.red3d.com/cwr/steer/ ), assuming you always want the boomerang to return to the character. Just a simple one which start with a ‘repel’ and switches to an ‘attract’ after a certain amount of time would do it.
The x and y are the boomerang coordinates and the targetX and targetY would be your character’s coordinates.
Try to get this working, it’s simpler:
// when the boomerang is ahead of the character, move it backwards and move it
// forwards when it is behind the character.
if(boomerangX > characterX){
boomerangX-=boomerangSpeed;
}
else{
boomerangX+=boomerangSpeed;
}
// when the boomerang is below of the character, move it upwards and move it
// downwards when it is above the character.
if(boomerangY > characterY){
boomerangY-=boomerangSpeed;
}
else{
boomerangY+=boomerangSpeed;
}
Each object in play needs an on-screen position, hence the x/y. When doing these free-flying objects like you do now, an approach to just very basic physics is to assign a pair more of doubles of the veloctity of the object. Two, because one for velocity on the x-axis and one on the y-axis.
Then when you update position, you can do something like:
player.setX(player.getX() + player.getXV());
This is provided that your x/y is also doubles (just cast them to integers when drawing).
Acceleration is as easy as player.setXV(player.getXV() + GameMain.SPEED_FACTOR); and be sure to decrease it as well if you want that