3

我正在制作一个自上而下的射击游戏,玩家的枪偏离了物体的坐标。我正在使用 GameMaker:Studio,所以 x 和 y 坐标是对象的中心。图像的偏移设置在这里:

bullet_offset_x = 30;
bullet_offset_y = 28;

这是开枪的代码:

var xpos = x + (bullet_offset_x * cos(degtorad(direction))) - (bullet_offset_y * sin(degtorad(direction)));
var ypos = y + (bullet_offset_x * sin(degtorad(direction))) + (bullet_offset_y * cos(degtorad(direction)));

var flash = instance_create(xpos, ypos, obj_flash);

with (flash){
    direction = other.direction;
    image_angle = other.direction;
}

我使用以下公式来放置枪口闪光灯:

x' = x cos(角度) - y sin(角度)

y' = x sin(角度) + y cos(角度)

所以:

xpos = x + x' 和 ypos = x + y'

但是,当我运行代码时,当角度为 0/360 时,枪口闪光灯正确定位,否则关闭。我计算错了吗?

图片:

正确的

正确的

不正确

不正确 不正确

4

2 回答 2

6

您需要使用lengthdir_xlengthdir_y功能,例如:

var xpos = x + lengthdir_x(offset_distance, offset_angle + image_angle); // or direction
var ypos = y + lengthdir_y(offset_distance, offset_angle + image_angle);

var flash = instance_create(xpos, ypos, obj_flash);

flash.direction = direction;
flash.image_angle = direction;

这里的小例子

要计算要代入公式的值,可以使用程序。最初它是用俄语制作的,但我已将其翻译成英文。我的英语很糟糕,但我希望你能理解。

upd: 偏移量示例:

var delta_x = 60;
var delta_y = -70;
var angle = point_direction(0, 0, delta_x, delta_y);
var distance = point_distance(0, 0, delta_x, delta_y);

var xpos = x + lengthdir_x(distance, image_angle + angle);
var ypos = y + lengthdir_y(distance, image_angle + angle);
var obj = instance_create(xpos, ypos, obj_flash);
obj.image_angle = image_angle;
于 2014-10-11T06:03:02.563 回答
0

当你的精灵的角度为 0 时,你的枪口闪光仍然invtan(28/30)与精灵的角度为 。因此,闪光灯相对于精灵旋转的角度可以由下式给出

flashRotation = spriteRotationDegrees - invtan(28/30) \\you can change this to radians

一旦找到,可以通过以下方式找到位置:

var x_pos = sprite_x_pos + Math.Sqrt(28^2 + 30^2)cos(flashRotation);
var y_pos = sprite_y_pos + Math.Sqrt(28^2 + 30^2)sin(flashRotation);

闪光灯的实际旋转角度(它指向的方向)将与精灵的角度相同。您可能需要使用 flashRotaion 方程,具体取决于哪种方式被视为正旋转。

于 2014-10-10T21:48:09.703 回答