Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

mickey

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

This topic is 5937 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

if you can, plz explain it very simple, i have a velocity for the ball, plus position and to bounce it, i do it the most simplest way., // this is how i move the ball ballpos += ballvelocity * elapsed time // and this how i bounce the ball against the wall and paddle check for collision against wall if true then { ballvelocity *= -1.0f; set ball position next to the object } check collision again paddles leftarea, rightarea and middle area, then same method as above, i make the velocitiy negative., okey my problem here is, the bounce on the paddle would be wrong because it would just bounce back where it came from, it should bounce on in a new direction, also, how would i go by giving the angle i desire for the ball? also, how do i know exactly on what spot on the paddle did the ball hit it? btw, am using x and z axis for this, i use bounding volumes for the collision detection and D3DXVECTOR3 for the positions and velocities., do i need an acceleration? if so, how will i use it? many thanks, [edited by - mickey on August 11, 2002 10:01:13 AM]

Share this post


Link to post
Share on other sites
Advertisement
hmm., how will i know, if the ball came from the left side, or the right side of the game area? so i can bounce the ball appropriately,

ie, if the ball came from the left side, and it hits the left area of the paddle, then bounce it back where it came from,

if the ball came from the right side, and it hits the left area of the paddle, then bounce it to the left side of the game area,

thanks!!

Share this post


Link to post
Share on other sites

  
bool ballcollidesleftpaddle();
bool ballcollidesrightpaddle();

static vec2 absvelocity(2,2); // just some values, could be choosen by the user..


if(ballcollidesleftpaddle()) {
ball.velocity.x = asbvelocity.x; //positive velocity => right

} else if(ballcollidesrightpaddle()) {
ball.velocity.x = -asbvelocity.x; //negative velocity => left

}

if(ballcollidestop()) {
ball.velocity.y = absvelocity.y; //positive velocity => down

} else if(ballcollidesbottom()) {
ball.velocity.y = -absvelocity.y; //negative velocity => up

}


"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
yes, that''s what i do right now, but after a while, the game looks silly, i want to specify the amount of angle that the ball would take when it hits the paddle,

thanks,

Share this post


Link to post
Share on other sites
Think of it this way..

If a ball moving at (-1,-1) hits a horizontal surface, then only the Y axis of the ball''s movement vector should be reflected, right? If the ball hits a vertical surface, then only the X axis of the ball''s movement vector should be reflected, right?

So... if you hit the roof or the paddle, multiply the ball-move-vector''s Y axis by -1, but leave the X alone. If you hit the side walls, ball-move-vector''s X axis gets reversed, but the Y gets left alone.

Or, for maximum flexibility... take the surface normal of the surface the ball hit. Call this vector SurfNorm, and call the ball''s movement vector BallMove.

BallMove.x += (-2*BallMove.x) * SurfNorm.x;
BallMove.y += (-2*BallMove.y) * SurfNorm.y;

Remember that the surface normals should be facing into the playing field (or out of the solid object they represent the sides of), and you should get this down pat with no problems at all.

The left wall has a surface normal of (1,0), while the right wall has a normal of (-1,0). The paddle''s top surface has a surface normal of (0, -1), and the roof has a surface normal of (0, 1) (if positive Y represents up)

You''ll actually need more complex math to do real reflection, but this is just a quick fix for you...

(Pardon... just re-read your original post. Replace all the Ys in my post with Zs)

Share this post


Link to post
Share on other sites
ehm, guys, that''s what i do, what i want is, change/give or have my own angle of the ball when it hits the paddle. I want to be the one to specify the angle,

Share this post


Link to post
Share on other sites
I made a game called Breakanoid a while ago. I came up with some algorithm for the paddle stuff:

// -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);


Right from the source. Please dont make me explain it...

Share this post


Link to post
Share on other sites
ehm., okey, can somebody else plz explain what happened there? what''s with the angle in and angle out? and what does he mean by " Calculate speed vector based on our outgoing angle "?

many thanks,

Share this post


Link to post
Share on other sites
Ok, I don't remember exactly here, but I'll try to explain.

The effect I wanted was that the ball should go out in "same" angle as it came in when it hit the middle of the paddle, and have the angle increase or decrease based on how far away from the middle it hit.

Let's say we have the ball coming in with the speed vector (2,1). If it hits in the middle, it should go out with the speed vector (2,-1). With me? In my algorithm the ball would bounce out with the speed vector (-2,-1) -- the same -- if it would hit the absolute left of the paddle, and bound out with a zero angle if it hit the absolute right. I then interpolate between these cases.

This is pretty easy to implement, if you know your trigonometry. My solution is messy and tweaked, so you should better try to do it yourself. Just pick up pen and paper and draw it.
The point is first to get where on the paddle the ball hit (fPaddleHit): -1 (left) to 0 (middle) to 1 (right). I multiplied this by 0.9 cus I didn't want the ball to go out completly horizontal.
Then you must calculate what angle the ball hit the paddle (fAngleIn). This angle should be calculated so that it be: -PI/2 (from left) to 0 (from above) to +PI/2 (from right).

Hell! I can't explain this. My calculations are very messy and tweaked to work. If anyone else understand what I'm doing and can explain it, please do!

The "Calculate speed vector based on our outgoing angle" is just what it says. fAngleOut is our outgoing angle, which we convert to the speed vector I use to move the ball. You should have something similar, right? It is as simple as it is in my source, except that you might not need the " + (float)(PI)" part.

[edited by - CWizard on August 13, 2002 1:00:08 PM]

Share this post


Link to post
Share on other sites
wow, thanks a lot CWizard! that''s actually the effect that i want, increasing the angle based on how far it hits the paddle from the middle!

will be back in 3 days if ever i won''t get this right, hope you''re still there,

thanks again,

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!