I'm making a game in java where enemy sprites spiral to the center and damage the main tower. The only thing I'm having problems with is the formula to make the sprite spiral. All I found on the internet it this: http://scratch.mit.edu/projects/1439249/
That is sort of what I want to do but I want to make them spiral to a point from outside the JFrame, not from a point within the JFrame.
I am in Secondary 4 and I don't have to much knowledge about such formulas yet and sorry if I have problems understanding the formulas. Thanks in advance!
One simple way of making a sprite appear to spiral is to pretend that it is attached to an arm, like the hand of a clock, that rotates around the center of the spiral. As that arm rotates, slowly move the sprite down the arm towards the center. What you end up with is a classic Archimedan Spiral
I can mock up some code for you, but that's going to take a few minutes.
Okay, here's the code.
These are the core of the math. They return the x and y values of an entity that is at the specified distance from the center (length) and angle from the center (angle).
Now, I don't know how you have your code set up, but lets pretend we have a
double
namedspiralProgress
that represents how far into its spiral your entity is. AtspiralProgress == 0
, the entity is just starting, and atspiralProgress == 1
, the entity is at the center.Here is what the code to get the x and y for the entity would look like:
In that snippet,
startingRadius
is how many units (pixels, if thats what x and y means in your program), the entity should start away from the center, androtations
is how many times the entity should loop around the center before reaching it.The coordinates this returns are for a spiral around {0, 0}, so if you want to spiral around some other point, say
{screenWidth / 2, screenHeight / 2}
, you'd addscreenWidth / 2
tox
andscreenHeight / 2
toy
.Here is a full Java program that demonstrates this math. Click the mouse anywhere in the window to reset the spiral.