4

I'm trying to make a nice deflection effect in a little physics engine I've made. Right now it deflects nicely off the normal of a polygon edge. But instead of making a polygon with 100 edges to get a smooth effect of a "rounded deflection" I figured I could calculate the deflection normal using an ellipse instead.

So, what I'd really like is a function that takes a point P on a line segment and returns the normal N on the circumference of an imaginary ellipse(w,h). See the attached picture for some details.

A picture of the problem

To get a point on the circumference of an ellipse I'm pretty sure it's:

x=P.x+Math.sin()*w
y=P.y+Math.cos()*h

but how can I get the normal from that?


Here's a fiddle with an attempt to implement the answer by Dr BDO Adams.

4

2 回答 2

5

Equation of an ellipse point is

x=x_centre+a*cos(t)
y=y_centre+b*sin(t)

For each point of ellipse you can find t as atan2( (y-y_centre)/b , (x-x_centre)/a )

When you know t tangent direction can be determined: dx/dt,dy/dt:

dx=-a*sin(t)
dy=b*cos(t)

When you know tangent direction, just rotate it by 90 degrees and you have a normal:

nx=b*cos(t)
ny=a*sin(t)

And to avoid calculating t we can combine it with the first two formulas:

nx=(x-x_centre)*b/a
ny=(y-y_centre)*a/b
于 2012-11-23T16:31:38.577 回答
0

First Use the atan2 function to get the angle of at the normal, and get a vector from that

theta = atan2(2y/semiminorradius, x/semimajorradius)
ny = semiminorradius * sin(theta)
nx = semimajorradius * cos(theta)

Do you need the normal vector to be normalised? (unit length) if so

r = sqrt(tx^2+ty^2)
nny = ny/r
nnx = nx/r

As you drawn it the point is actually (ny,nx)

于 2012-11-23T11:47:12.490 回答