2

坦克履带需要在坦克的任一侧,坦克可以面向360度旋转。这是一张图片来解释这一点:

http://imgur.com/ySFTk

坦克履带目前仅水平偏移,我试图沿坦克核心垂直偏移它们(不工作)。

这是我到目前为止所做的:

private void tracksPosition()
{
    _DegreeToRadien = Math.toRadians(_degrees);

    _ObjectXCenter = (int) (_object_x + ((_itemAnimation.getWidth() / 2)) - _trackAnimationLeft.getWidth() / 2);
    _ObjectYCenter = (int) (_object_y + ((_itemAnimation.getHeight() / 2)) - _trackAnimationLeft.getHeight() / 2);

    //For left track
    _xOffset = -1 * (_itemAnimation.getHeight() / 2);

    _trackLeftPosition.set
    (
        (int)(((_xOffset) * Math.cos(_DegreeToRadien / 2)) + _ObjectXCenter),
        (int)(((_xOffset) * Math.sin(_DegreeToRadien / 2)) + _ObjectYCenter)
    );

它适用于 X 偏移,但由于某种原因,我无法在不奇怪的情况下计算出 Y 偏移。

//------- 答案 ----------// 对于所有想知道我是如何做到这一点的人,答案是:

    //For left track

    //Decide how far away the track is from the tank
    _xOffset = _itemAnimation.getHeight() / 1.5;

    //Decide where the track is horizontally to the tank (Ie front, back)
    _DegreeToRadien = Math.toRadians(_degrees + 110);

    //Set the point of the track, takes the centre of the tank and adds the current position, cos and sin basically divide (though multiplication) the current position according to the direction the tank is facing.
    _trackLeftPosition.set
    (
        _ObjectXCenter + (int)(_xOffset * Math.cos(_DegreeToRadien))
        ,
        _ObjectYCenter + (int)(_xOffset * Math.sin(_DegreeToRadien))
    );
4

2 回答 2

3

我需要更多信息来提供帮助,但是:

  1. 您可能可以通过使用一些矩阵数学来更系统地做您想做的事情。
  2. 注释代码中的每个变量。有时这可以帮助我在事情变得抽象时发现错误。

编辑:

此链接显示如何围绕原点旋转一个点。 http://en.wikipedia.org/wiki/Rotation_matrix

如果您将坦克的组件表示为相对于原点的一系列顶点,您可以系统地对每个点应用旋转。然后在这些点之间画线以形成旋转的形状是一件小事。例如,如果您的坦克是方形的,您可以确定它的顶点位于 (1,1)、(-1,1)、(-1,-1) 和 (1, -1)。您的轨道会相似,但左侧轨道可能是 (-1, 1.25), (-1.25, 1.25), (-1.25, -1.25), (-1, -1.25)。相同的旋转矩阵会正确旋转它们。这将使它们围绕原点旋转。不是你想要的,但这是一个开始。

然后要在 xy 轴上进行平移,只需将 X 和 Y 坐标添加到水箱的整体 XY 坐标中。

我没有时间刷新我的记忆,但是一个稍微大一点的矩阵也可以做翻译。所以基坐标、旋转和期望的 (x,y) 都进去了,最后的点坐标出来了。

这可能看起来更复杂,但您的代码会更小,更不容易出错。

于 2012-04-22T14:12:38.693 回答
0

对于所有想知道我是如何做到这一点的人来说,答案是:

    //For left track

    //Decide how far away the track is from the tank
    _xOffset = _itemAnimation.getHeight() / 1.5;

    //Decide where the track is horizontally to the tank (Ie front, back)
    _DegreeToRadien = Math.toRadians(_degrees + 110);

    //Set the point of the track, takes the centre of the tank and adds the current position, cos and sin basically divide (though multiplication) the current position according to the direction the tank is facing.
    _trackLeftPosition.set
    (
        _ObjectXCenter + (int)(_xOffset * Math.cos(_DegreeToRadien))
        ,
        _ObjectYCenter + (int)(_xOffset * Math.sin(_DegreeToRadien))
    );
于 2012-04-23T19:01:13.810 回答