3
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Orientation is a vector, so to rotate it all you have to do is apply basic vector math.

First of all, your vector's components are swapped. Sin is for Y component and cos for X component. So your code ends up as:

 float orientation_x = (float) Math.cos(orientation);
 float orientation_y = (float) Math.sin(orientation);

Your object changes direction instantly because each update tick you are choosing a random variable between a range as the direction instead of allowing a smooth transition of the orientation angle.

A simple solution to your problem could be adding a small angle each tick (either positive or negative with respect to your orientation angle). This angle "differential" depends on where your object is trying to steer.. You could even add a steering acceleration to provide an even smoother rotation.

\$\endgroup\$
0
\$\begingroup\$

It seems to me like what you want is a slerp function, see: http://en.wikipedia.org/wiki/Slerp

\$\endgroup\$
  • \$\begingroup\$ Uhm, is something much simpler i think. I have to change the orientation gradually according to a fixed float rotation variable. But i don't know how xD \$\endgroup\$ – Loris Mar 5 '15 at 13:39
  • \$\begingroup\$ That is exactly what a slerp or lerp does, you probably just need a LERP. It returns a value from 'start' to 'end' at '0..1' time to make the change gradual. That's as simple as it gets! The 0..1 is determined at each frame you draw based on your timer and how slow you want the change to be. \$\endgroup\$ – Patrick Hughes Oct 4 '15 at 1:45
0
\$\begingroup\$

One method you could use is instead of assigning a random rotation to orientantion, you should increment or decrement orientation by a small random amount. Pseudocode:

orientation += (randomFloat() - 0.5) * VARIANCE
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.