# STOP!!

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

## Recommended Posts

hi guys, in my program, i have objects travelling from one side to another. the problem is after reaching the opposite side the object just goes off the screen. i would prefer the objects to either stop and travel back in the direction it came from or come around again from the initial side of the screen. can anyone help please? cheers

##### Share on other sites
if ( x < 0 ) xv = xv * -1

or

if( x < 0 ) x = SCR_WIDTH;

kinda thing should work

##### Share on other sites
Quote:
 Original post by ErUs(some code)

To bounce, the correct code is:
if( x < 0 ) {x = -x; vx = -vx; }

To wrap around, the correct code is:
if( x < 0 ) { x += SCR_WIDTH; }

[Edited by - ToohrVyk on November 27, 2005 8:03:23 AM]

##### Share on other sites
For wrapping, I think you meant:

if( x < 0 ) { x += SCR_WIDTH; }

Because subtracting from a number less than zero will just send it further out into negative space. (Unless SCR_WIDTH is negative, of course. ;)

##### Share on other sites
You are perfectly right. How ironic.

##### Share on other sites
Lets say that the x variable stores the position of the object on the x plane, and SCR_WIDTH stores our screen's width.
if (x => SCR_WIDTH) // If our object has passed the edge of our screen...{    x - SCR_WIDTH; // Subtract the screen width from our object's x position - thus sending it back to the beginning of the screen}else if (x < 0) // If our object has gone further to the left of our screen...{    x = 0; // Send it back to the beginning}

Sorry for repeating what others have said, but I thought I'd make it a clittle clearer as to what does what for you. Good luck!

##### Share on other sites
Hehe... we are all making silly mistakes.

Say we have screenwidth = 640.

For wrapping:

If the object is at x = 650 then we want to place it at x = 10 so:
x -= screenwidth

If the object is at x = -10 then we want to place it at x = 630 so:
x += screenwidth

For bouncing:

If the object is at x = 650 then we want to place it at x = 630 so:
x = screenwidth - x + screenwidth
x = 2 * screenwidth - x
vx = -vx

If the object is at x = -10 then we want to place it at x = 10 so:
x = -x
vx = -vx

##### Share on other sites
Quote:
 Original post by MetoricalHehe... we are all making silly mistakes.Say we have screenwidth = 640.For bouncing:If the object is at x = 650 then we want to place it at x = 630 so:x = screenwidth - x + screenwidthx = 2 * screenwidth - xvx = -vxIf the object is at x = -10 then we want to place it at x = 10 so:x = -xvx = -vx

For bouncing, you really need to take the velocity into account. e.g. if the ball is travelling at 10m/s and hits a wall 9m away, it will bounce back only one meter in our one second time step.

e.g.

if (x<0){    // Bounce ball back    x=abs(vx)+x;   // x is negative here    // Reverse velocity    vx=-vx;}if (x>screen.width){    // Bounce ball    x=screen.width-(abs(vx)-(x-screen.width));    // Reverse velocity    vx=-vx;}

##### Share on other sites
Quote:
 Original post by OldDevFor bouncing, you really need to take the velocity into account. e.g. if the ball is travelling at 10m/s and hits a wall 9m away, it will bounce back only one meter in our one second time step.

His solution did take this into account:

x = 0;max = 9;vx = 10;x += vx; // Euler integration, x = 10if( x > max ) { // 10 > 9  x = 2*max - x; // 18-10 = 8  vx = -vx; // vx = -10}

The final situation is the correct one: the ball bounced back 1m from the wall at 9m, and finds itself at x=8.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 16
• 15
• 28
• 12
• 14
• ### Forum Statistics

• Total Topics
634805
• Total Posts
3019367
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!