[英]Tangent to the circle
I'm writing curve snake game like this http://www.gamekb.com/thumbs_v2/01872/1872657-85play-curve-fever-2.gif in Java. 我正在用Java这样编写曲线蛇游戏http://www.gamekb.com/thumbs_v2/01872/1872657-85play-curve-fever-2.gif 。 If I want to move under specific angle it:
如果要在特定角度下移动:
posX += cos(angle);
posY += sin(angle);
When I press Left or Right it calculates middles of rotating (for left and for right) and then 当我按“向左”或“向右”时,它会计算旋转的中间点(对于左和右),然后
posX = middleX + sin(angle) * radius;
posY = middleY + cos(angle) * radius;
angle += PI/180;
And if I release the key it should calculate new angle: 如果我松开钥匙,它应该计算新角度:
vectorx = middleX - posX; /*normal vector to tangent*/
vectory = middleY - posY;
k = -(vectorx/vectory); /*directiv of tangent. Should be angle in radians but not
in all cases */
angle = k;
This piece of code means I calculate vector[vectorx, vectory] that is perpendicular to my new straight with angle I am calculating and k
is from parametric form of straight and it is angle in radians, basically I'm trying to make a tangent to the circle in point where I release the arrow. 这段代码意味着我计算的向量[vectorx,vectory]垂直于我正在计算的角度的新直线,而
k
来自直线的参数形式,并且是弧度的角度,基本上,我试图与释放箭头时的圆。
You haven't specified what angle
represents when the snake is turning. 您尚未指定蛇转弯时代表的
angle
。 It could be trivial to calculate the new angle just using old one, but here is a way using the tangent: 仅使用旧角度来计算新角度可能是微不足道的,但这是使用切线的一种方法:
vectorX = posX - middleX;
vectorY = posY - middleY;
tangentX = -vectorY; // flip the signs of these two for
tangentY = +vectorX; // turning in the other direction
angle = atan2(tangentY, tangentX)
An alternative way is to keep the meaning of angle
as the snake's direction and forget about the centre of the circle and the tangent. 另一种方法是保持
angle
的含义作为蛇的方向,而忽略圆心和切线。 When the snake turns, 1 / radius
is added or subtracted from angle
and the position is still calculated using posX += cos(angle), posY += sin(angle)
. 当蛇转弯时,将
1 / radius
添加或减去angle
并且仍然使用posX += cos(angle), posY += sin(angle)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.