Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java steering object with two wheel velocities

Tags:

java

math

physics

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).

  • If lWheelV = 1 & rWheelV = 1, the object moves forward
  • If lWheelV = -1 & rWheelV = 1, the object turns left (counter-clockwise)
  • If lWheelV = 0.5 & rWheelV = 1, the object will drive forward while slowly turning left
  • If lWheelV = -1 & rWheelV = -1, the object will move backward.

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.

like image 511
Conner Ruhl Avatar asked Jul 07 '26 19:07

Conner Ruhl


1 Answers

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;
    }
    
like image 121
Martijn Courteaux Avatar answered Jul 09 '26 09:07

Martijn Courteaux