I’ve carried out a mechanism to maneuver an object in the direction of a goal based mostly on https://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-seek–gamedev-849 the place it says
steering = nothing(); // the null vector, which means "zero drive magnitude"
steering = steering + search();
(...)
steering = truncate (steering, max_force)
steering = steering / mass
velocity = truncate (velocity + steering , max_speed)
place = place + velocity
This appears like
double maxSpeed = 5;
double maxForce = 10;
double mass = 1;
// search is the specified goal vector / straight in the direction of the goal
Vector2 search = Vector2(vacation spot.x - supply.x,
vacation spot.y - supply.y);
if (search.x == 0) {
search.y = maxSpeed;
} else if (search.y == 0) {
search.x = maxSpeed;
} else {
search.x = (search.x / distance) * maxSpeed;
search.y = (search.y / distance) * maxSpeed;
}
// calculate steering
Vector2 steering = Vector2(0, 0);
steering += search;
steering = truncate(steering, maxForce);
steering /= mass;
// calculate new velocity based mostly on previous velocity and steering
velocity = truncate(velocity + steering, maxSpeed);
place += velocity;
together with the truncate perform
truncate(Vector2 vi, double maxValue) {
Vector2 vo = Vector2(vi.x, vi.y);
if (vo.x > maxValue) {
vo.x = maxValue;
}
if (vo.y > maxValue) {
vo.y = maxValue;
}
return vo;
}
Essential to note right here is that this code is being run inside the sport loop being up to date on each tick. The velocity
Vector is a member of the category thus my concept is to make use of the final path/vector, calculate the brand new path (search) and implement the steering to get one thing in-between.
With out the steering, this works fantastic however as quickly as I allow the steering, the habits is that the objects are transferring zig zag.