gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Ball bouncing

#18076 - ProblemBaby - Sat Mar 20, 2004 2:46 am

why doesn't this code work??????????????
Code:

if (ball[0].x < 10 || ball[0].x > 230)
{
      if (ball[0].Direction > 180)
         ball[0].Direction = 540 - ball[0].Direction;         
      else
         ball[0].Direction = 180 - ball[0].Direction;
}

if (ball[0].y < 10 || ball[0].y > 150)
{
      ball[0].Direction = 360 - ball[0].Direction;
}


its fucks up after a while its like ball[0].Direction changes its value it is a u16

if I set what direction should be it works

i tried this and its work

Code:

if (ball[0].x < 10 || ball[0].x > 230)
{
      if (330 > 180)
         ball[0].Direction = 540 - 330;         
      else
         ball[0].Direction = 180 - ball[0].Direction;
}

if (ball[0].y < 10 || ball[0].y > 150)
{
      // ball[0].Direction = 360 - ball[0].Direction; doesn't work in    this case either
                ball[0].Direction = 360 - 330; // works fine!
}


and oh the sin and cos table has 361 values sin[360] = sin[0] and same for cos

#18079 - yaustar - Sat Mar 20, 2004 3:42 am

Do you have an error check if ball[0].Direction goes into negative figures?

eg what would happen if:
Code:
ball[0].Direction = 200;
ball[0].Direction = 180 - ball[0].Direction;

_________________
[Blog] [Portfolio]


Last edited by yaustar on Sat Mar 20, 2004 3:49 am; edited 2 times in total

#18080 - Miked0801 - Sat Mar 20, 2004 3:43 am

1. 330 > 180 is always true (but you knew this as it is test case).
2. Why use 360 degree tables? 256 is better and faster (minor)
3. Why use directions for reflection - just negate the X/Y velocities when it hits the wall. If you want it slightly random, add a small random number +/- to the new velocity.

Throw an assert on ball[0].Direction at the beginning of the function to make sure that something else in your code isn't wiping it out. BTW, if you did the 256 thing, to reflect, you'd make direction a u8 and do your math as is. There's no way for it to go out-of-bounds.

ASSERT(ball[0].direction <= 360);

or

if(ball[0].direction > 360)
{
printf("Direction out of bounds\n");
}

That you say it dies eventually makes me think it might be a memory corruption error and the printf/assert will catch that. BTW, when it dies, is it on an X or Y reflection? That might give you insight.

Yaustar - the initial if in the X makes sure direction isn't negative I believe.

#18083 - yaustar - Sat Mar 20, 2004 4:00 am

hmmm... maybe I should change it :P
Code:
ball[0].Direction = 541;
ball[0].Direction = 540- ball[0].Direction;


Mike, I have some questions as well
1) What does ASSERT do, I havent seen it yet :/
2) how would a LUT of sin[256] work? I currently am looking at 360 table in bemusement.. :(

edit: or are you refering to the values stored inside the table ?

edit2: I am only second guessing here so I could be very wrong. The ball is bouncing inside a box. This if statement
Code:
if (ball[0].x < 10 || ball[0].x > 230)

is only equals to true if the ball hits the area of 0-10 and 230-240, is this waht you want or you want it to equal to true for 10-230? If so:
Code:
if (ball[0].x > 10 && ball[0].x < 230)

_________________
[Blog] [Portfolio]

#18085 - poslundc - Sat Mar 20, 2004 4:08 am

yaustar wrote:
1) What does ASSERT do, I havent seen it yet :/


Have a look at this thread.

Quote:
2) how would a LUT of sin[256] work? I currently am looking at 360 table in bemusement.. :(


Just use your own unit of measurement to measure the angle of a circle. Instead of 360 degrees or 2pi radians around a circle, make it 256. So half a circle is 128, a quater is 64, etc.

Advantages include having a table that's nice and aligned, being able to represent fractional degrees using fixed-point math very easily, and quickly finding the correct entry for any value - just mod by 256 (bitwise-AND 255).

For more detail, you can make your table have 512 entries instead of 256. This gives you almost 50% more resolution than a 360-degree table would.

Dan.

#18098 - ProblemBaby - Sat Mar 20, 2004 12:30 pm

yeah it was a good idea to have 256 entries instead of 360 in the sin and cos tables