SFML community forums

Help => Graphics => Topic started by: kursukia on January 13, 2014, 06:25:33 pm

Title: Circular movement
Post by: kursukia on January 13, 2014, 06:25:33 pm
Hello,

I want to let move circle in a circular path. How can I do that?

I tried this. It works, but this was not the solution for my problem => its completly wrong:

        //if(stoney <= 100){down=true; up=false;}
        //if(stoney >= 350){down=false; up=true;}

        //if(down==true,up==false)
        //{
        //      stone.move(0,Speed*ElapsedTime);
        //}
        //else if(down==false,up==true)
        //{
        //      stone.move(0,-Speed*ElapsedTime);
        //}

        //if(stonex <= 125){right=true; left=false;}
        //if(stonex >= 375){right=false; left=true;}

        //if(right==true,left==false)
        //{
        //      stone.move(Speed*ElapsedTime,0);
        //}
        //else if(right==false,left==true)
        //{
        //      stone.move(-Speed*ElapsedTime,0);
        //}

        //std::cout << "X: " << stonex << " Y: " << stoney << std::endl;

I think, I have to work with sin and cos, but I dont know how I can do that..

kurs
Title: Re: Circular movement
Post by: zsbzsb on January 13, 2014, 06:44:02 pm
Code: [Select]
X = sin(angle) * speed
Y = cos(angle) * speed

Don't forget that if you use std::sin and std::cos it takes radians, not degrees.  ;)


Code: [Select]

      270
       |
180 -     - 0 or 360
       |
       90

Title: Re: Circular movement
Post by: Nexus on January 13, 2014, 07:20:27 pm
Using Thor.Vectors (http://www.bromeon.ch/libraries/thor/v2.0/doc/group___vectors.html):
thor::PolarVector2f v;
v.r = radius;
v.phi = 0.f;

// In the loop: change velocity's angle and offset position
v.phi += elapsedTime * rotationSpeed;
stone.move(elapsedTime * v);
Title: Re: Circular movement
Post by: kursukia on January 13, 2014, 08:45:10 pm
Code: [Select]
X = sin(angle) * speed
Y = cos(angle) * speed

Don't forget that if you use std::sin and std::cos it takes radians, not degrees.  ;)


Hello,

allright, thank you. But.. can you exactly tell me what "angle" is in this code?

Edit:

Its a little bit strange.. is this wrong?

        float stonex = stone.getPosition().x;
        float stoney = stone.getPosition().y;


        float angle = stonex/stoney;

        x = std::sin(angle) * Speed;
        y = std::cos(angle) * Speed;

        stone.setPosition(x,y);
        std::cout << x << " " << y << std::endl;
Title: Re: Circular movement
Post by: zsbzsb on January 13, 2014, 09:10:56 pm
stonex and stoney are not the angle. An angle is the direction in which you are pointing. In degrees angles range from 0 to 360. Think of a compass, there is north, east, south, and west that are all 90 degrees from each other. That is why I included a small representation of angles in my first post.
Title: Re: Circular movement
Post by: kursukia on January 13, 2014, 11:41:57 pm
stonex and stoney are not the angle. An angle is the direction in which you are pointing. In degrees angles range from 0 to 360. Think of a compass, there is north, east, south, and west that are all 90 degrees from each other. That is why I included a small representation of angles in my first post.

puh.. I really understand you. But I have no idea, how I can do that. ._.
Title: Re: Circular movement
Post by: Nexus on January 13, 2014, 11:46:18 pm
Maybe you should read up on trigonometry, this is a very important concept in game programming.

Alternatively, you directly use the Thor example I gave you, so you can work on a higher level (vectors instead of trigonometric functions). The Thor.Vectors module can even be used without building the library, as it's header-only.
Title: Re: Circular movement
Post by: the_mean_marine on January 14, 2014, 08:05:41 am
This page http://www.mathopenref.com/coordparamcircle.html should explain what you need to know in sufficient detail.

I'd highly recommend following the suggestions of the other posters and study trigonometry. The importance of understanding maths in game development can't be overstated.
Title: Re: Circular movement
Post by: kursukia on January 26, 2014, 02:12:15 pm
Thank you. Sorry for my late replay. I studied hard and I got it now. :)

Thats my code:

       
sx = sr*std::cos(st)*(Speed*ElapsedTime);
sy = sr*std::sin(st)*(Speed*ElapsedTime);

if(Keyboard::isKeyPressed(Keyboard::Space))
{
st++; // mini example
stone.setPosition(  stone.getPosition().x + sx,
                               stone.getPosition().y + sy);
}

But it moves too fast. How can I solve this? I cant use "sr*std::cos(st)*(Speed*ElapsedTime)" because its increase/decrease the value. :(

kursukia
Title: Re: Circular movement
Post by: kursukia on February 03, 2014, 07:36:30 pm
Hey,

can nobody help me? I have just a problem with time control. If I press "Space", its move really fast. And I dont have any Idea, how I can solve this. :(


sr = 100; // radius
st = 0; // angle

sx = sr*std::cos(st);
sy = sr*std::sin(st);

if(Keyboard::isKeyPressed(Keyboard::Space))
{
        st++;


        //stone.setPosition(  stone.getPosition().x + sx,
        //                                      stone.getPosition().y + sy);

        stone.move(  sx, sy);


}
Title: Re: Circular movement
Post by: Azaral on February 03, 2014, 08:10:35 pm
Are you time limiting your game somehow? If not, then its going to go as fast as you computer can run through the loops
Title: Re: Circular movement
Post by: kursukia on February 04, 2014, 04:21:20 pm
Are you time limiting your game somehow? If not, then its going to go as fast as you computer can run through the loops

Hey :)

Yes, its limited on 60 FPS.

And its still too fast. I want to make a slow movement with:
Speed = 300.f;
so how can I do this? ._.

Title: Re: Circular movement
Post by: wintertime on February 04, 2014, 10:05:44 pm
If its too fast you should reduce the speed you multiply with.
Also dont make logic updates dependent on tinkering with the framerate. And preferably use fixed timesteps.
Title: Re: Circular movement
Post by: kursukia on February 04, 2014, 11:11:04 pm
If its too fast you should reduce the speed you multiply with.

Sorry, maybe Im very confused now. I know that I have to reduce the speed, but the problem is: Nothing is multiplied with "Speed" (have a look at the code).
The main question is: How can I integrate the "Speed" into the code? You understand? Sorry for my english. :(

Also dont make logic updates dependent on tinkering with the framerate. And preferably use fixed timesteps.

I know. Im still learning.
Title: Re: Circular movement
Post by: Azaral on February 05, 2014, 06:00:46 am
Your st variable is essentially your speed. You are increasing it by one each frame. Also, cos and sin take the angle in radians, not degrees. So you are giving it 1 radian, then 2 radians, then 3, 4, 5, 6, and at 7 you would be around the circle since 360 degrees = 1 Tau (or 2 Pi). So, every 6.28 frames, you are going around the circle.

First you need to correct that by converting your degree into radians, or work in radians. I always create a header file that contains mathematical constants and conversions I will need. One is DegreesToRadians and the other is RadiansToDegrees. You can easily find these values.

So, if you want to control your speed, you should be acting on st or replace st with your speed.
Title: Re: Circular movement
Post by: Hapax on February 07, 2014, 01:15:36 am
st would be the speed of rotation, not the speed of movement.
If it's turning too quickly, divide st by n or - as Azaral suggested - convert to radians.
If it's moving (changing position) too quickly, you'll need to divide sx and sy by n.

n, in this case, is dependent on your computer's speed. It would be trial and error. If you wanted to be able to multiply by a speed variable, you'd need to divide by a higher number.