Jump to content
  • Advertisement
Sign in to follow this  
teskovalue

2d angle of reflection error?

This topic is 2637 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

Hello everybody, I have been trying to get the correct reflection from an angled surface at 45 degrees from an incoming direction. I had a little look at a formula on some explanation of ray tracing I found. I haven't tested it extensively but it looks roughly OK.
My 2d coordinate system is standard 0,0 at the top left to screen-width, screen-height on the bottom right. I have a test right angle triangle represented by 3 line segments and a single point which I am testing the collision with for now before I expand to a box. Here's a little diagram of what happens on screen.

S is the start position, O is the collision etc.

x x x x x x x x
x x x x x x x
x x x x x O o o o o o o <-S
x x x x x-o
x x x x....o
x x x.......o
x x..........o
x.............o


So I thought this is fine BUT all is not fine, because the incoming vector was say (3,0) and the new outgoing vector is (2.38419e-07, 3) hmmm. So if I carry on and hit another right angle this time its way off.

My code for getting the reflection is here:



void get_reflection_vector(float &_vx, float &_vy)
{
Vector2<float> in(_vx, _vy);
Vector2<float> surface(ls[closest_line].p1.x - ls[closest_line].p2.x,
ls[closest_line].p1.y - ls[closest_line].p2.y); //ls is a std::vector of line segments
Vector2<float> n = surface.norm();
float n_dot_in = -n.dot(in) ;
Vector2<float> out = in.add(n.mul(n_dot_in * 2.0f ));
_vx = out.x;
_vy = out.y;
}


I know its a bit messy and should return a vector and stuff but this is just me starting out and trying to get it working quick and dirty.

Relevant Vector2 members are:

Vector2<T> norm()
{
float len = this->getlen();
Vector2<T> res(x/len, y/len);
return res;
}

Vector2<T> mul(T scale)
{
Vector2<T> res(x*scale, y*scale);
return res;
}

Vector2<T> add(Vector2<T> v)
{
Vector2<T> res( x + v.getx(), y + v.gety() );
return res;
}

T dot(Vector2<T> v)
{
T res = x * v.getx() + y * v.gety();
return res;
}


float getlen() { return sqrt( (x*x) + (y*y) ); }



I'm probably going to feel a bit stupid as I expect its something really simple but I cant see what's wrong. Can anyone help?

Share this post


Link to post
Share on other sites
Advertisement
Had a little look around, seems I needed a dot product of the surface rather than dividing it by its length. This works fine now.

void get_reflection_vector(float &_vx, float &_vy)
{
Vector2<float> in(_vx, _vy);
Vector2<float> surface(ls[closest_line].p2.x - ls[closest_line].p1.x,
ls[closest_line].p2.y - ls[closest_line].p1.y);
float dot_norm = surface.dot(surface);
float dot_in = surface.dot(in);
Vector2<float> out = in - surface * ( (dot_in/dot_norm) * 2.0f );
_vx = -out.x;
_vy = -out.y;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!