iDevGames Forums
Question Regarding the Reflect Angle of a Transition - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Question Regarding the Reflect Angle of a Transition (/thread-123.html)



Question Regarding the Reflect Angle of a Transition - iBaby - Apr 23, 2010 08:13 AM

Hi, I'm new to iPhone game development and I plan to start developing a simple pong game.

I am currently using CGRectIntersectsRect for my collision detection and reflecting the ball after it hits the bat by simply reversing the angle where it hits the bat.

What I am trying to do is to make it so that the reflect angle of the ball is determined by the position of where it hits the bat, and not a simple reversing of direction. (E.g. if the ball lands in the middle of the bat, it will go straight, the further away from the middle, the bigger the reflect angle)

Can someone enlighten me on this particular problem I am having?

I hope my question is understandable... Blush

Thanks a lot.


Question Regarding the Reflect Angle of a Transition - OneSadCookie - Apr 23, 2010 03:50 PM

http://www.teacherschoice.com.au/maths_library/coordinates/polar_-_rectangular_conversion.htm


Question Regarding the Reflect Angle of a Transition - iBaby - Apr 23, 2010 10:24 PM

Umm... I've read the link you've posted, it's the mathematical equation of converting a normal coordinate into polar coordinate, but I don't have any idea how to relate it to the reflect angle of the ball when it hits the bat...

Can you guide me on how to implement the mathematical equation to the programming itself?

I tried googling on this particular topic and everyone gives a different idea on how to do it, I am so confused...

Sorry but I'm still new in this, please go easy on me Cry


Question Regarding the Reflect Angle of a Transition - JustinFic - Apr 27, 2010 03:15 PM

For this math, I'm assuming your paddle is horizontal, and your ball has hit it from the top. (Using breakout as a model here since that's the exact behavior you want.)

The pseudocode in a nutshell:

Code:
horizontalDistance = ballCenterPosition.x - paddleCenterPosition.x

newBallVelocity.x = horizontalDistance * some coefficient (play with it)
newBallVelocity.y = -oldBallVelocity.y

alternatively,

newBallVelocity.x = oldBallVelocity.x + horizontalDistance * some coefficient (play with it)

Note I'm using velocity as a vector here with x and y components instead of an angle in degrees/radians-- it's much easier to visualize it this way for me. Hope that helps!