Re: Please help with elliptical path algorithm




"Bill" <Bill@xxxxxxxxxxx> wrote in message
news:oNoOk.81901$XB4.36785@xxxxxxxxxxxxxxxxxxxxxxxxx
I am working on a 2D game in which I would like to move a sprite from one
location on the screen to another but instead of moving in a straight line
I would like the path to be curved. The starting point and ending points
can be anywhere on the screen. An elliptical path is what I want. Here is
my first attempt, but it is using a circular path and isn't working all
that great.

vector<myVector> path;
int steps( 32 );
myVector centerPoint( ( endPoint - beginPoint ) / 2.0f + beginPoint );
float radius( ( centerPoint - beginPoint ).Length() );
float startAngle( atan2( centerPoint.x - beginPoint.x, centerPoint.y -
beginPoint.y ) + 270.0f * ( PI / 180.0f ) );
float endAngle( atan2( centerPoint.x - endPoint.x, centerPoint.y -
endPoint.y ) + 270.0f * ( PI / 180.0f ) );
float angleStep( ( endAngle - startAngle ) / steps );

while( steps-- )
{
path.push_back( MyVector( centerPoint.x + cos( startAngle ) * radius,
centerPoint.y + sin( startAngle ) * radius ) );
startAngle += angleStep;
}

I don't quite get this (C++?) code.

But I assume you have a start point A, and endpoint B, and you have an
ellipse with one of the axes being AB and want to get from A to B by
following one half or the other of the ellipse's circumference.

Well an ellipse can be considered just a circle, stretched in one axis by
some factor. In this case stretched along the perpendicular axis to AB.

My graphics skills are limited so I found it easier to start with a
normalised circle of radius 1, with An being at (1,0), and Bn at (-1,0), and
to draw a semicircle (through (0,1)) from An to Bn.

Then to scale this little circle to the size of the full circle (using the
nominal radius of the full ellipse), apply the elliptical scale factor along
Y, rotate to the orientation of your original AB, and to translate the
centre to (A+B)/2. This was done with a matrix (I used a 3D matrix but a 2D
one should do as well) to simplify things:

A,B are the start and end points; F is the stretch factor (1 is normal
circle, <1 or >1 for an ellipse), and steps is the resolution (so the actual
output will be a polygon). line() is some display function:

FUNCTION DRAWPATH(a,b,f,steps)=

centre:=(a+b)/2
radius:=linelength(centre,a)
rot:=getangle(centre,a)

m:=matrix(cos(rot)*radius,sin(rot)*radius,0,
-sin(rot)*radius*f,cos(rot)*radius*f,0,
0,0,1,
centre.x,centre.y,0)

p:=(1,0)
angle:=angleincr:=pi/steps

for i:=1 to steps-1 do
q:=(cos(angle),sin(angle))

line(p*m,q*m)
p:=q
angle+:=angleincr
end
line (p*m,(-1,0)*m)
END

(BTW this is not C++ code.)

--
Bartc

.



Relevant Pages

  • Re: Another OO Problem (and thanks for the input I received so far)
    ... > cirle as a special kind of ellipse. ... If I did decide that a circle should be a sub-class of ellipse, ... I'd inherit and hide the second focal point and radius and add a class ...
    (comp.programming)
  • Re: Find center of ellipse
    ... Suppose to have a circle centered in the origin with radius 'r'. ... you want to find the center of this ellipse when it touches the circle ... Perhaps you mean to require the axes to parallel the ...
    (sci.math)
  • Re: two little angel/degrees graphing questions
    ... > Cos* Radius = X ... parametric equations of the circle. ... Equation of ellipse, centered at with axis along the x and y ...
    (sci.math)
  • [SUMMARY] The Smallest Circle (#157)
    ... about the smallest enclosing circle problem... ... def initialize(*coords) ... y_big is the minimum radius of a circle ... always been the square of the radius, we finally take its square root ...
    (comp.lang.ruby)
  • Re: need help with homework
    ... So if you solve for circumference you get pi x diameter, ... Now if you know relation between diameter and radius, ... Center of circle splits diameter into two radii. ... circular disk, i.e. a disk whose perimiter is a circle. ...
    (sci.math)