Mirror an angle?

Started by
7 comments, last by fpsgamer 15 years, 9 months ago
Hey. Say I have an object coming in at say 45 degrees to a surface above it, it will bounce off at a mirrored angle. I guess my question is, what would the formula be to work this out? And would I need a different one for a vertical surface? A different one for a different direction? I'm making a simple breakout game and obviously would like to make the ball bounce.
Advertisement
In the case of breakout the ball is interacting with only vertical and horizontal surfaces. This is a good thing because we can employ a simple trick to calculate the reflected vector.

Lets say the ball's direction vector is <x,y>.

That vector reflected off of a horizontal surface is is <-x, y>
That vector reflected off of a vertical surface is is <x, -y>
Are you just using horizontal and vertical surfaces? If so, just negate the horizontal velocity when hitting a vertical surface, and negate the vertical velocity when hitting a horizontal surface.
At the moment I'm just using an angle and calculating the x and y real time, is there a way to just use the angle?


Also, what would be the formula for a slanted / diagonal surface?
Quote:Original post by dwfait
At the moment I'm just using an angle and calculating the x and y real time, is there a way to just use the angle?


Also, what would be the formula for a slanted / diagonal surface?


It wont be possible until you familiarize yourself with the concept of a direction vector.
Well I am familiar with vectors. Not sure why I picked to work with angles really. I've converted the code to have a double vectorX and a double vectorY, to be multiplied by a double moveSpeed variable. So all I would do is -vectorY for horizontal and -vectorX for vertical?


How would I go about diagonal / slanted surfaces? Would I need the angle of the surface?
Quote:Original post by dwfait
Well I am familiar with vectors. Not sure why I picked to work with angles really. I've converted the code to have a double vectorX and a double vectorY, to be multiplied by a double moveSpeed variable.


I'm not sure what you're referring to as the "angle" here. Those sound like vectors to me.

Quote:Original post by dwfait So all I would do is -vectorY for horizontal and -vectorX for vertical?


Yes, for simple reflections off of horizontal and vertical planes.

Quote:Original post by dwfait
How would I go about diagonal / slanted surfaces? Would I need the angle of the surface?


You would need the normal vector of the surface. This will work in 2 or 3 dimensions:

I_R = I - 2*(N dot I) cross N

I is your incident vector, I_R is your resulting reflected vector and N is the normal to the surface the incident vector is reflecting off of.
Quote:Original post by fpsgamer
Quote:Original post by dwfait
Well I am familiar with vectors. Not sure why I picked to work with angles really. I've converted the code to have a double vectorX and a double vectorY, to be multiplied by a double moveSpeed variable. So all I would do is -vectorY for horizontal and -vectorX for vertical?


How would I go about diagonal / slanted surfaces? Would I need the angle of the surface?


You would need the normal vector of the surface. This will work in 2 or 3 dimensions:

I_R = I - 2*(N dot I) cross N

I is your incident vector, I_R is your resulting reflected vector and N is the normal to the surface the incident vector is reflecting off of.




Would I be right in thinking that the Normal is the vector of the line that is perpendicular to the surface?
Quote:Original post by dwfait
Would I be right in thinking that the Normal is the vector of the line that is perpendicular to the surface?


Yup. IIRC is must also be unit length.

This topic is closed to new replies.

Advertisement