Path Following Algorithm for Entity
up vote
1
down vote
favorite
Within my game there are many entities, where each entity has a path (Array<Vector2>
) assigned to it. No path to follow (indicating that the entity should move at all) is indicated by an empty path (the path is never null
). updateMoveAttributes
is called every frame for every entity, so it is important that it is efficient.
Note that my entity's direction is in radians, ranging from - Math.PI
to Math.PI
, since this is what Math.atan2
returns.
The below snippet contains updateMoveAttributes
and its supporting methods.
private final Array<Vector2> path = new Array<>();
private int pathIndex = 0;
private static final float EPSILON = 1e-3f;
private float getXDifference(Vector2 pathComponent) {
float xDifference = pathComponent.x - getSprite().getX();
xDifference = Math.abs(xDifference) < EPSILON ? 0 : xDifference;
return xDifference;
}
private float getYDifference(Vector2 pathComponent) {
float yDifference = pathComponent.y - getSprite().getY();
yDifference = Math.abs(yDifference) < EPSILON ? 0 : yDifference;
return yDifference;
}
public float orient(Vector2 pathComponent) {
float xDiff = getXDifference(pathComponent);
float yDiff = getYDifference(pathComponent);
return (float) Math.atan2(yDiff, xDiff);
}
void updateMoveAttributes() {
// No path assigned
if (getPath().size == 0)
return;
// No more path to travel
if (getPathIndex() == getPath().size) {
setSpeed(0);
return;
}
Vector2 nextPathComponent = getPath().get(getPathIndex());
float angleToComponent = orient(nextPathComponent);
setDirection(angleToComponent);
float xDiff = getXDifference(nextPathComponent);
float yDiff = getYDifference(nextPathComponent);
boolean angleInPositiveXQuadrants = (-Math.PI / 2 <= angleToComponent && angleToComponent <= Math.PI / 2);
boolean pastX = angleInPositiveXQuadrants ? (xDiff <= 0) : (xDiff >= 0);
boolean angleInPositiveYQuadrants = (0 <= angleToComponent) && (angleToComponent <= Math.PI);
boolean pastY = angleInPositiveYQuadrants ? (yDiff <= 0) : (yDiff >= 0);
// Indicates that we have passed the path component we are on route to, adjust course with respect
// to next available path component.
if (pastY && pastX)
setPathIndex(getPathIndex() + 1);
// In case user upgrades entity during travel of entity -> movement speed changes -> update required for actual speed
setSpeed(getMovementSpeed());
}
public Array<Vector2> getPath() {
return path;
}
java performance game coordinate-system libgdx
bumped to the homepage by Community♦ 6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
1
down vote
favorite
Within my game there are many entities, where each entity has a path (Array<Vector2>
) assigned to it. No path to follow (indicating that the entity should move at all) is indicated by an empty path (the path is never null
). updateMoveAttributes
is called every frame for every entity, so it is important that it is efficient.
Note that my entity's direction is in radians, ranging from - Math.PI
to Math.PI
, since this is what Math.atan2
returns.
The below snippet contains updateMoveAttributes
and its supporting methods.
private final Array<Vector2> path = new Array<>();
private int pathIndex = 0;
private static final float EPSILON = 1e-3f;
private float getXDifference(Vector2 pathComponent) {
float xDifference = pathComponent.x - getSprite().getX();
xDifference = Math.abs(xDifference) < EPSILON ? 0 : xDifference;
return xDifference;
}
private float getYDifference(Vector2 pathComponent) {
float yDifference = pathComponent.y - getSprite().getY();
yDifference = Math.abs(yDifference) < EPSILON ? 0 : yDifference;
return yDifference;
}
public float orient(Vector2 pathComponent) {
float xDiff = getXDifference(pathComponent);
float yDiff = getYDifference(pathComponent);
return (float) Math.atan2(yDiff, xDiff);
}
void updateMoveAttributes() {
// No path assigned
if (getPath().size == 0)
return;
// No more path to travel
if (getPathIndex() == getPath().size) {
setSpeed(0);
return;
}
Vector2 nextPathComponent = getPath().get(getPathIndex());
float angleToComponent = orient(nextPathComponent);
setDirection(angleToComponent);
float xDiff = getXDifference(nextPathComponent);
float yDiff = getYDifference(nextPathComponent);
boolean angleInPositiveXQuadrants = (-Math.PI / 2 <= angleToComponent && angleToComponent <= Math.PI / 2);
boolean pastX = angleInPositiveXQuadrants ? (xDiff <= 0) : (xDiff >= 0);
boolean angleInPositiveYQuadrants = (0 <= angleToComponent) && (angleToComponent <= Math.PI);
boolean pastY = angleInPositiveYQuadrants ? (yDiff <= 0) : (yDiff >= 0);
// Indicates that we have passed the path component we are on route to, adjust course with respect
// to next available path component.
if (pastY && pastX)
setPathIndex(getPathIndex() + 1);
// In case user upgrades entity during travel of entity -> movement speed changes -> update required for actual speed
setSpeed(getMovementSpeed());
}
public Array<Vector2> getPath() {
return path;
}
java performance game coordinate-system libgdx
bumped to the homepage by Community♦ 6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Within my game there are many entities, where each entity has a path (Array<Vector2>
) assigned to it. No path to follow (indicating that the entity should move at all) is indicated by an empty path (the path is never null
). updateMoveAttributes
is called every frame for every entity, so it is important that it is efficient.
Note that my entity's direction is in radians, ranging from - Math.PI
to Math.PI
, since this is what Math.atan2
returns.
The below snippet contains updateMoveAttributes
and its supporting methods.
private final Array<Vector2> path = new Array<>();
private int pathIndex = 0;
private static final float EPSILON = 1e-3f;
private float getXDifference(Vector2 pathComponent) {
float xDifference = pathComponent.x - getSprite().getX();
xDifference = Math.abs(xDifference) < EPSILON ? 0 : xDifference;
return xDifference;
}
private float getYDifference(Vector2 pathComponent) {
float yDifference = pathComponent.y - getSprite().getY();
yDifference = Math.abs(yDifference) < EPSILON ? 0 : yDifference;
return yDifference;
}
public float orient(Vector2 pathComponent) {
float xDiff = getXDifference(pathComponent);
float yDiff = getYDifference(pathComponent);
return (float) Math.atan2(yDiff, xDiff);
}
void updateMoveAttributes() {
// No path assigned
if (getPath().size == 0)
return;
// No more path to travel
if (getPathIndex() == getPath().size) {
setSpeed(0);
return;
}
Vector2 nextPathComponent = getPath().get(getPathIndex());
float angleToComponent = orient(nextPathComponent);
setDirection(angleToComponent);
float xDiff = getXDifference(nextPathComponent);
float yDiff = getYDifference(nextPathComponent);
boolean angleInPositiveXQuadrants = (-Math.PI / 2 <= angleToComponent && angleToComponent <= Math.PI / 2);
boolean pastX = angleInPositiveXQuadrants ? (xDiff <= 0) : (xDiff >= 0);
boolean angleInPositiveYQuadrants = (0 <= angleToComponent) && (angleToComponent <= Math.PI);
boolean pastY = angleInPositiveYQuadrants ? (yDiff <= 0) : (yDiff >= 0);
// Indicates that we have passed the path component we are on route to, adjust course with respect
// to next available path component.
if (pastY && pastX)
setPathIndex(getPathIndex() + 1);
// In case user upgrades entity during travel of entity -> movement speed changes -> update required for actual speed
setSpeed(getMovementSpeed());
}
public Array<Vector2> getPath() {
return path;
}
java performance game coordinate-system libgdx
Within my game there are many entities, where each entity has a path (Array<Vector2>
) assigned to it. No path to follow (indicating that the entity should move at all) is indicated by an empty path (the path is never null
). updateMoveAttributes
is called every frame for every entity, so it is important that it is efficient.
Note that my entity's direction is in radians, ranging from - Math.PI
to Math.PI
, since this is what Math.atan2
returns.
The below snippet contains updateMoveAttributes
and its supporting methods.
private final Array<Vector2> path = new Array<>();
private int pathIndex = 0;
private static final float EPSILON = 1e-3f;
private float getXDifference(Vector2 pathComponent) {
float xDifference = pathComponent.x - getSprite().getX();
xDifference = Math.abs(xDifference) < EPSILON ? 0 : xDifference;
return xDifference;
}
private float getYDifference(Vector2 pathComponent) {
float yDifference = pathComponent.y - getSprite().getY();
yDifference = Math.abs(yDifference) < EPSILON ? 0 : yDifference;
return yDifference;
}
public float orient(Vector2 pathComponent) {
float xDiff = getXDifference(pathComponent);
float yDiff = getYDifference(pathComponent);
return (float) Math.atan2(yDiff, xDiff);
}
void updateMoveAttributes() {
// No path assigned
if (getPath().size == 0)
return;
// No more path to travel
if (getPathIndex() == getPath().size) {
setSpeed(0);
return;
}
Vector2 nextPathComponent = getPath().get(getPathIndex());
float angleToComponent = orient(nextPathComponent);
setDirection(angleToComponent);
float xDiff = getXDifference(nextPathComponent);
float yDiff = getYDifference(nextPathComponent);
boolean angleInPositiveXQuadrants = (-Math.PI / 2 <= angleToComponent && angleToComponent <= Math.PI / 2);
boolean pastX = angleInPositiveXQuadrants ? (xDiff <= 0) : (xDiff >= 0);
boolean angleInPositiveYQuadrants = (0 <= angleToComponent) && (angleToComponent <= Math.PI);
boolean pastY = angleInPositiveYQuadrants ? (yDiff <= 0) : (yDiff >= 0);
// Indicates that we have passed the path component we are on route to, adjust course with respect
// to next available path component.
if (pastY && pastX)
setPathIndex(getPathIndex() + 1);
// In case user upgrades entity during travel of entity -> movement speed changes -> update required for actual speed
setSpeed(getMovementSpeed());
}
public Array<Vector2> getPath() {
return path;
}
java performance game coordinate-system libgdx
java performance game coordinate-system libgdx
asked Jul 8 at 20:44
Mar Dev
26229
26229
bumped to the homepage by Community♦ 6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
- Method
getXDifference
andgetYDifference
are very similar, you can convert this into a single method that accepts two variables. - you could also inline the return statement as the method name is sufficient to tell what it is returning.
Instead of calculating
pastX
andpastY
, you can write methods to return the computed value.
boolean isPastX(variables...){
// logic to calculate pastX and return it.
}
and the
if
statement will turn intoif( isPastX() && isPastY())
.
This will also help in short-circuiting the condition in case calculatingpastX
orpastY
is very expensive(it isn't in this example, just a suggestion).
Curly braces
{}
should always be used after conditional statements or loops, even it's just a single line. It may avoid confusion in some scenarios.
if (getPath().size == 0){
return;
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
- Method
getXDifference
andgetYDifference
are very similar, you can convert this into a single method that accepts two variables. - you could also inline the return statement as the method name is sufficient to tell what it is returning.
Instead of calculating
pastX
andpastY
, you can write methods to return the computed value.
boolean isPastX(variables...){
// logic to calculate pastX and return it.
}
and the
if
statement will turn intoif( isPastX() && isPastY())
.
This will also help in short-circuiting the condition in case calculatingpastX
orpastY
is very expensive(it isn't in this example, just a suggestion).
Curly braces
{}
should always be used after conditional statements or loops, even it's just a single line. It may avoid confusion in some scenarios.
if (getPath().size == 0){
return;
}
add a comment |
up vote
0
down vote
- Method
getXDifference
andgetYDifference
are very similar, you can convert this into a single method that accepts two variables. - you could also inline the return statement as the method name is sufficient to tell what it is returning.
Instead of calculating
pastX
andpastY
, you can write methods to return the computed value.
boolean isPastX(variables...){
// logic to calculate pastX and return it.
}
and the
if
statement will turn intoif( isPastX() && isPastY())
.
This will also help in short-circuiting the condition in case calculatingpastX
orpastY
is very expensive(it isn't in this example, just a suggestion).
Curly braces
{}
should always be used after conditional statements or loops, even it's just a single line. It may avoid confusion in some scenarios.
if (getPath().size == 0){
return;
}
add a comment |
up vote
0
down vote
up vote
0
down vote
- Method
getXDifference
andgetYDifference
are very similar, you can convert this into a single method that accepts two variables. - you could also inline the return statement as the method name is sufficient to tell what it is returning.
Instead of calculating
pastX
andpastY
, you can write methods to return the computed value.
boolean isPastX(variables...){
// logic to calculate pastX and return it.
}
and the
if
statement will turn intoif( isPastX() && isPastY())
.
This will also help in short-circuiting the condition in case calculatingpastX
orpastY
is very expensive(it isn't in this example, just a suggestion).
Curly braces
{}
should always be used after conditional statements or loops, even it's just a single line. It may avoid confusion in some scenarios.
if (getPath().size == 0){
return;
}
- Method
getXDifference
andgetYDifference
are very similar, you can convert this into a single method that accepts two variables. - you could also inline the return statement as the method name is sufficient to tell what it is returning.
Instead of calculating
pastX
andpastY
, you can write methods to return the computed value.
boolean isPastX(variables...){
// logic to calculate pastX and return it.
}
and the
if
statement will turn intoif( isPastX() && isPastY())
.
This will also help in short-circuiting the condition in case calculatingpastX
orpastY
is very expensive(it isn't in this example, just a suggestion).
Curly braces
{}
should always be used after conditional statements or loops, even it's just a single line. It may avoid confusion in some scenarios.
if (getPath().size == 0){
return;
}
answered Jul 9 at 2:25
Ankit Soni
481111
481111
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f198103%2fpath-following-algorithm-for-entity%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown