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