Jump to content
  • Advertisement
Sign in to follow this  
MrChrisnis

Problem with Rebounding

This topic is 2271 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.


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();
// }
//}
}


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
Advertisement
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=#666600][[color=#000000]k[color=#666600]]->[color=#000000]velX [color=#666600]=[color=#000000] [color=#666600]-[color=#666600]([color=#000000]s[color=#666600][[color=#000000]k[color=#666600]]->[color=#000000]velX[color=#666600]);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!