STOP!!

Started by
7 comments, last by ToohrVyk 18 years, 4 months ago
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
Advertisement
if ( x < 0 ) xv = xv * -1

or

if( x < 0 ) x = SCR_WIDTH;

kinda thing should work
-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )
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]
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. ;)
You are perfectly right. How ironic.
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!
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
Quote:Original post by Metorical
Hehe... 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 + 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


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;}
Quote:Original post by OldDev
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.


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.


This topic is closed to new replies.

Advertisement