【问题标题】:Y Offset, Tracks on a tank - TrigonometryY 偏移,坦克上的轨道 - 三角学
【发布时间】:2012-04-22 13:43:46
【问题描述】:

坦克履带需要在坦克的任一侧,坦克可以面对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))
    );

【问题讨论】:

  • 你想完成什么?您是否尝试在相对于罐体的适当位置绘制轨道?断了怎么办?
  • 是的,我试图保持轨道不变,atm 他们只是沿着物体水平偏移。
  • 正确轨道的代码在哪里?
  • 左边的我修好了,右边的就不那么难了:P
  • 我已经发布了答案,但我还不能回答我自己的问题。

标签: java android graphics trigonometry


【解决方案1】:

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

  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)。相同的旋转矩阵会正确旋转它们。这将使它们围绕原点旋转。不是你想要的,但它是一个开始。

然后要获得 x-y 轴的平移,您只需将 X 和 Y 坐标添加到水箱的整体 X-Y 坐标中。

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

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

【讨论】:

  • 我是矩阵来旋转和缩放我的对象未来。它们不会相互影响吗?
【解决方案2】:

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

    //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))
    );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    相关资源
    最近更新 更多