 |
|
| Computers Forum Index » Computer - Games Programming (Algorithms) » Curved movement... |
|
Page 1 of 1 |
|
| Author |
Message |
| Bill... |
Posted: Wed Oct 29, 2008 7:19 pm |
|
|
|
Guest
|
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. Ideally adjusting the
amount of curvature and which side of the set of points the curve occurs
would be great. Any help would be appreciated, thanks. |
|
|
| Back to top |
|
|
|
| Alfie [UK]... |
Posted: Sat Nov 01, 2008 1:13 am |
|
|
|
Guest
|
On Thu, 30 Oct 2008 09:58:35 -0400, Bill <Bill at (no spam) hotmail.com> wrote:
Quote: 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. Can you please
help me produce an elliptical path?
I can see an issue with your code; ATAN2 usually takes it's parameters
as 'y, x', not 'x, y'.
You also shouldn't need the '+ 270.0f...', etc I presume you put that
in to try to 'rotate' the result caused by the switch of x and y.
<- UPDATED HALF CIRCLE CODE ->
vector<myVector> path;
int steps( 32 );
myVector centerPoint( ( endPoint - beginPoint ) / 2.0f + beginPoint );
float radius( ( centerPoint - beginPoint ).Length() );
float startAngle( atan2( beginPoint.y - centerPoint.y , beginPoint.x -
centerPoint.x ) );
float endAngle( atan2( endPoint.y - centerPoint.y , endPoint.x -
centerPoint.x ) );
float angleStep( ( startAngle - endAngle ) / steps );
while( steps-- )
{
path.push_back( MyVector( centerPoint.x + cos( startAngle ) *
radius, centerPoint.y + sin( startAngle ) * radius ) );
startAngle += angleStep;
}
To 'flatten' the curve you'll basically need to choose a point
'behind' the current center along the line of the perpendicular
bisector (of start and end points), then recalculate your start and
end angles.
I need to dig out my old trig/geometry notes to work out the 'easy'
way to do this stuff...I mostly do VB6 maintenance now so my C++ is a
bit rusty
--
Alfie [UK]
<http://www.delphia.co.uk/>
Spark's Sixth Rule for Managers: If a subordinate asks you a question, look at him as if he is mad. When he looks down, paraphrase the question. |
|
|
| Back to top |
|
|
|
|
|
All times are GMT
The time now is Mon Nov 30, 2009 12:19 am
|
|