Conner Ruhl
Conner Ruhl

Reputation: 1723

Java steering object with two wheel velocities

Let's say I have a two wheeled object, where each wheel has an independent velocity (lWheelV and rWheelV for the left and right hand wheels respectively). The velocities of each of the wheels are limited to the range [-1, 1] (ie. between -1 and 1).

This may be easier to visualise in the following image:

object motion diagram

What mathematics do I need to describe such an object, and more importantly how could I implement software that would replicate this behavior in Java.

Upvotes: 3

Views: 926

Answers (1)

Martijn Courteaux
Martijn Courteaux

Reputation: 68907

It depends on really a whole bunch of things like vehicle width, fps, etc...

However, some tips:

  • To calculate the rotation for the vehicle in one frame, you can use the arctan function.

    float leftWheel = 1.0f;
    float rightWheel = 0.5f;
    float vehicleWidth = 1.0f;
    
    float diff = rightWheel - leftWheel;
    float rotation = (float) Math.atan2(diff, vehicleWidth);
    
  • To determine the speed the vehicle is going to move along its axis, use this:

    float speedAlongAxis = leftWheel + rightWheel;
    speedAlongAxis *= 0.5f;
    
  • To rotate the axis of the vehicle by the angle computed in the first tip:

    float axisX = ...;
    float axisY = ...;
    /* Make sure that the length of the vector (axisX, axisY) is 1 (which is 
     * called 'normalised')
     */
    
    float x = axisX;
    float y = axisY;
    
    axisX = (float) (x * Math.cos(rotation) - y * Math.sin(rotation));
    axisY = (float) (x * Math.sin(rotation) + y * Math.cos(rotation));
    
  • To move the vehicle over the axis:

    float vehicleX = ...;
    float vehicleY = ...;
    
    vehicleX += axisX * speedAlongAxis;
    vehicleY += axisY * speedAlongAxis;
    
  • A normalise() method looks like this:

    public float normalise()
    {
        float len = (float) Math.sqrt(x * x + y * y);
        x /= len;
        y /= len;
        return len;
    }
    

Upvotes: 1

Related Questions