I'm attempting to create my own primitive 2D graphics based game engine. The core component of the game is firing different projectiles at enemies. I need to get this component working before I can continue working.
What I have now moves my projectile along a line going through the Starting point (x, y) and the Target point (x1, x2). I make use of the linear function y = mx + b
. The problem is that how I update the position of the projectile causes an inconsistent velocity depending on the slope of the line. (greater slopes cause it to move away more quickly).
Here's the core structure of the game loop I'm running:
private void executeGameLoop()
{
long nextFrameStart = System.nanoTime();
while(panel.getRunning())
{
do
{
panel.update();
nextFrameStart += FRAME_PERIOD;
} while(nextFrameStart < System.nanoTime());
long remaining = nextFrameStart - System.nanoTime();
panel.repaint();
if (remaining > 0)
{
try
{
Thread.sleep(remaining / 1000000);
}
catch(Throwable e)
{
System.out.println(e.getMessage());
}
}
}
}
This is just the mechanism for updating the structure and graphics. Every time this calls panel.update
the projectile updates its position, given certain circumstances. Here are the methods that update the projectile:
This tells the projectile it has a target and sets up information about the line.
public void setHasTarget(boolean hasTargetIn)
{
if(hasTargetIn)
{
deltaX = getTargetX() - getX();
deltaY = getTargetY() - getY();
slope = deltaY / deltaX;
intercept = getY();
System.out.println("y = " + slope + "x + " + intercept); //line the projectile will follow
}
hasTarget = hasTargetIn;
}
This next method sets the position to the next step on the line. (X is updated by velocity, y is updated dependent on x)
public void setNext()
{
float temp = (slope) * (getX() + velocity) + intercept;
System.out.println("Slope: " + (slope) * (getX() + velocity));
System.out.println("Current: (" + getX() + ", " + getY() + ")");
System.out.println("Next: (" + (getX() + velocity) + ", " + (getY() + temp) + ")");
setX(getX() + velocity);
setY(temp);
}
This last method calls setNext()
and is called by the main loop.
public void update()
{
if(hasTarget)
setNext();
}
As I've said, given my current code, the result when I run is a projectile that moves on the screen at inconsistent speeds dependent on the slope of the line. I would like to be able to change my code so that the projectile moves on the screen at a consistent rate over any trajectory. Thank you in advance for any help.