Advertisement

help plz.., breakout/arkanoid clone, bouncing ball, giving it angles etc.,

Started by August 11, 2002 08:59 AM
24 comments, last by mickey 22 years, 5 months ago
ehm, can somebody plz tell me what atan2f(...) does or what is it there for? i never got that far from trigo,

thanks,
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
atan() is inverted tan, atan2() is a nice if you don''t want to risk a division by zero, and still get correct angle. Check here: http://www.introl.com/introl-demo/Libraries/C/ANSI_C/math/atan2.html.

I think you should try to come up with how "my" algorithm work on your own, and then you can look on my code for inspiration on how to implement it; I think you need to understand it to get it working in your engine.
Advertisement
The ''f'' in atan2f() is for the (32-bit) float version of the function, which is otherwise double. As I''m only using floats here I always use sinf(), cosf(), atan2f() etc.
hello, i think i''ve done it, with your help and a little more help from other tutorials.., i think i''ve understood it,

x/y = length * sin/cos // this will give you your desire angle, they say, this is from polar coords to cartesian coords,

anyway, i''ve a new problem hope you could help me out here too. I''ve done all sorts of collision detection between the ball and the block, my only problem left now is, sometimes the ball gets stuck in the block or i have a block that needs to be hit thrice before it breaks, but sometimes the ball just hit it once and it breaks, i don''t know if the problem here is that my computer is fast or probably the speed of the ball., any ideas how to improve it?

btw, my loop goes like this,

check collision
moveball

maybe i should interchange those two huh?

thanks,
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
Hm. In my version, I meant to implement a very sophisticated collision detection, where each time the ball starts going somewhere it calculates when (in time) it would hit anything, and where it should go from there. Then in my loop, I just checked if that time had been reached, and if so, I took the destination action.
This worked well for the borders, and for when the ball hit the brick on its sides, but I never came up with a good algorithm to detect when and how the ball would hit the corner. Due to this, I had to implement a real time collision detection as well. I think that the most important is to not only check the ball's position, but also its vector when detecting a hit.

A small excerpt from my code:
    // Are we in the middle of a brick? No idea what to do...if(this->pcBrickType[nBrickHit = nBrick])	nTypeOfHit = 0x80 | 0x02 | 0x04 | 0x10; // Hitting a brick X - 1?else if(this->pcBrickType[nBrickHit = nBrick - 1] && fLocalX < pBall->fRadius && pBall->fSrcSpeedX < 0)	nTypeOfHit = 0x80 | 0x01 | 0x02 | 0x10; // Hitting a brick X + 1?else if(this->pcBrickType[nBrickHit = nBrick + 1] && fLocalX > 1 - pBall->fRadius && pBall->fSrcSpeedX > 0)	nTypeOfHit = 0x80 | 0x01 | 0x02 | 0x10; // Hitting a brick Y - 1?else if(this->pcBrickType[nBrickHit = nBrick - this->nBoardWidth] && fLocalY < pBall->fRadius && pBall->fSrcSpeedY < 0)	nTypeOfHit = 0x80 | 0x00 | 0x04 | 0x10; // Hitting a brick Y + 1?else if(this->pcBrickType[nBrickHit = nBrick + this->nBoardWidth] && fLocalY > 0.5 - pBall->fRadius && pBall->fSrcSpeedY > 0)	nTypeOfHit = 0x80 | 0x00 | 0x04 | 0x10; // Are we hitting paddle?else if(pBall->fCurrentY > (float)this->nBoardHeight / 2 - 0.5 - pBall->fRadius	 && pBall->fSrcSpeedY > 0	 && pBall->fCurrentX > this->fPaddleHPosX - this->fPaddleHSize	 && pBall->fCurrentX < this->fPaddleHPosX + this->fPaddleHSize) {	nTypeOfHit	= 0x80;	// -1 for absolute left of paddle, 0 for middle, +1 for absolute right	fPaddleHit	= (pBall->fCurrentX - this->fPaddleHPosX) / this->fPaddleHSize * (float)0.9;	// Our angle of attack	fAngleIn	= atan2f(-pBall->fSrcSpeedX, pBall->fSrcSpeedY);	// Calculate our defence angle	fAngleOut	= fPaddleHit * ((float)(PI / 2) + fAngleIn * (fPaddleHit < 0 ? -1 : 1)) - fAngleIn;	// Calculate speed vector based on our outgoing angle	pBall->fSrcSpeedX = -pBall->fSrcSpeed * sinf(fAngleOut + (float)(PI));	pBall->fSrcSpeedY = -pBall->fSrcSpeed * cosf(fAngleOut);} // If no hit, we'll leave it hereif(!nTypeOfHit) return false; // Adjust angles etc.pBall->fDestX		= pBall->fCurrentX;pBall->fDestY		= pBall->fCurrentY;pBall->fDestSpeed	= pBall->fSrcSpeed;pBall->fDestSpeedX	= (nTypeOfHit & 0x02) ? -pBall->fSrcSpeedX : pBall->fSrcSpeedX;pBall->fDestSpeedY	= (nTypeOfHit & 0x04) ? -pBall->fSrcSpeedY : pBall->fSrcSpeedY;pBall->fDestAngle	= atan2f(pBall->fDestSpeedX, pBall->fDestSpeedY); // If we hit a brick, take care of itif(nTypeOfHit & 0x10) this->HitBrick(nBrickHit);    

As you see. When I check if the ball hits the right side of the brick to the left "brick space" the ball is in, I only raise a hit if the ball's X-vector is negative. I flag this as "vertical hit" and then negates the X-vector, and so it won't detect a hit next time. I do not properly handle when the ball find itself right inside a brick (that is, its center is inside a brick), but that should never happen.

I could mention, that I'm using a virtual coordinate system in my game, where each brick is 1.0 units wide and 0.5 units high. And use two arrays of chars[BoardWidth * BoardHeight] (note, not [BoardWidth][BoardHeight]), one for the type of brick, pcBrickType[] (also indicating if there is any brick), and one for the status of that brick, pcBrickStatus[] (which is not used here). Also note, that my game isn't finished, so the code above isn't perfect.

fLocalX and fLocalY are floats with the values 0.0-1.0 and 0.0-0.5, respectivly, indicating where in the "brick space" the ball is. nBrick is the index in the array where the ball is in.

[edited by - CWizard on August 18, 2002 12:03:25 PM]
helo,

ehm, what do you mean by this "but also its vector when detecting a hit", what kind of vector am i going to watch out for this?

also, do you set the position too of the ball after it hits or you just bounce it back with the appropriate angle?

i used a 3 array of int for my game, ie, map[width][height][type of block] then seperate meshes for each type of block i have but all the block''s texture is only contained in one file. This way, i don''t need to call SetTexture everytime i render a block but instead, use the right mesh(the only thing that is different are the texture coordinates), how about you? are you using d3d8 too?
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
Advertisement
hmm.., "where each time the ball starts going somewhere it calculates when (in time) it would hit anything", do you think i need to do this too so that my ball doens''t stuck in the blocks or goes out of bounds? yeah, sometimes my ball just go out of bounds, it doesn''t bounce properly when it hits the sides of the wall,
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
argh! halp!

maybe it has got to do with my blocks having round sides(i used a box, attached 2 cylinders unto it''s side), everything is smooth as long as the ball hits the bottom, top and sides of the blocks, but when it comes to the corners, the ball would either slide through or get stuck inside. Maybe this has something to do with the ball moving so fast?

or maybe my checking is bad? pls look at how i do it,

if(ballposZ + min/max >= blocksposZ + min/max && ballposz + min/max <= blockPosZ + min/max)
// possible collision, now check x
if(ballposX + min/max >= blockPosX + min/max && ballposX + min/max <= blockposX + min/max)
// collision detected
bounce ball


http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
quote:
Original post by mickey
ehm, what do you mean by this "but also its vector when detecting a hit", what kind of vector am i going to watch out for this?
This was the ball''s vector. What I mean is this: If the ball is "in" or "at" the left border, you should only detect a hit when the ball''s vector has a negative X-part (ie. travelling left), and not if it is positive (ie. travelling right). Make sense? This is to avoid race conditions, when the ball gets a bit stuck in the wall or brick, and continue bouncing back and forth. Just think about it; If your ball is "inside" the left border, and is travelling right, you don''t want it to change direction to the left.
quote:
also, do you set the position too of the ball after it hits or you just bounce it back with the appropriate angle?
Eh, in my current case: no. Well, usually it do, as I have a hybrid system for collission detection. When the hit is predicted (and it didn''t hit anything else before that) it will be moved to exactly the radius from the brick wall (and on it''s path towards it), while on a "real time" hit (the stuff I''ve shown here) it won''t be adjusted, only the speed vector changes.
quote:
i used a 3 array of int for my game, ie, map[width][height][type of block] then seperate meshes for each type of block i have but all the block''s texture is only contained in one file. This way, i don''t need to call SetTexture everytime i render a block but instead, use the right mesh(the only thing that is different are the texture coordinates), how about you? are you using d3d8 too?
Sorry to say, I don''t quite understand why you need a three dimensional array or how you use it, but that''s up to you, and I guess it''s working. No, I didn''t use Direct3D, just DirectDraw 7. The stuff I made was mostly to try a kind of game state machine with independent objects that I plan to employ in the future, so it was never really meant to be a "real" game.
quote:
hmm.., "where each time the ball starts going somewhere it calculates when (in time) it would hit anything", do you think i need to do this too so that my ball doens''t stuck in the blocks or goes out of bounds? yeah, sometimes my ball just go out of bounds, it doesn''t bounce properly when it hits the sides of the wall,
Well, I wouldn''t suggest it. I think it works really great in my game (at least for the borders ), but I think it would be a pain in the ass to implement it at this stage. As you might have seen in my code, I keep both source and destination status of ball. And I do not increment the balls position, but calculate it exactly from where it''s source position was, it''s "source" speed vector and the time elapsed since it departed from that source point. I couldn''t make this collision prediction complete, so I also implemented convensional detection as back-up, but then the prediction isn''t really needed any more. I think you should build on what you have, instead of starting all over.

I''m very unsure about your code in your latest post, as I''m tired (haven''t slept for 27 hours ), and I will have a look at after some sleep and see if I can make a constructive comment. Perhaps I will understand it directly after a slept, but know I can''t figure out what the min/max is for...
hello CWizard,

thanks you very much for sparing your time helping me out,

heh, sorry about the ball''s vector, yeah i understand that i also use that kind of method.

i constructed a bounding volumes for all the objects in my game using D3DX so that''s what min/max is for.,

anyway, i almost understand now., one thing left bothers me., atan2f() is used to calculate the angle in which the ball hits the paddle., how could that be used for the next angle?? i mean, the angleout would be calculated "on which part" of the paddle the ball would hit it, so the anglein of the ball doesn''t matter.., or in short, i didn''t understand this code, "fAngleOut = fPaddleHit * ((float)(PI / 2) + fAngleIn * (fPaddleHit < 0 ? -1 : 1)) - fAngleIn"

and last., my ball still gets stuck in the blocks., i''ve observed that, evertime my ball hits near the corners of my rounded shape blocks, it get''s stuck, otherwise, no. (ie, top/bottom and sides),

again, thanks very much for your patient CWizard, hope you could hang-on am almost there!
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,

This topic is closed to new replies.

Advertisement