Speed limit

Posted By: theDust

Speed limit - 08/17/08 14:01

Hi, i have 2 variables: player_speed.x and player_speed.y
How can i limit these variables ? They should not going over a specific value .For example 100 (and -100 for the negativ movement). Any ideas ?
Posted By: cro_games

Re: Speed limit - 08/17/08 14:30

Try this:
player_speed.x = minv(100,(maxv(player_speed.x, -100))) // player_speed.x will be limited between -100 and 100

player_speed.y = minv(100,(maxv(player_speed.y, -100))) // player_speed.y will be limited between -100 and 100


Posted By: Michael_Schwarz

Re: Speed limit - 08/17/08 15:13

player_speed.x = clamp(player_speed.x, -100, 100);
player_speed.y = clamp(player_speed.y, -100, 100);
Posted By: theDust

Re: Speed limit - 08/17/08 18:37

This two codes are working but my player is moving a bit in the wrong direction now. Really weird...I'll try to fix this, any idea is welcome smile

btw I move my player ( a ball) with this code:
phent_addtorqueglobal (ball, player_speed);
Posted By: cro_games

Re: Speed limit - 08/17/08 19:21

Than use this:
Code:
player_speed = minv(100,(maxv(player_speed, -100))) // player_speed will be limited between -100 and 100

or
Code:
player_speed = clamp(player_speed, -100, 100);

Posted By: theDust

Re: Speed limit - 08/17/08 19:59

Hmmm, it says error:

"Can not convert `struct VECTOR` to `Fixed`
player_speed = clamp(player_speed, -100, 100);

"Can not convert `Fixed` to `struct VECTOR`
player_speed = clamp(player_speed, -100, 100);


My, player_speed is a vector, maybe this is the problem ?
Posted By: MrGuest

Re: Speed limit - 08/17/08 20:31

post your code, it's easier to highlight the problem smile
Posted By: cro_games

Re: Speed limit - 08/17/08 21:06

You use physics,add this to the code:

phent_setdamping(ball_entity,70,70);
Posted By: theDust

Re: Speed limit - 08/18/08 10:19

Here the action of my player:

action spieler()
{
zeiger = my;
my.emask |= (ENABLE_BLOCK | ENABLE_ENTITY);
phent_settype (zeiger, PH_RIGID, PH_SPHERE);
phent_setmass (zeiger, 1,PH_SPHERE);
phent_setfriction (zeiger, 500);
phent_setdamping (zeiger, 10, 50);
phent_setelasticity (zeiger, 50, 100);


while (1)
{
jump.x = 0; jump.y = 0; jump.z = 30000;
my.event = jump2;
winkel = cam_angle + 270;
if(key_cur)
cam_angle -= 3*time_step;
if(key_cul)
cam_angle += 3*time_step;

if(key_cuu)
{
player_speed.x -= cos (winkel) * 3 *time_step;
player_speed.y -= sin (winkel) * 3 *time_step;
}

if(key_cud)
{
player_speed.x += cos (winkel) * 3 *time_step;
player_speed.y += sin (winkel) * 3 *time_step;
}

if(key_cud != 1 && key_cuu != 1)
{
player_speed.x = 0;
player_speed.y = 0;
}
cam();
phent_addtorqueglobal (zeiger, player_speed);
wait (1);
}
}


player_speed is a vector (i have written VECTOR player_speed; in my Code). As you can see i always have used phent_setdamping, that doesn't help.
Posted By: cro_games

Re: Speed limit - 08/18/08 17:52

if(key_cuu)&&(player_speed.x > -100)
{
player_speed.x -= cos (winkel) * 3 *time_step;
player_speed.y -= sin (winkel) * 3 *time_step;
}

if(key_cud)&&(player_speed.x < 100)
{
player_speed.x += cos (winkel) * 3 *time_step;
player_speed.y += sin (winkel) * 3 *time_step;
}
Posted By: theDust

Re: Speed limit - 08/18/08 21:25

Nice solution, thx smile
I have changed it a bit and it works perfectly.
Posted By: Fenriswolf

Re: Speed limit - 08/18/08 21:54

And for clamping the vectors magnitude between SPEED_MIN and SPEED_MAX:
Code:
vec_normalize(player_speed, clamp(vec_length(player_speed), SPEED_MIN, SPEED_MAX));

Posted By: cro_games

Re: Speed limit - 08/18/08 22:37

Originally Posted By: theDust
Nice solution, thx smile
I have changed it a bit and it works perfectly.


You have ask for +-100 wink
Np man..next time just show us the code..
© 2024 lite-C Forums