Sign in to follow this  

How to get sprite to move in a circle

This topic is 3729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to create a gyruss type game (space invaders, just moves in circles) using c++ and SDL. I need to get my sprite to move around in a fixed circle. I'm not sure as to how to go about coding this. From what i understand, i need to use sin and cos angles in radians to create the path the sprite can move along.

Share this post


Link to post
Share on other sites
sin and cos are 90 degrees out of phase.

Have the x component of the sprite's location vary with one, and the y component of the sprite's location vary with the other and you will get circular motion.

x = ax*sin (t+bx)
y = -ay*cos (t+by)

Where a affects the radius of the circle and b affects the phase (you can change the circular motion into ellipsoidal motion).

Share this post


Link to post
Share on other sites
Thanks.
I'm fairly new to programming, so could u please help me out with the coding. What would the code for the circular path or movement of the sprite be?

Share this post


Link to post
Share on other sites
Nobodies going to code in for you.

1. Nobody has time enough for their own projects, let alone yours.
2. You haven't given us a glimpse of your current code.

So first things first, have you gotten your sprite to move in a straight line? If so, calculating the points he must move to and executing them in some sort of list shouldn't be to hard. The Unbeliever has outlined the general formula for it.)

If you're having problems with the trigonometry, Google "Pure Math 30 Explained" and click on the trigonometry subsection.

Share this post


Link to post
Share on other sites
From one of my source files:

class rotate_around
: public tfun<vect2d>::base
{
tfun<vect2d> center;
tfun<float> angle;
tfun<float> radius;

public:

circular_motion(
const tfun<vect2d> & center,
const tfun<float> & angle,
const tfun<float> & radius)
: center(center), angle(angle), radius(radius) {}

vect2d at(time t) const
{
const float angle = this -> angle.at(t);
const float radius = this -> radius.at(t);
const vect2d center = this -> center.at(t);

return center + vect2d::polar(angle,radius);
}
};

Share this post


Link to post
Share on other sites
Thanks.
I've used the following code to get it to move in a circle, which it does, but when i alternate between the left and right key, the sprites' position changes. It doesn't move around the same circular path. It moves to a different area on the screen where it then moves in a circle. Any ideas?

int r = 10;
float angle = 0;
//Starting position of sprite on screen
float batX = 400;
float batY = 400;

if ( keysHeld[SDLK_LEFT] )
{

batX -= r*cos(angle);
batY += r*sin(angle);
angle-=0.05;
}

if ( keysHeld[SDLK_RIGHT] )
{
batX += r*cos(angle);
batY += r*sin(angle);
angle-=0.05;
}

Share this post


Link to post
Share on other sites
Try changing the 'angle' differently in the two cases, *interpreting* the angle the same way in the two cases, and *not* "integrating" the angle's effect by adding to BatX/BatY each frame (instead, set them to the original value plus whatever from the angle calculation).

Share this post


Link to post
Share on other sites

This topic is 3729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this