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

Author Topic: Radial Controls  (Read 1715 times)

0 Members and 1 Guest are viewing this topic.

nullGrind

  • Newbie
  • *
  • Posts: 23
    • View Profile
Radial Controls
« on: January 24, 2010, 12:20:55 pm »
Hello Guys

Right now im coding on a rocket game. The player is supposed to rotate the rocket and press space to accelerate in the chosen direction. Right now the rocket seems to randomly go into random directions. Does anybody know how i can control the rocket?

Here ist the code, interesting lines start at line 42.

The other problem is that the rocket does not fly in smooth curves but when i accelerate and at the same time rotate it starts to stutter and wont move.

Code: [Select]

#include <stdlib.h>
#include <iostream.h>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>


int main(int argc, char** argv) {
    sf::RenderWindow Game(sf::VideoMode(800, 600, 0), "Gravitation");
    sf::WindowSettings::WindowSettings(24, 8, 4);
    sf::Blend::Alpha;


    sf::Image IRocketP1;
    if (!IRocketP1.LoadFromFile("wrocket.png")) {
        // Error...
    }
    sf::Sprite rocketP1;
    rocketP1.SetImage(IRocketP1);
    rocketP1.SetColor(sf::Color(255, 255, 255, 255));
    rocketP1.SetX(0.f);
    rocketP1.SetY(0.f);
    rocketP1.SetPosition(350.f, 500.f);
    rocketP1.SetRotation(0.f);
    rocketP1.SetCenter(rocketP1.GetSize().x / 2 + 20, rocketP1.GetSize().y / 2 + 20);

    double speedR = 100;
    double widthR = rocketP1.GetSize().x;
    double heightR = rocketP1.GetSize().y;
    double speedRocket = 3;
    double direction = 0;
    double speedRotate = 2;
    int resX = 1280;
    int resY = 800;

    while (Game.IsOpened()) {
        Game.UseVerticalSync(true);

        // CONTROL
        float ElapsedTime = Game.GetFrameTime();
        if (Game.GetInput().IsKeyDown(sf::Key::Left) && rocketP1.GetPosition().x > 0)
            direction += speedRotate;
        if (Game.GetInput().IsKeyDown(sf::Key::Right) && rocketP1.GetPosition().x + widthR < resX)
            direction -= speedRotate;
        rocketP1.SetRotation(direction);
        if (Game.GetInput().IsKeyDown(sf::Key::Space)) {
            rocketP1.Move(speedRocket * sin(direction), speedRocket * cos(direction));
        }
        cout << direction;
       


        sf::Event Close;
        while (Game.GetEvent(Close)) {

            if (Close.Type == sf::Event::Closed)
                Game.Close();
            if ((Close.Type == sf::Event::KeyPressed) && (Close.Key.Code == sf::Key::Escape))
                Game.Close();
        }

        Game.Draw(rocketP1);
        Game.Display();
        Game.Clear();

    }
    return (EXIT_SUCCESS);
}

[/quote][/code]

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
Re: Radial Controls
« Reply #1 on: January 24, 2010, 02:25:22 pm »
Quote from: "nullGrind"
Code: [Select]

            rocketP1.Move(speedRocket * sin(direction), speedRocket * cos(direction));

It's reversed, sin is for opposite side (Y axis) and cos for adjacent (X axis). Also remember that Y grows from top to down, so you probably want to turn the Y component negative.


Quote from: "nullGrind"
the rocket does not fly in smooth curves

That's because of the move formula. It will work when you fix it. When you get it to work, add some real acceleration:
 - while pressing space increase speedRocket by some value.
 - while not pressing space, decrease it.
Be sure to test against the the speed limits before changing velocity.
Pluma - Plug-in Management Framework

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Radial Controls
« Reply #2 on: January 24, 2010, 06:12:14 pm »
Also, you use a HUGE value for direction. You are assuming that sin() and cos() take direction in degrees. They don't. They takes direction in radians, which have values of 2pi radians = 360 degrees. You are rotation in nearly an entire circle when you rotate.
I use the latest build of SFML2

 

anything