开发者

Find radians reflection angle

I'm coding a simple Flash game, just to learn flash and improve my maths, but I'm getting very confused with Radians as ther开发者_运维百科e are new to me.

What I've done so far is using your mouse you (click & release) you shoot a ball off in that direction using radians. Now what I'd like to happen is when the ball hits the wall it bounces off in it's reflection angle. Eg. if the ball hits the right-hand-side wall travelling at in a radians of -0.65 it should bounce back in the radians of about -2.5

Could some please explain to me how I go about doing this? Thanks


If you work with radians, you should (always) express them in Pi, simply divide by Pi. Make it much easier to work with and understand.

-0.65 rad = -0.207*Pi rad
-2.5 rad = -0.796*Pi rad

A full circle is 2*Pi. The normal for a horizontal surface is 0.5*Pi (or -0.5*Pi).

normal = 0.5*Pi
angle of incidence = i + Pi - 0.5*Pi //EDIT: +Pi, need to invert the direction
angle of reflection = 0.5*Pi - angle of incidence

See Reflection on wikipedia for a image and some explanation. That gives this formula for a horizontal wall:

r = 0.5*Pi - (i + Pi - 0.5*Pi)
r = 2*Pi - i 
r = -i //subtract 2*Pi

For example

i = -0.207*Pi //gives
r = 0.207*Pi //for a horizontal wall

Similar for vertical wall:

r = 0 - (i + Pi - 0)
r = -Pi - i

EDIT I realized this was not correct. For the formula to work, i has to point exactly in the opposite direction. Fixed it, hopefully it is correct now. If I got time later, I'll include an image explaining the mistake and solution.


It's much easier to do this sort of thing using Vectors (as in point Vectors, not Vector array).

Assuming your circle has a Vector v (the direction that it's travelling) and you're colliding against a line that has a normalised normal Vector n (the normal is the Vector that runs perpendicular to the line - normalised means that it has a length of 1), then you can calculate the reflection like this:

// gets the dot product between 2 points
public function dot( a:Point, b:Point ):Number
{
    return ( a.x * b.x ) + ( a.y * b.y );
}

// reflects a point
public function reflect( p:Point, dir:Point ):void
{
    // using the formula [R = V - (2 * V.N) * N] or [V -= 2 * N * (N.V)]
    var vn2:Number  = 2.0 * MathHelper.dot( p, dir );
    p.x = p.x - ( dir.x * vn2 );
    p.y = p.y - ( dir.y * vn2 );
}

Then call reflect( v, n );

Depending on which side of the line you're colliding with (left or right), you may need to multiply the final x and y by -1

For a good overview of Vectors check out http://tonypa.pri.ee/vectors/start.html (it's old, but the concepts are sound)


With some simple math you can convert radians to degrees and vice versa:

var radians :Number = 90 * (Math.PI / 180);
var degrees :Number = radians * (180 / Math.PI);
trace(radians, degrees)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜