Sign in to follow this  

Problem with Rebounding

This topic is 2052 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I am making a simple shooter game, and have come up against a problem with the target sprites rebounding back. I am able to get them rebounding with one method (See code below) however I am trying to get it working using the -abs(pointer->velX); method. Using this method the sprites are fine rebounding back off the right hand border, however when I try getting them to rebound off the left hand border, the whole game just freezes.

[CODE]
for(int k = 0; k < NUM_STATIONS; k++)
{
if(s[k]->get_x() + s[k]->get_width() >= 620 || s[k]->get_x() <= 20)
{
/*s[k]->set_world_position(619 - s[k]->get_width(), s[k]->get_y());
s[k]->velX = -s[k]->velX;
s[k]->set_auto_move(10);
s[k]->update_everything();*/
while(s[k]->get_x() + s[k]->get_width() >= 630 || s[k]->get_x() <= 20)
{
s[k]->velX = -abs(s[k]->velX);
s[k]->set_auto_move(10);
s[k]->update_everything();
}
}
//if(s[k]->get_x() <= 20)
//{
// /*s[k]->set_world_position(21, s[k]->get_y());
// s[k]->velX = -s[k]->velX;
// s[k]->set_auto_move(10);
// s[k]->update_everything();*/
// while(s[k]->get_x() < 21)
// {
// s[k]->velX = -abs(s[k]->velX);
// s[k]->set_auto_move(10);
// s[k]->update_everything();
// }
//}
}
[/CODE]

The coded out parts are the different ways I have tried it, splitting up the if statement didn't work, and there is also the method where I set the sprite position to the rebound boundary + or - 1 pixel and reversed the velocity.

Any help would be greatly appreciated :)

Thanks
Chris

Share this post


Link to post
Share on other sites
Why are you using abs? You know abs will always return a positive value right?

So, if your x velocity is already positive and moving right, say 20, then -abs(20) = -20, which is what you want.
But if x vel is negative and moving left, say -20, then -abs(-20) = -20, which is wrong.

Just remove the abs, and negate the velocity

[color=#000000]s[/color][color=#666600][[/color][color=#000000]k[/color][color=#666600]]->[/color][color=#000000]velX [/color][color=#666600]=[/color][color=#000000] [/color][color=#666600]-[/color][color=#666600]([/color][color=#000000]s[/color][color=#666600][[/color][color=#000000]k[/color][color=#666600]]->[/color][color=#000000]velX[/color][color=#666600]);[/color]

Share this post


Link to post
Share on other sites

This topic is 2052 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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