Moving an object at a constant speed from point A to B

I am trying to create my own primitive 2D graphics engine. The main component of the game is shooting from various shells at enemies. I need this component to work before I can continue.

That I am now moving my projectile along a line passing through the Starting point (x, y) and Target point (x1, x2). I use the linear function y = mx + b . The problem is that, as I update the position of the projectile, it causes an inconsistent speed depending on the slope of the line. (large slopes make him leave faster).

Here is the basic structure of the game loop in which I run:

  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 a structure and graphics update mechanism. Each time you call panel.update shell updates its position, taking into account certain circumstances. Here are the methods that update the projectile:

This says that the projectile has a target and sets line information.

 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 in the line. (X is updated by speed, y is updated by 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 said, given my current code, the result when I launch is a projectile that moves around the screen at inconsistent speeds, depending 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 constant speed along any path. Thank you in advance for any help.

+4
source share
1 answer

In general, the best way to handle directional motion is to use trigonometry. Your projectile needs two things to do this: direction (in radians) and speed.

The three trigger functions you need are sin, cos, and arctan

To update your X: setX(getX() + (speed * Math.cos(direction)));

To update your Y: setY(getY() + (speed * Math.sin(direction)));

To calculate the direction: Math.atan(slope)

You must add the direction and speed fields to your class and declare them as duplicates.

 public void setHasTarget(boolean hasTargetIn) { if (hasTargetIn) { deltaX = getTargetX() - getX(); deltaY = getTargetY() - getY(); direction = Math.atan(deltaY / deltaX); // Math.atan2(deltaY, deltaX) does the same thing but checks for deltaX being zero to prevent divide-by-zero exceptions speed = 5.0; } hasTarget = hasTargetIn; } public void setNext() { setX(getX() + (speed * Math.cos(direction))); setY(getY() + (speed * Math.sin(direction))); } 
+8
source

Source: https://habr.com/ru/post/1501583/


All Articles