Skip to main content
gamedev.net
🔒 Locked

Object oriented

Started by JL Dec 11, 2002 at 2:46 AM 5 replies 650+ views
Original Post
JL
JL
Hi! This should be a very basic question, but anyway. I am going to do a (2d) bouncing-ball game. In the screen there should be lots of objects that is solid. The ball should bounce on these objects. The objects are all rectangular. What is the technique when deciding when the ball should bounce, and how it should bounce. I thought this was simple. But when I did it, the ball bounces sometimes and sometimes not. When the ball bounces on the wall I use the technique where xspeed or/and yspeed is inverted. This should be the same technique as when dealing with objects in a platform game, shouldn''t it. I want to have an answer from someone who have experience about this. Thanks /J Lindroos
/J Lindroos
JL
JL
I think the method I have to determine if the ball should bounce is - not very good. It is like this:

for yspeed inverting

if ball.x+ball.width > object.x
and ball.x < object.x+object.width
and ball.y+ball.height >= object.y
and ball.y <= object.y+object.height

for xspeed inverting

if ball.y+ball.height > object.y
and ball.y < object.y+object.height
and ball.x+ball.height >= object.x
and ball.x <= object.x+object.width

What do you guys think?



/J Lindroos
/J Lindroos
JL
JL
Come on guys, this is a simple new-beginner question. Everyone who have ever made a platform game should have confronted this!


/J Lindroos

[edited by - JL on December 13, 2002 2:41:37 AM]
/J Lindroos
Trienco
Trienco
im doing a test for each of the four sides of a block, just to be sure it wont move through it *g*.

here Size is the radius of the Ball, MinX, MaxX, MinY, MaxY are the limits of the box/object. XVel, YVel are the velocity of the ball in x,y direction.

if ((YPos>=MaxY+Size)&&(newY<=MaxY+Size)) {
float ISX=XPos+(XVel*(MaxY-YPos)/YVel);
if ((ISX>=MinX-Size)&&(ISX<=MaxX+Size)) {
newX=ISX; newY=MaxY+Size;
YVel=-YVel;
return 1;
}
}
else if ((YPos<=MinY-Size)&&(newY>=MinY-Size)) {
float ISX=XPos+(XVel*(MinY-YPos)/YVel);
if ((ISX>=MinX-Size)&&(ISX<=MaxX+Size)) {
newX=ISX; newY=MinY-Size;
YVel=-YVel;
return 1;
}
}
else if ((XPos<=MinX-Size)&&(newX>=MinX-Size)) {
float ISY=YPos+(YVel*(MinX-XPos)/XVel);
if ((ISY>=MinY-Size)&&(ISY<=MaxY+Size)) {
newX=MinX-Size; newY=ISY;
XVel=-XVel;
return 1;
}
}
else if ((XPos>=MaxX+Size)&&(newX<=MaxX+Size)) {
float ISY=YPos+(YVel*(MaxX-XPos)/XVel);
if ((ISY>=MinY-Size)&&(ISY<=MaxY+Size)) {
newX=MaxX+Size; newY=ISY;
XVel=-XVel;
return 1;
}
}
f@dzhttp://festini.device-zero.de
JL
JL
Finally! Thank you very much.

Just one thing. The ball in my case is not round, it is a rectangular image, but I suppose it is better to make it round, i.e just use the radius of the ball. Another question is, why does this function always return 1?

This seems to be what I have been looking for, thankyou again!



/J Lindroos
/J Lindroos
Trienco
Trienco
it doesnt.. its hard to see without proper formation, but return 1 is only inside the if (ISX bla) parts. of course at the very end of this should be a return 0.

a ball is easier to test and if your blocks are axis aligned you will only see a difference if the ball hits a corner.

maybe the math isnt 100% correct. im still not sure what exactly i did there.. maybe i SHOULD start commenting my code, so i will know what i was thinking when i wrote it.. but it worked well in my pong and breakout clones *g*

if you want the whole code:
http://festini.device-zero.de/downloads/pongsrc.zip
or http://festini.device-zero.de/downloads/index.shtml


f@dzhttp://festini.device-zero.de

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.