在枪管处产生子弹

ter*_*pak 2 rotation game-maker

我正在制作一个自上而下的射击游戏,并且玩家的枪支偏离了对象的坐标。我正在使用GameMaker:Studio,因此x和y坐标是对象的中心。图像的偏移量在此处设置:

bullet_offset_x = 30;
bullet_offset_y = 28;
Run Code Online (Sandbox Code Playgroud)

这是枪支射击的代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

因此:

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

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

图片:

正确

正确

不正确的

不正确的 不正确的

Dmi*_*7ry 5

您需要使用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;
Run Code Online (Sandbox Code Playgroud)

这里的小例子

要计算要替换为公式的值,可以使用程序。它最初是用俄语制作的,但我已将其翻译成英文。我的英语太糟糕了,但我希望你能听得懂。

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;
Run Code Online (Sandbox Code Playgroud)