8 Replies - 1257 Views - Last Post: 23 June 2013 - 08:24 AM

#1 dreaminxna  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 53
  • Joined: 10-June 13

angle of reflection and incidence?

Posted 18 June 2013 - 05:19 AM

Hi. Im new to xna specially using some physics. I tried some searching and found some formula i think, but i dont know how to use it. And what is this normal value in getting the angle of reflection?
here is my code
if (ballRect.Intersects(paddleRect))
            {

                ballSpeed.Y *= -1;
            }
if (ballPos.X < 0)
            {
                ballSpeed.X *= -1;
                ballPos.X = 0;
            }
            if (ballPos.X > GraphicsDevice.Viewport.Width - ballRect.Width)
            {
                ballSpeed.X *= -1;
                ballRect.X = GraphicsDevice.Viewport.Width - ballRect.Width;
            }
            

            if (ballPos.Y < 0)
            {
                ballSpeed.Y *= -1;
                ballRect.Y = 0;
            }



here is my declaration
 private Texture2D ball, paddle;
        private Rectangle paddleRect, ballRect;
        private Vector2 paddlPos = Vector2.Zero;
        private Vector2 ballPos = Vector2.Zero;

        private float paddleSpeed = 250;
        private Vector2 ballSpeed = new Vector2(2,2);


You can see in my code that it do some bouncing but that was fixed.The angle was fixed where ever part of my paddle is hit. I just want it to be accurate as possible. Its for my project. Im weak in mathematics so can anyone explain?? pls :(/>

Is This A Good Question/Topic? 0
  • +

Replies To: angle of reflection and incidence?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,126
  • Joined: 27-December 08

Re: angle of reflection and incidence?

Posted 18 June 2013 - 07:41 AM

I have a tutorial on 2D collisions. Hope it helps!
Was This Post Helpful? 1
  • +
  • -

#3 dreaminxna  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 53
  • Joined: 10-June 13

Re: angle of reflection and incidence?

Posted 18 June 2013 - 04:30 PM

View Postmacosxnerd101, on 18 June 2013 - 07:41 AM, said:

I have a tutorial on 2D collisions. Hope it helps!


Thanks. I have read your tutorial and i'll be honest it gives me some understanding but i still cant figure out how to use it on code :( :oops: ..But thanks.. when it comes to programming, math kills me :hang:
Was This Post Helpful? 0
  • +
  • -

#4 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 579
  • View blog
  • Posts: 1,286
  • Joined: 24-April 12

Re: angle of reflection and incidence?

Posted 21 June 2013 - 12:11 PM

Sorry that I'm a bit slow to reply to this. I've been busy, and honestly I skipped over this post as I'm usually not excited about reading someone else's code to figure out what their problem is. I would much rather they just ask "How do you do such and such?" After I read it, this also turned out to be a little more straight forward and simple than I originally thought for some reason. But I have some time now and will take a stab at it.

I suspect that your problem is that you don't understand vectors. That's not surprising as I've met only a hand full of people in my whole life (less than 20) who even had a clue what vectors are. My last professor teaching vectors quite obviously had no clue what they are and was obviously teaching straight from the book.

Fortunately, you really don't have to go too deep into vectors here for what you are doing. I have some tutorials on vectors on VirtuallyProgramming.com, but can't provide the exact hyperlink at the moment. Its in the "pre-tutorials" section although I can't remember how that's titled.

To give you the summary: a vector is basically an "arrow" that represents a direction that is permenantly tied to an amount. The length of the arrow represents the "amount" and the direction from tail to head represents the direction. The tail and the head are two seperate points. However, this is un-necessary because the exact position of the head and tail do not matter when your only goal is to relate the direction and the amount. That is the difference between vectors and "arrows". Because of this, the tail of the vector is always assumed to be at X=0, Y=0. Only one point is then needed to represent the position of the head.

Your ballSpeed is a true vector of this sort.

private Vector2 ballSpeed = new Vector2(2,2);
This sets the ball speed to be 2 in the X direction and 2 in the Y direction. Or more accurately it creates a vector (arrow) that has its tail at 0,0 and its head at 2,2 indicating that its direction is that of pointing from 0,0 to 2,2 and its amount (speed) =SQRRT(X^2 + Y^2) (Pythagorean Theorum). (I assume this is per second and will have to be divided by the number of frames per second (probably 60) in order to make this units per second.) Also, note that the vector values can be decimal values making it possible to point any possible direction in 360 degrees down to minutes and seconds.


private Vector2 ballPos = Vector2.Zero;
(Vector2.Zero is actually not a vector either, but X=0,Y=0). ballPos is writen as a vector but its not a true vector. It is simply a position. That's confusing. But we often represent a position as a vector because you can do "vector addition" if both of them are vectors. (Vector addition here would have the ball position as an arrow from 0,0 to the ball position. And vector addition would "add" the speed vector by mathematically applying the tail of the speed vector against the head of the position vector. That vector addition gives you the new position after the speed/velocity is applied.

Your ballSpeed is movement per second, or actually in this case probably movement per frame (probably 1/60th of a second).

One of the "tricks" regarding vectors is that you can reverse their direction simply by multiplying the vector times the "scalar (non-vector)" value of negative one.

In your code above, you are not multiplying the entire vector by negative one, which would perfectly reverse coarse. Instead, you are multiplying just the X or the Y componant by negative one to only reverse THAT componant of the motion.

So if you hit a wall on the X side of the screen, then by multiplying the X componant of the vector by negative one you reverse its motion in the X direction without reversing its motion in the Y direction. Likewise, if you hit a wall on the Y side of the screen, then by multiplying the Y componant of the vector by negative one you reverse its motion in the Y direction.

This is actually pretty simple. This is axis aligned reflection where the "walls" you are reflecting off of are perfectly aligned with the X/Y axis. If you are reflecting off of an angled surface, this isn't going to work at all because its overly simplified. But it works perfectly well if the surfaces are all aligned with the axis. This is the way you usually start out doing this.

Now, you mentioned normals. I did not see any vector normals in your code, so I wasn't sure what you were talking about. Hopefully, you are not getting into vector normals, because I would need several more pages to explain that. Suffice it to say that vector normals are used when attempting to do a reflection on a non-axis aligned surface. Vector normals are the "more complicated" way of doing this which are not concerned with the surfaces being axis aligned. For now, I would stick with this axis aligned method as it's FAR more mathematically simple.

To give you a preview of using vector normals: a normalized vector is a vector (arrow) with a length of one regardless of what direction it points ( SQRRT(X^2 + Y^2)=1) [Pythagorean Theorum]. Because it has no amount (length is always one) it can be used for some special purposes, but always represents a direction without an amount.

In the case of reflections, the vector normal represents the direction the surface is facing. Its a normalized vector because it doesn't "face that direction" by an amount; it simply faces that direction (there is no amount involved with facing). Using a vector normal to represent facing direction of the surface allows the surface to not be forced into being aligned with the axis in order to do the math. Once you know which direction the surface you are reflecting off of is facing (represented by the normal vector), you can then use Trig to cause the angle of reflection to be exactly the same as the angle of incidence that you are striking the surface from. It will leave at the exact same angle it arrived at, but in the opposite direction. Trig allows you to calculate the angles involved when you have a normal vector telling you what direction the surface is facing.

But again, this is unnecessary when the surface is pointed straight down the X or Y axis, because simply reversing the sign of the one componant will cause the correct reflection.

This post has been edited by BBeck: 21 June 2013 - 12:51 PM

Was This Post Helpful? 2
  • +
  • -

#5 dreaminxna  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 53
  • Joined: 10-June 13

Re: angle of reflection and incidence?

Posted 21 June 2013 - 07:47 PM

View PostBBeck, on 21 June 2013 - 12:11 PM, said:

To give you the summary: a vector is basically an "arrow" that represents a direction that is permenantly tied to an amount. The length of the arrow represents the "amount" and the direction from tail to head represents the direction. The tail and the head are two seperate points. However, this is un-necessary because the exact position of the head and tail do not matter when your only goal is to relate the direction and the amount. That is the difference between vectors and "arrows". Because of this, the tail of the vector is always assumed to be at X=0, Y=0. Only one point is then needed to represent the position of the head.


apology accepted :D/>/> but honestly you dont have to apologize :D/> . Answering my post is enough for me and Im thankful to that.

In your answer, does that mean I should stop treating vector as a position? because thats what I always do. I just visualize vector as a position. And the direction is a matter of just adding the x and y value. Say if i have a instance of
Rectanlge(new Vector(2,2),width,height)
, meaning that the vector is from 0,0(top-left right?) I move to (2,2) and that is now the position. I dont know if this is correct to visualize. But i imagine that the vector is always 0 both x and y (top-left), now when I initialize my vector in load content, I visualize it as moving slowly from 0,0 to 2,2. So that is the amount or the length of my vector to travel, from starting position of 0,0 until i reached 2,2. And when I reached 2,2, i can simply assume that its now the position because that's the end of my rectangle journey from 0,0. Is that the correct visualization of vectors? and by the way is it safe to assume my 0,0 is always top-left when it comes to vector? I just cant visualize the tail in other area of the screen beside top-left :D/> . I will keep reading your post until i get this vector :D/> .Thanks for the tutorial. I think the vector stuff is on the fundamentals tab with title "whats your vector victor" :D/> . And about the normals, yes i was talking about the normals in getting the angle of reflection. Because thats what i saw on some forum. They say you must get the angle of reflection in order to bounce the ball correctly. And since you point out that i still dont understand vector in which you are correct i think i will skip for that part for a moment and get back to it once i fully understand vectors. Thank you very much :D/> :genius:/>
Was This Post Helpful? 0
  • +
  • -

#6 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 579
  • View blog
  • Posts: 1,286
  • Joined: 24-April 12

Re: angle of reflection and incidence?

Posted 22 June 2013 - 10:04 AM

"Should you stop treating 2D vectors like positions?" That's a good question.

I'll start by saying that most beginners are going to think of it exactly like you are. And that will work. You could continue to think of them that way.

However, if you ever want to do 3D, you are going to be totally lost if you continue to think of vectors as positions/coordinates/points. And you may have problems with vectors in physics in 2D thinking of them that way, although you are certain to have problems with 3D physics if you are still thinking of vectors that way.

So, in short, the sooner that you can start treating vectors like vectors, rather than points/coordinates/positions the better game programmer you will be and the more prepared you will be to move on to more advanced topics. But, the stuff you are working on will work if you're thinking of it the way you described.

I mostly work in 3D and haven't done a whole lot of 2D. But even if I were doing 2D I would think of vectors the same way.

I would think of an object's position as a point that happens to be stored in an XNA object called a vector. I would realize that I'm storing as a vector, instead of as a "point" object, because I plan on doing vector math on it. IF I thought of it as an actual vector, I would imagine it as a "displacement" from the origin (x=0,y=0). And I would imagine an arrow from the origin to the point stored in the vector object.

But generally I would imagine the object's position as a point. However, I would always imagine the velocity/speed/movement as an actual vector. I would think of it as an arrow of its own that represents the direction of the movement. And I would imagine the length of this arrow as representing the speed of the movement. (Later in physics, forces will be represented the same way. So if one object hits another object, what direction does the original object get knocked in?) I would imagine this movement arrow attached to nothing other than the origin. But when determining how this movement per frame affects the object's position each frame, I would imagine putting the tail of my velocity/speed/movement vector/arrow on the point of the object's position and the new position being where the arrow points to.

This is not really different than the way you talked about thinking about it. It's still just a displacement of the object's position by a given amount in a given direction.

But doing it as a vector will make it easier in the long run. And it will become almost an absolute necessity by the time you get into 3D.

For example, you could write code to move every frame like this:


Vector2 ObjectsPosition = new Vector2(20f,20f); //Use floating point to be able to position between grid squares/pixels.
Vector2 ObjectsVelocity = new Vector2(0f, 0f); //This is movement per frame, but I'm starting at no movement.

//Move Object (although with zero motion)
ObjectsPosition.X += ObjectsVelocity.X;
ObjectsPosition.Y += ObjectsVelocity.Y;

//Give it some motion
ObjectsVelocity.X += 0.124;  //What angle, or direction, is this? Who knows.
ObjectsVelocity.Y += -0.043; //What over-all speed is this? Hope you know trig!

//Move Object (Now we're moving.)
ObjectsPosition.X += ObjectsVelocity.X;
ObjectsPosition.Y += ObjectsVelocity.Y;

//Change heading by 32 degrees.
float HeadingChange = MathHelper.ToRadians(32f);  //Convert degrees to radians.

ObjectsVelocity.X = ObjectsVelocity.X*(float)Math.Cos(HeadingChange) - ObjectsVelocity.Y * (float)Math.Sin(HeadingChange);

ObjectsVelocity.Y = ObjectsVelocity.X*(float)Math.Sin(HeadingChange) + ObjectsVelocity.Y * (float)Math.Cos(HeadingChange);

//Move Object (Now we're moving in a new direction but at the same speed.)
ObjectsPosition.X += ObjectsVelocity.X;
ObjectsPosition.Y += ObjectsVelocity.Y;

//Reverse coarse 180 degrees.
ObjectsVelocity.X = -ObjectsVelocity.X;
ObjectsVelocity.Y = -ObjectsVelocity.Y;

//Move Object (Now we're moving in the opposite direction.)
ObjectsPosition.X += ObjectsVelocity.X;
ObjectsPosition.Y += ObjectsVelocity.Y;

//Now what if we want to change the speed/velocity to 33% without changing the direction?
//???
//...
//Or worse, what if you want to set an exact speed of 4.2 units per frame without changing the 
//direction of travel?
//Since you're not thinking of it as a vector you've got a bit of an issue there.




Now lets look at the same thing written as actual vectors instead of treating the vectors like they are points:

Vector2 ObjectsPosition = new Vector2(20f,20f); //Use floating point to be able to position between grid squares/pixels.
Vector2 ObjectsVelocity = new Vector2(0f, 0f); //This is movement per frame, but I'm starting at no movement.

//Move Object (although with zero motion)
ObjectsPosition += ObjectsVelocity;

//Give it some motion
ObjectsVelocity = new Vector2(0.124, -0.043); 
//A better way would be to give it new Vector2(1.0f, 0f) multiplied by desired velocity per frame and then rotate it to the heading you want. But I was trying to make it match the previous code.

//What over-all speed is this? We can know regardless of the direction of travel.
float OverAllSpeed = ObjectsVelocity.Length();  //The length of the vector is its speed per frame.

//Move Object (Now we're moving.)
ObjectsPosition += ObjectsVelocity;

//Change heading by 32 degrees.
float HeadingChange = MathHelper.ToRadians(32f);  //Convert degrees to radians.

//This code is still basically the same. This is how you rotate a vector's 2D direction.
ObjectsVelocity.X = ObjectsVelocity.X*(float)Math.Cos(HeadingChange) - ObjectsVelocity.Y * (float)Math.Sin(HeadingChange);

ObjectsVelocity.Y = ObjectsVelocity.X*(float)Math.Sin(HeadingChange) + ObjectsVelocity.Y * (float)Math.Cos(HeadingChange);

//Move Object (Now we're moving in a new direction but at the same speed.)
ObjectsPosition += ObjectsVelocity;

//Reverse coarse 180 degrees.
ObjectsVelocity *= -1;

//Move Object (Now we're moving in the opposite direction.)
ObjectsPosition += ObjectsVelocity;

//Now what if we want to change the speed/velocity to 33% without changing the direction?
ObjectsVelocity *= 0.33f; //It's that simple.

//What if we want to set the velocity to 4.2 without changing direction?
//Simply set the length of the arrow/vector you want by setting its length to 1 (normalizing)
//and multiplying by the length (speed per frame) you want.
//Setting its length to one loses any previous velocity information and sets its velocity to one
//unit per frame. But the key here is that 1*Whatever=Whatever and this will change the 
//vector's length to be that number.
ObjectsVelocity.Normalize();  //Normalize to make the math for the change work out right.
ObjectsVelocity *= 4.2f;  //Set the exact velocity regardless of direction of travel.




Hopefully, you can see how much easier the second example is in code. It's more difficult because you have to understand vectors in order to understand why it works. But its much more simple in terms of how the code is written. And this is how you will be doing it when you get into 3D and High Level Shader Language anyway. So, the sooner you learn it, the better. At least that's what I think.

I believe X=0, Y=0 is always going to be top left of the screen in 2D. But where is X=0, Y=0, Z=0 in 3D? In 2D, you "may" want X=0,Y=0 to be center of the screen, but then you have to do a bunch of conversion math to convert everything to center of the screen (0.5*screenwidth, 0.5*screenheight). That would then allow X and Y to go negative and still be on the screen. A lot of times I'm thinking in 3D where the origin (0,0,0) is at the center of the imaginary universe, but who knows where it is on screen (that's dependent on camera position, etc.).

So, when you're visualizing vectors attached to the origin (0,0) maybe imagine the origin in the center of the screen because they can point in the negative directions/quadrants (which would be offscreen normally in 2D). Most of the time I'm thinking of position as just a point and velocity as an actual vector.

Don't worry too much about normal right now until you get basic motion going comfortably. You don't need them really until you start bouncing off angled surfaces. The normal for the sides of the screen would all be pointing straight down the X or Y axis. That's why they can largely be ignored for what you're doing. (Technically when I normalized the vector in my example it became a normal vector, or normal. But don't worry about that either.)

The bottom line is that Microsoft decided to build Vector2 and Vector3 objects into XNA. They did not build point objects into XNA. That should serve as a hint that they believe you should think of vectors as vectors and use them as vectors rather than thinking of them as points. As normalizing, doing a dot product on, doing a cross product on, or multiplying points together doesn't really make sense. But doing all that to vectors makes complete sense.

This post has been edited by BBeck: 22 June 2013 - 10:35 AM

Was This Post Helpful? 2
  • +
  • -

#7 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 579
  • View blog
  • Posts: 1,286
  • Joined: 24-April 12

Re: angle of reflection and incidence?

Posted 22 June 2013 - 10:50 AM

In the example you gave:

Rectanlge(new Vector(2,2),width,height)

They are kind of "cheating". It really should be:

Rectangle(XPosition,YPostion,width,height)

But since your position is already stored in a vector object its probably easier to just do:

Rectangle(ObjectsPosition,width,height)

So, in this case you probably should just think of it as a point.

They could go one step further and use a vector to represent an arrow from top left to bottom right of the rectangle to define it:
OffsetToBottom.X=width;
OffsetToBottom.Y=height;
Rectangle(ObjectsPostion,OffsetToBottomRight)

ObjectsPostion in that example is the top left. You may actually see something similar in code sometime where they are defining a rectangle or especially a 3D box. Using 3D vectors a 3D box can be defined using just those two 3D vector values assuming that the box is axis aligned.

One of the reasons that learning vectors is so confusing, I think, is because we "cheat" all the time and put values in our vector objects that don't really represent vectors, such as positions/points or even vertices. (Vertices are points with additional information attached to them, such as texture coordinates, that are often stored or declared as vectors even though they really have nothing to do with vectors.)

This post has been edited by BBeck: 22 June 2013 - 10:56 AM

Was This Post Helpful? 1
  • +
  • -

#8 dreaminxna  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 53
  • Joined: 10-June 13

Re: angle of reflection and incidence?

Posted 23 June 2013 - 07:10 AM

View PostBBeck, on 22 June 2013 - 10:04 AM, said:

"Should you stop treating 2D vectors like positions?" That's a good question.

I'll start by saying that most beginners are going to think of it exactly like you are. And that will work. You could continue to think of them that way.


Ill start by saying thank you very much for guiding me :D I just viewed vectors as vector and its kind of hard for me as Im used in viewing it as a position hehe :) but i will just keep reading your post then examine the code you gave. But im sure sooner or later i will be comfortable in vectors. Ill just keep trying :D :gunsmilie: And Yes I am planning to do a 3d some day and some physics but for now if i cant master 2D i cant move to 3D :D :rockon:
Was This Post Helpful? 1
  • +
  • -

#9 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 579
  • View blog
  • Posts: 1,286
  • Joined: 24-April 12

Re: angle of reflection and incidence?

Posted 23 June 2013 - 08:24 AM

Vectors are one of those things that blow peoples' minds and they just can't seem to understand it. That is, until they do. (Seriously, although we only spent a couple of weeks on it, I swear not a single person in my trig class really got it including the guy teaching the class. Several people passed the test, but passing the test does not necessarily mean you understood.)

If you keep trying to understand it, you'll eventually have an epiphany and wake up one morning going "Duh! How did I not understand that?" I think the problem for most people is A) not having it properly explained and Bee) trying to think that its more complicated than it really is. Mostly its just about imagining these "arrow objects" in 2D or 3D space. There's an unbelievable amount of confusion around vectors for some reason, and all I can think of is that a lot of people who use vectors mathematically still haven't really reached that "Ah ha!" moment where it all becomes clear.

Most of the example code I wrote above actually covers about 80% or more of everything you'll do with vectors in 2D. Probably the most complicated part of vectors is understanding vector dot products and cross products and you can probably forget about that, or ignore it, in 2D. For Oriented Bounding Rectangles in 2D you may get into vector dot products (or is it cross products, I always get the two names confused), but you're still a fair ways away from that. And even then it's likely to be copy and paste code that you don't necessarily have to fully understand to use.

You may want to look up some tutorials on vector math, depending on your math background. You first learn about vectors in Trigonometry and then you get fully into them in very high level math courses that I've never taken. I kind of figured them out on my own mostly from working in game programming and using them. But seeing the actual math may make it easier or may just confuse you more. I find that you get a lot of people explaining them that may not really understand them themselves. If it seems terribly confusing, I or whoever is explaining it is probably just not doing a good job explaining it.

Having a solid understanding of trig before going into vectors helps tremendously too. For an actual math course, its a requirement. You have to have trig to do vector rotations (or rotations of any type for that matter) and measuring vectors (such as finding the length of a vector). But XNA does a lot of the math for you with methods like Length(). So, I don't know that you necessarily need to know trig to understand how to use vectors in XNA.

Probably the big thing in XNA is to get away from using the individual X and Y components inside the vector and treating the vector as an object the way I did in the final example. Practice will likely make things clearer.

Oh. And Kahn Academy on the internet probably has some videos that cover the math behind vectors, although it may be just more confusing than helpful. Still, it may be worth checking out some of the tutorials on vector math on the internet when trying to understand them.

Almost all you'll ever be doing with vectors in 2D is adding them to combine them, calculating what angle they point at (which I didn't include code for), setting what angle they point at, calculating their length, and setting their length. If you know how to do all that, you pretty much know how to manipulate vectors as much as you need in 2D.

You don't really need to know how the math works as much as you just need to know how to put them to use. And XNA does most of the math for you anyways.

Mainly, think of true vectors as objects that store a specific direction along with an amount (an amount of speed or distance, an amount of force, or an amount of something else). Thinking of vectors as objects that keep track of direction may help you start thinking in the right direction. You've been using them as objects that store 2D amounts of displacement, but focusing on them as directional objects may help make the distinction.

Perhaps try and use a vector to give an object on the screen a heading and work to control the exact heading in degrees as you move it around the screen. I think focusing on their directional part may help.

Good luck!

This post has been edited by BBeck: 23 June 2013 - 08:53 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1