Cis*_*ran 4 c# angle game-physics bresenham
我正在尝试制作游戏小行星.我现在的问题是,如果按下向上箭头键,它会将"船"向上移动10个像素.如果你按下LEFT箭头键,它会将"船"向左转5度,当你向左转或向右转时我就会发挥作用.然后尝试向上移动.它不会进入转向的方向.它只会将转向的"船"向Y方向移动10度.
What I am thinking of doing is having a variable called direction, think of this variable as a circle with 360 degrees. What I am trying to do is everytime I hit the Left Arrow, it will subtract 5 from direction, which started at 0 and goes backwards from 360 and thus set it to 355. I would then divide 355 by 10 and get 35.5. Then I would divide 10 by 35.5 and get .355. I would then subtract .355 from 10 (moving up in the Y). And subtract it from 0 (moving left in the X). So I would be moving 9.645 up in the Y and moving 0.355 left in the X.
The issue I'm having is the "ship" in Asteroids I have is a Graphics.FillPie, which needs Ints to use as the Start Angle and Sweep angle, However as you can see I would have to be working with doubles or floats. I'm pretty sure I'm overcomplicating this and am pretty sure there is something 100 times simpler, I'm thinking something along the lines of Bresenham's Line Algorithm. If anyone could help by suggesting something easier or a fix to my problem, it would be much appreciated. Thanks in advance.
Pet*_*ter 13
听起来您需要根据其当前的弧度方向计算船舶的航向矢量.例如:
double xVector = Math.Sin(orientationInRadians);
double yVector = Math.Cos(orientationInRadians);
Run Code Online (Sandbox Code Playgroud)
然后使用左右箭头控制船舶的当前方向.获得矢量后,您可以计算船舶的位置,该位置与航向矢量的当前位置相距10个单位.首先,您需要将矢量缩放到单位(长度为1)的矢量.
double magnitude = sqrt(xVector*xVector + yVector*yVector);
double unitVectorX = xVector/magnitude;
double unitVectorY = yVector/magnitude;
Run Code Online (Sandbox Code Playgroud)
现在你有一个长度为1个单位但仍指向船舶当前方向的矢量.接下来将船舶的positionX和positionY作为其当前坐标.
double distanceToTravel = 10;
double newPositionX = positionX + unitVectorX*distanceToTravel;
double newPositionY = positionY + unitVectorY*distanceToTravel;
Run Code Online (Sandbox Code Playgroud)
即使您最终使用仅允许整数的系统进行渲染,您也希望使用双精度或浮点数来跟踪船舶的位置和方向.您不应使用渲染引擎的精度存储当前位置和方向,否则您将在移动和旋转时开始看到奇怪的跳跃和暂停.此外,使用上述方法可以使distanceToTravel非常量(假设在按下按键时加速,而不是从按键移动固定距离).
| 归档时间: |
|
| 查看次数: |
2788 次 |
| 最近记录: |