Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Sign in to follow this  
Blastfrog

I suck at code

Recommended Posts

Wondering how I can have a number "chase" another number at a fixed speed, but NOT overshoot if the difference is less than that fixed amount per tic.

There's a target and a chaser. The target is the joystick position, the chaser is the actual speed you turn ranging from -1.0 to 1.0. The idea is that the chaser accelerates at two different speeds, the acceleration speed is 0.065 per tic, and deceleration is 0.13 per tic. What I want is if the difference is less than that, to not overshoot but instead set the value right to the target.

This is my current mess:

	if(((joyacc < 0)&&(joyaxes[axis_turn] > 0)||(joyacc > 0)&&(joyaxes[axis_turn] < 0))||
	((joyacc > joyaxes[axis_turn])&&(joyacc > 0)||(joyacc < joyaxes[axis_turn])&&(joyacc < 0)))
	{ // Decelerate
		if(joyacc < joyaxes[axis_turn])
			joyacc += 0.13;
		else if(joyacc > joyaxes[axis_turn])
			joyacc -= 0.13;
	}
	else
	{ // Accelerate
		if(joyacc < joyaxes[axis_turn])
			joyacc += 0.065;
		else if(joyacc > joyaxes[axis_turn])
			joyacc -= 0.065;
	}

	if((joyaxes[axis_turn] == 0)&&((joyacc < 0.066)&&(joyacc > -0.066)))
		joyacc = 0.0;

Share this post


Link to post

This code doesn't include any use of the joyacc variable to actually set the joyaxes (?) variable. So any hard limit must be placed when/where that usage occurs.

Share this post


Link to post

Can you please sign into Google Hangouts? Might be faster to communicate that way

What sets the joyaxes variable is the joystick itself. joyacc is used directly for how much to turn.

Share this post


Link to post

Sounds like you're basically describing angular momentum. Probably your best approach is to take a biased average of the current momentum and the target, eg. each tic do something like

xmom = xmom * (1 - theta) + xpos * theta;
Where xmom is momentum you should use for your actual turning amount, xpos is joystick position, theta is a value between 0-1.0 indicating "stiffness" (how quickly momentum follows the target position)

Share this post


Link to post

Pseudocode because I don't know what you're actually working with:

 //use deceleration rate by default
float increment = 0.13;

 //if input is further from the center of axis than current speed,
 // and both are on the same side of center, use acceleration rate
bool sameside = false;
sameside = (joyacc > 0 && joyaxes[axis_turn] > 0) || (joyacc < 0 && joyaxes[axis_turn] < 0);
if (abs(joyaxes[axis_turn]) > abs(joyacc) && sameside) increment = 0.065;

 //How far from current rate is the input?
float delta = 0;
delta = joyaxes[axis_turn] - joyacc;

 //Limit adjustment to +/- 0.065 or 0.13
if (delta > increment) delta = increment;
if (delta < -increment) delta = -increment;

joyacc += delta;

Share this post


Link to post
fraggle said:

Sounds like you're basically describing angular momentum. Probably your best approach is to take a biased average of the current momentum and the target

While that could work, I wanted it to feel similar to the math behind moving left and right in Mario or Sonic, which is much simpler, merely adding to/subtracting from a momentum value.

BrainMuncher said:

Pseudocode because I don't know what you're actually working with

That actually worked perfectly, thanks! I've credited you in the source code.

Share this post


Link to post
BrainMuncher said:

What is the project you're working on?

A total conversion called Zed's Quest. Check your private messages for a copy of it.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
×