SOLVED How can i limit a x and y speed that has separate accelerations

Perg

Member
i' have this code
GML:
//player movement
//accel
image_angle = (image_angle + 360) mod 360;
xaccel = ((image_angle mod 180)-90)/90*acceleration;
var ya = image_angle + 90
ya = (ya + 360) mod 360
yaccel = ((ya mod 180)-90)/90*acceleration;
if image_angle < 180 {
    xaccel = -xaccel;
}
if ya < 180 {
    yaccel = -yaccel;
}
//speed
xspeed += xaccel;
yspeed += yaccel;
x += xspeed;
y += yspeed;
basically what it does is pick the image angle and decompose it in a x and y acceleration, the acceleration is added to the speed and there we got a cool looking ship that slides gracefully through the room. after many tries though i can't limit the speed without messing up with the movement, the better i have got was something like
GML:
if spd > speedlimit {
    xspeed -= sign(xspeed)/something
    yspeed -= sign(yspeed)/something
}
else {
    xspeed += xaccel
    yspeed += yaccel
}
but i couldn't get anything better than a barely working speed limiter full of glitching.
any help should help ( i'm quite new to programing )
 

Nidoking

Member
Well, I learned it in high school, so that answer is completely meaningless. If you know about sines and cosines and how they relate to right triangles, then you can use those to work out the correct x and y components of your motion. If not, then lengthdir_x and lengthdir_y will do about the same thing. Remember that if you're working in degrees, the sine and cosine functions are dsin and dcos.
 

Perg

Member
GML:
//player movement
//accel
xaccel = acceleration*dcos(image_angle)
yaccel = acceleration*-dsin(image_angle)
//speed
xspeed += xaccel;
yspeed += yaccel;
x += xspeed;
y += yspeed;
simplified the acceleration, (thanks to nidoking)
 

Perg

Member
GML:
xval = abs(xspeed) / abs(yspeed)
yval = abs(yspeed) / abs(xspeed)
xval = clamp(xval, 0, 1)
yval = clamp(yval, 0, 1)
xspeed = clamp(xspeed, -(speedlimit*xval), speedlimit*xval)
yspeed = clamp(yspeed, -(speedlimit*yval), speedlimit*yval)
after being dank by about 4 days this seems to work
 
Top