Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Circular movement  (Read 7765 times)

0 Members and 1 Guest are viewing this topic.

kursukia

  • Newbie
  • *
  • Posts: 20
    • View Profile
Circular movement
« 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

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Circular movement
« Reply #1 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

« Last Edit: January 13, 2014, 09:12:52 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Circular movement
« Reply #2 on: January 13, 2014, 07:20:27 pm »
Using Thor.Vectors:
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);
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

kursukia

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Circular movement
« Reply #3 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;
« Last Edit: January 13, 2014, 09:06:49 pm by kursukia »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Circular movement
« Reply #4 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.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

kursukia

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Circular movement
« Reply #5 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. ._.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Circular movement
« Reply #6 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

the_mean_marine

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Circular movement
« Reply #7 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.

kursukia

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Circular movement
« Reply #8 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

kursukia

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Circular movement
« Reply #9 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);


}
« Last Edit: February 03, 2014, 07:39:30 pm by kursukia »

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Circular movement
« Reply #10 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

kursukia

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Circular movement
« Reply #11 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? ._.


wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Circular movement
« Reply #12 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.

kursukia

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Circular movement
« Reply #13 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.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Circular movement
« Reply #14 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.

 

anything