【发布时间】:2012-04-22 13:43:46
【问题描述】:
坦克履带需要在坦克的任一侧,坦克可以面对360度旋转。这是一张图片来解释这一点:
坦克履带目前只是水平偏移,我试图沿着坦克核心垂直偏移它们(不起作用)。
这是我到目前为止所做的:
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