首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Y偏移,坦克上的轨迹-三角

Y偏移,坦克上的轨迹-三角
EN

Stack Overflow用户
提问于 2012-04-22 21:43:47
回答 2查看 160关注 0票数 2

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

http://imgur.com/ySFTk

坦克履带目前只是水平偏移,我正试着沿着坦克的核心垂直偏移它们(不起作用)。

以下是我到目前为止所做的工作:

代码语言:javascript
复制
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偏移量,否则它会变得奇怪。

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

代码语言:javascript
复制
    //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))
    );
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-24 03:01:14

对于那些想知道我是如何做到这一点的人,这里有一个答案:

代码语言:javascript
复制
    //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))
    );
票数 0
EN

Stack Overflow用户

发布于 2012-04-22 22:12:39

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

  1. ,你可以通过使用一些矩阵数学,更系统地做你想做的事情,

  1. ,注释代码中的每个变量。有时,这可以帮助我在事物变得抽象时找到错误。

编辑:

此链接显示如何绕原点旋转点。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),最后的点坐标就出来了。

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10268425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档