I'm using Java to learn some movement algorithm. I would like to implement the "wandering" algorithm, but in order to do it, i should understand how to rotate an Object gradually.
Actually, my Player changes its orientation instantly (and too many times!) and it is not so good.
This is my code:
logic update
public void update(float delta) {
// WANDERING
// orientation as a vector
float orientation_x = (float)Math.sin(orientation);
float orientation_y = (float)Math.cos(orientation);
// update velocity
velocity_x = maxSpeed * orientation_x;
velocity_y = maxSpeed * orientation_y;
int max = 1;
int min = 0;
int random1 = (rand.nextInt((max - min) + 1) + min);
int random2 = (rand.nextInt((max - min) + 1) + min);
orientation = (random1 - random2) * maxRotation; // random binomial (i don't know which value i have to assign to maxRotation
System.out.println(orientation);
x += (velocity_x * delta);
if(x >= GamePanel.PWIDTH) {
x = 0.0f;
}
y += (velocity_y * delta);
if(y >= GamePanel.PHEIGHT) {
y = 0.0f;
}
}
draw
public void draw(Graphics dbg) {
// draws the player
Graphics2D g2d = (Graphics2D)dbg;
g2d.setColor(Color.RED);
AffineTransform old = g2d.getTransform();
g2d.rotate(orientation, x + SIZE/2, y + SIZE/2);
g2d.fillRect((int)x, (int)y, SIZE, SIZE);
g2d.setTransform(old);
}
With this code, the behavior that i get is horrible. The Player changes his direction too many times and instantly.
This is the pseudocode of the algorithm:
# Holds the static data for the character
character
# Holds the maximum speed the character can travel
maxSpeed
# Holds the maximum rotation speed we’d like, probably
# should be smaller than the maximum possible, to allow
# a leisurely change in direction
maxRotation
def getSteering():
# Create the structure for output
steering = new KinematicSteeringOutput()
# Get velocity from the vector form of the orientation
steering.velocity = maxSpeed *
character.orientation.asVector()
# Change our orientation randomly
steering.rotation = randomBinomial() * maxRotation
# Output the steering
return steering