Legacy GM How to change direction for a physics object?

B

Bhreno

Guest
Hello. I would like to know how do I change the direction of a physical object in GMS1.4 without using the phy_speed_x & phy_speed_y variables (which are already being used to a previous random value). It's possible?
For example, the object to be created may follow any direction, however its speed has already been set by phy_speed_x & phy_speed_y, and I wanted to change just his direction, without changing the speed.
 
Do you mean that you want to instantaneously change to a different direction, but keep the same speed?

Apparently, there's no built-in variable that measures physics speed in pixels per step, so you'll need to measure it yourself. sqrt(phy_speed_x*phy_speed_x + phy_speed_y*phy_speed_y)

And if you have an angle you want to move in, then you can use lengthdir_x and lengthdir_y to set new phy_speed_x and phy_speed_y values using that angle, and the speed measured above.

What's with the weird picture uploads?
 
B

Bhreno

Guest
I will explain further:

My obj_player should have random speed and direction every time it is created.
speed_player = irandom(7); 8 different speeds;
direction_player = irandom(360); all possible different directions;

The speed I could set with phy_speed_x & phy_speed_y, and it worked perfectly.

The direction, whenever I try to set the random value, it ends up interfering with the speed and the object goes crazy through the room.
 

FrostyCat

Redemption Seeker
How exactly did you set phy_speed_x and phy_speed_y? If you did it properly, both the speed and the direction should work straight out of the box.
Code:
speed_player = irandom(7);
direction_player = irandom(360);
phy_speed_x = lengthdir_x(speed_player, direction_player);
phy_speed_y = lengthdir_y(speed_player, direction_player);
 
B

Bhreno

Guest
I set the speed variables as follows:

In Create Event:

Code:
speed_ball = irandom(4);

switch (speed_ball)
{
case 0: speed_ball = 3; break;
case 1: speed_ball = 4; break;
case 2: speed_ball = 5; break;
case 3: speed_ball = 6; break;
case 4: speed_ball = 7; break;
}


phy_speed_x = speed_ball;
phy_speed_y = speed_ball;
In Step Event:

Code:
//speed min x & y
if (phy_speed_x) > (-2) && (phy_speed_x) < 2 && (phy_speed_y) > (-2) && (phy_speed_y) < 2
{
if (phy_speed_x) < 0
{
phy_speed_x = -2;
}
else
if (phy_speed_x) > 0 or (phy_speed_x) == 0
{
phy_speed_x = 2;
}
else
if (phy_speed_y) < 0
{
phy_speed_y = -2;
}
else
if (phy_speed_y) > 0 or (phy_speed_y) == 0
{
phy_speed_y = 2;
}
}

else
//speed max x & y
if (phy_speed_x) < (-10) && (phy_speed_x) > 10 && (phy_speed_y) < (-10) && (phy_speed_y) > 10
{
if (phy_speed_x) < (-10)
{
phy_speed_x = -10;
}
else
if (phy_speed_x) > 10
{
phy_speed_x = 10;
}
else
if (phy_speed_y) < (-10)
{
phy_speed_y = -10;
}
else
if (phy_speed_y) > 10
{
phy_speed_y = 10;
}
}
 
B

Bhreno

Guest
How exactly did you set phy_speed_x and phy_speed_y? If you did it properly, both the speed and the direction should work straight out of the box.
Code:
speed_player = irandom(7);
direction_player = irandom(360);
phy_speed_x = lengthdir_x(speed_player, direction_player);
phy_speed_y = lengthdir_y(speed_player, direction_player);
If I use this code, the speed goes out of control.
 
Last edited by a moderator:
Top