Legacy GM Bullet - how to set impulse speed - physics

P

PvdH

Guest
Hello, i have small problem with speed of bullets in my game. I have a game where player is shooting bullets towards mouse - everything works fine except i can't change bullet speed.

Looks like my bullet is faster if i click when my mouse is far away from shooting object,
if my mouse is closer then speed is lower.

Phy_speed is read only, i know i can change probably phy_speed_x and phy_speed_y?
The problem is when i have to set the speed in other direction then vertical or horizontal...

My code:

Code:
/// scr_attack_state

var ldx = lengthdir_x(0, mouse_x);
var ldy = lengthdir_y(0, mouse_y);

bullet = instance_create(x+ldx, y+ldy, obj_bullet);

// bullet = instance_create(x, y, obj_bullet);
with(bullet)
{
    
    physics_apply_impulse(x,y, (mouse_x - x) * 1,-(y - mouse_y) * 1);

}
 

CMAllen

Member
You know how fast you want your bullet to move and you know the direction you want the bullet to move in. You just need to apply that speed along the proper vector of x and y. To do this, you can use the lengthdir_x and lengthdir_y functions to get the proper forces to apply along the x and y axis.
Code:
impulsex = lengthdir_x(shot_dir, shot_speed)
impulsey = lengthdir_y(shot_dir, shot_speed)
phy_speed_x = impulsex;
phy_speed_y = impulsey;
 
This method sets the bullet's rotation to point at the target and uses local impulse and clamps to maintain constant speed.
Bullet object create code -
Code:
phy_rotation = -point_direction(x,y,device_mouse_x(0),device_mouse_y(0));
physics_apply_local_impulse(0,0,0.05,0)
Bullet object step event -
Code:
phy_speed_x = clamp(phy_speed_x,-0.05,0.05)
phy_speed_y = clamp(phy_speed_y,-0.05,0.05)
physics_apply_local_impulse(0,0,0.05,0)
 
Top