具有两个车轮速度的Java转向对象

时间:2012-01-29 22:10:32

标签: java math physics

假设我有一个两轮物体,每个轮子都有一个独立的速度(分别为左右轮的lWheelV和rWheelV)。每个车轮的速度限制在[-1,1]范围内(即介于-1和1之间)。

  • 如果lWheelV = 1& rWheelV = 1,对象向前移动
  • 如果lWheelV = -1& rWheelV = 1,物体向左转(逆时针)
  • 如果lWheelV = 0.5& rWheelV = 1,物体将向前行驶,同时慢慢向左转
  • 如果lWheelV = -1& rWheelV = -1,对象将向后移动。

在下图中可能更容易想象:

object motion diagram

我需要用什么数学来描述这样一个对象,更重要的是我如何实现可以在Java中复制此行为的软件

1 个答案:

答案 0 :(得分:1)

这取决于车辆宽度,fps等等一大堆......

然而,一些提示:

  • 要在一帧中计算车辆的旋转,您可以使用arctan功能。

    float leftWheel = 1.0f;
    float rightWheel = 0.5f;
    float vehicleWidth = 1.0f;
    
    float diff = rightWheel - leftWheel;
    float rotation = (float) Math.atan2(diff, vehicleWidth);
    
  • 要确定车辆沿其轴移动的速度,请使用:

    float speedAlongAxis = leftWheel + rightWheel;
    speedAlongAxis *= 0.5f;
    
  • 按照第一个提示中计算的角度旋转车辆的轴:

    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));
    
  • 将车辆移过轴:

    float vehicleX = ...;
    float vehicleY = ...;
    
    vehicleX += axisX * speedAlongAxis;
    vehicleY += axisY * speedAlongAxis;
    
  • normalise()方法如下所示:

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