-
Bouncing ball - help ??
I'm writing a little program where a ball is bouncing at some walls.
How do i get the right code for bouncing of a line..??
Gugge
Hope some of you can answer this....
some of the code I got:
----------------
typedef struct line lineT;
struct line
{
int x1, x2;
int y1, y2;
};
typedef struct ball ballT;
struct ball
{
int x,y,r; /*r = radius */ /* RADIUS1 = 8 */
};
void bounce_check_ball(int *x, int *y, int *xstep, int *ystep, ballT *ball, lineT lines)
{
if(*ball[0] <= lines[6]) /* Theres something WRONG here */
{
*y = lines[6]-1;
*ystep = -1*(random(2)+1);
}
}
main()......
lineT lines[30]; /* Array to keep all borderlines */
ballT ball[1];
/* A line whice the ball should bounce off at */
lines[6].x1 = 510;
lines[6].y1 = 116;
lines[6].x2 = 100;
lines[6].y2 = 116;
ball[0].x = x; /* Startcoordinater for Ball */
ball[0].y = y; /* ----------||----------- */
ball[0].r = RADIUS1; /* Radius for ball[0] */
while(1)
{........
bounce_check_ball(&x, &y, &xstep, &ystep, &ball, lineT lines[6]);
x += xstep;
y += ystep;
setcolor(LIGHTCYAN);
restorescreen((x-RADIUS1),(y-RADIUS1), &ball1);
delay(15);
restorescreen((oldx-RADIUS1),(oldy-RADIUS1), &blank); oldx=x; oldy=y;
return 0;
}
-
Could you be a little more descriptive? I am having trouble understanding completely, but here is my suggestion from what I can tell...
Code:
typedef struct
{
int xPos, yPos;
int xStep, int yStep;
int radius;
} Ball; // Each ball has its own velocity, so I added it to the struct.
// Checks to see if the ball *bounceMe intersects line
// *bounceOffMe and changes the velocity of bounceMe
// appropriately if it does. Returns whether *bounceMe actually
// hit *bounceOffMe
int bounceBall (Ball * bounceMe, lineT const * bounceOffMe)
{
int bounce = 0;
int A, B, C, X, Y;
int xStepNew;
int yStepNew;
if (ballIntersectsLine (bounceMe, bounceOffMe) {
bounce = 1;
X = bounceOffMe -> x2 - bounceOffMe -> x1;
Y = bounceOffMe -> y2 - bounceOffMe -> y1;
A = X * Y;
B = X * X - Y * Y;
C = X * X + Y * Y;
xStepNew = (2 * bounceMe -> yStep * A + bounceMe -> xStep * B) / C;
yStepNew = (2 * bounceMe -> xStep * A - bounceMe -> yStep * B) / C;
bounceMe -> xStep = xStepNew;
bounceMe -> yStep = yStepNew;
}
return bounce;
}
As you can see, the formula for a bounce is something of a monster (it is possible I got the formula wrong even, but I'm pretty sure this is it).
-
tx for answering. I will try to figure out your code tomorrow, It's getting late overhere..
It's difficult to get all the code in here, since my code i very long all inclusive.
Sometimes it's much easiere to see what the problem is, when the code is in the editor..
I hope i can use you answer, or else I will ask some more..
Gugge
-
I got a bouncing ball program to work, if you need any help, i'll give you the source.
-
Tx I would love to see that.
I got the ball to bounce of lines (walls) in 1 rectangle fx. (1,1,639,479) /* graphic mode 640x480) */
But the real trouble is comming when I try to add more lines on the screen, both vertikal- , horizontal- and odd lines...
my email is [email protected]
tx very much..
Gugge
-
It's in Direct X though, I should've posted that before, sorry. If you still want me to post it, I will
-
sure still like to see it..
Did you program it in C??
-
Nope, C++, but
1. It should still work.
2. I'll post it here or PM you when I get more time later on.