1

编辑:我在 Decker 的链接(矢量移动演示)中深入研究了文档非常完善的源代码,并且我相当有信心可以解决其中的一些代码。谢谢大家的帮助:D

我正在为 javascript 中的游戏开发运动。左右箭头键旋转宇宙飞船的图像,而向上箭头则使其加速。使用旋转度数和速度,我可以用x和和来计算运动,但这意味着船像汽车一样操控。鉴于它应该在太空中,我想让船的旋转仅在加速时影响它的路径,并考虑到船的当前运动。yMath.sin()Math.cos()

我把它弄乱了很多,并尝试将运动分成两个独立的力量,当前方向和速度以及所需的方向,但似乎没有什么比它应该的感觉更接近。

抱歉,如果这令人困惑,这里是原始运动的简化代码:

function main()
{
if(keyStates[39]) // Right arrow pressed?
    ship.deg+=8; 
if(keyStates[37]) // Left arrow pressed? 
    ship.deg-=8; 
if(keyStates[38]) // Up arrow pressed?
{
    if(ship.speed<16)
        ship.speed+=1;
}
var shift=getXYshift(ship.deg,ship.speed);
function getXYshift(deg,speed)
{
    return {x:Math.round(Math.cos((90-deg)*Math.PI/180)*speed*-1), y:Math.round(Math.sin((90-deg)*Math.PI/180)*speed)};
}
setTimeout(function(){ main() }, 50);
}
4

4 回答 4

3

You can use one Vector to keep track of the ships speed and direction and alter the direction of that vector when the up arrow is pressed by checking the angle of a second Vector used to keep track of the ships current angle.

I recommend getting this book Supercharged Javascript Graphics which explains in detail the use of vectors and much more.

You can also view the source code for one of the books examples here at the authors website which has a vector handling object that could prove useful to you.

于 2012-07-18T23:52:23.587 回答
2

You should be using a mathematical concept known as a "vector" for your movement. A vector is simply a force and a direction. This vector will be applied to the X,Y coordinate of your ship (ignoring its direction) every frame when determining where to draw the ship. When you accelerate you will use the direction the ship is facing and a constant value assigned to acceleration to form a vector that can be applied to your movement vector for calculating its effect on velocity.

Here is a quick introduction to vectors: http://www.khanacademy.org/science/physics/v/introduction-to-vectors-and-scalars , after watching the video you should have a good idea about what need to be looking for. From there Google should be your friend.

EDIT: Above when I said: "you will use the direction the ship is facing and a constant value assigned to acceleration to form a vector that can be applied to your movement vector for calculating its effect on velocity" I was referring to vector algebra. If you decide to use vectors to solve your problem, you will need to use the concept of vector addition to accelerate. When you press the arrow key, you will generate a vector of magnitude m (where m can be any real number indicating how fast you want to accelerate) and a direction d (more than likely this value will correspond with the direction the ship is facing). You will then add this new vector to the ships current vector to get the ships new vector after the acceleration for the current frame is applied. You can read more here: http://emweb.unl.edu/Math/mathweb/vectors/vectors.html

Cheers and happy Coding

于 2012-07-18T23:52:34.773 回答
2

根据我的低物理知识:

船有一个方向的速度。这可以表示为来自您空间的向量,例如 X 轴上的 x 像素,Y 轴上的 y 像素(可能还有更多维度)每秒。

然后它有一个旋转速度,比如每秒逆时针 α 度。

要计算你的船一秒钟的行程,只需将速度矢量添加到坐标中。并将旋转添加到当前方向。

要根据旋转和加速度更改速度矢量,您将在当前方向的方向上构建一个长度与加速度相关的矢量。然后将加速度矢量添加到速度矢量。

伪代码:

ship = {
   coordinates: [0, 0], // space units
   orientation: 0, // radiant
   speed: [0, 0], // space units / time frame
   rotation: 0 // radiant / time fram
}
function animatestep {
   coordinates[0] += speed[0];
   coordinates[1] += speed[1];
   orientation += rotation;
}
onaccelerate = function {
   speed[0] += cos(orientation) * acceleration;
   speed[1] += sin(orientation) * acceleration;
}
onleft = function {
   rotation++;
}
onright = function {
   rotation--;
}

请注意,这使得太空船的行为真的像太空船,因为旋转可能很难停止。您可能需要允许逐步设置船的方向,而不是使用旋转速度 :-) 您还可以设置旋转和加速度的限制(否则船会爆裂)并使用最大速度(如光速,包括不同速度的加法)。

于 2012-07-19T00:04:11.240 回答
1

您在按右/左箭头后使用 getXYshift 吗?您应该只在按下向上箭头时使用它。这样,您的船将在不向任何方向加速的情况下旋转。仅在按下向上箭头键时更改速度,不按下时减速。不要用左右键改变速度,用它们来改变你的船的旋转。

于 2012-07-18T23:32:17.653 回答