Advertisement

Not able to move my object in the reverse direction of the x coordinate

Started by September 28, 2014 10:56 AM
6 comments, last by Kaptein 10 years, 4 months ago

I am a beginner in games development and using c++ and opengl to work around with vector class developed by me to move an object.
i have successfully managed to move my object on the positive x direction but i want my object to go in the reverse direction after reaching a defined value. in my project that value is 1.0. here is a piece of my code that will help understand my problem better.


void moveUp()
{


part.velocity.x += 0.01;
part.velocity.y += 0.03;



part.position.x += part.velocity.x;
part.position.y += part.velocity.y;


part.acceleration.x -= 0.0;
part.acceleration.y -= 0.01;

part.velocity.x += part.acceleration.x;
part.velocity.y += part.acceleration.y;

cout << endl <<"this is the position on the x coordinate: " << part.position.x << endl;

if(part.position.y <= 0.15)
{
part.acceleration.x = 0;
part.acceleration.y = 0;
part.velocity.x = 0;
part.velocity.y = 0;
}

cout << endl << "this is the velocity on the x coordinate: " << part.velocity.x << endl;
if(part.position.x > 1)
{
part.velocity.x = 0;
part.velocity.y = 0;
part.velocity.x -= 0.01;
part.velocity.y += 0.03;
part.position.x += part.velocity.x;
part.position.y += part.velocity.y;

part.acceleration.x += 0;
part.acceleration.y -= 0.01;

part.velocity.x += part.acceleration.x;
part.velocity.y += part.acceleration.y;

}







}

after running this code the object keeps bouncing at one place on y coordinate and does not move back on the x coordinate. when i print out the velocity of the object on x coordinate..it gives me the encircled value in the attached picture.

[attachment=23960:screenshot.jpg]

any help is appreciated. Also i want the object to jump once and come to a stationary position instead of using a glutTImerFunc(), that keeps calling the update function again and again after i press a key to make the object jump, the object keeps bouncing.

part.acceleration.x -= 0.0; IMO.
Advertisement

part.acceleration.x -=0.0 means no change in acceleration regardless of "+" or "-" because the value increasing or decreasing is 0 which means no change..however i'm changing the value of the velocity..

part.velocity.x -= 0.01;

but doing that just gives me that weird value.

Your problem likely occurs because you always start out (in the moveUp function) by increasing the x position. The code then decreases the x position when it reaches 1. The result is that you increase to 1, decrease to below 1, increase to 1, etc.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

yes that what the problem is..i figured that out but i can't figure out how to keep decreasing the x position one it reaches the value 1. any idea?

yes that's what the problem is..i figured that out but i can't figure out how to keep decreasing the x position when it reaches the value 1. any idea?

Advertisement

First: it's not clear what behavior you want the object to have. You appear to have complicated things a bit with "acceleration," as you increment/decrement it in your code.

Can you describe what behavior you want the object to have, rather than how you want to code it?

Simulating a "jump" is often done with a fixed acceleration (e.g., gravity). At the start of the jump, the object is given an initial upward velocity. Then, each update, the velocity is decreased by acceleration * delta-time. You could simulate that with a simple velocity -= someSmallAmount. That process continues, the velocity eventually changes from positive to negative, and the object falls (still under the same acceleration) until it reaches the "ground."

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


float towardsZero(float x, float speed)
{
    if (std::abs(x) < speed)
        return 0.0f;

    else if (part.x > 0.0)
        return std::max(0.0, x - speed);

    return std::min(0.0, x + speed);
}

Now x will tend towards zero from whichever side of the axis it happens to be on

EDIT: Ok, I understand now

Eg. if speed is (10, 0) you need to figure out what is the highest value you can get away with, say 1.0 (if that is the thinnest terrain you can have)


// number of iterations to run to prevent running through things:
// who cares about rounding, right? just add 1 and be done with it to prevent 0 zero iterations
const int iterations = 1 + speed.length() / 1.0;

// direction player is facing:
const vec2 norm = speed.normalized();

float step_size = 1.0f;
bool moved = false;

while (iterations--)
{
     bool collision = test(player, norm * step_size)
     if (collision)
         // halve step
         step_size *= 0.5f;
     else
     {
         player.pos += norm * step_size;
         moved = true;
     }
}

so what happens above? well it's not a perfect algorithm since you don't guarantee he will move the maximal distance he can go

but it's good enough, for starters

for each iteration, try moving step_size, and if that fails, halve step size

we also take note of if the player ever got moved, since you usually want to use that later on for stuff like animations and gameplay logic

a better algorithm would try halving step size and not decreasing iterations until the player actually moved

but then we have to be careful about not running too many iterations (eg. infinite loop)

This topic is closed to new replies.

Advertisement