• Create Account

### #ActualMichael Tanczos

Posted 22 May 2012 - 11:57 AM

You need to compute the surface normal for the wall first, which is basically a vector that points out in a perpendicular direction. To do that you'll need two points that make up the edge of the wall.. the following code should work fine for that purpose:

public Vector2 ComputeNormal(Vector2 point1, Vector2 point2)
{
Vector2 normal = new Vector2();
normal.X = point2.Y - point1.Y;
normal.Y = point1.X - point2.X;

normal.Normalize();

return normal;
}



That will at least return a unit vector perpendicular to the wall, and then you can use the built in reflect method to do the reflection.. something like:

normal = ComputeNormal(wallpoint1, wallpoint2);
shotVelocity = Vector2.Reflect(shotVelocity, normal);


Your direction vector is essentially the same thing as a velocity vector I'd imagine so long as you aren't making sure it's always a unit vector (unit vectors are a vector with magnitude one, which is direction only)

### #1Michael Tanczos

Posted 22 May 2012 - 11:51 AM

You need to compute the surface normal for the wall first, which is basically a vector that points out in a perpendicular direction. To do that you'll need two points that make up the edge of the wall.. the following code should work fine for that purpose:

public Vector2 ComputeNormal(Vector2 point1, Vector2 point2)
{
Vector2 normal = new Vector2();
normal.X = point2.Y - point1.Y;
normal.Y = point1.X - point2.X;

normal.Normalize();

return normal;
}



That will at least return a unit vector perpendicular to the wall, and then you can use the built in reflect method to do the reflection.. something like:

normal = ComputeNormal(wallpoint1, wallpoint2);
shotVelocity = Vector2.Reflect(shotVelocity, normal);


PARTNERS