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

Author Topic: [SOLVED]Smart Moving Problem  (Read 12174 times)

0 Members and 1 Guest are viewing this topic.

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
[SOLVED]Smart Moving Problem
« on: May 05, 2017, 03:44:40 pm »
Hey! I have a little problem with the "smart moving"(smart moving means : if the player is like and it presses up the spaceship will go in the direction where it is oriented(I mean it won't go simply up).So the question is : How to do smart moving in a game ?
« Last Edit: June 06, 2017, 08:05:15 pm by Boanc »
Guess Who's Back ?

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Smart Moving Problem
« Reply #1 on: May 05, 2017, 04:42:50 pm »
Make a variable that keeps track of the angle of the spaceship. Use trigonometric functions (such as sin and cos) to calculate how much the spaceship should be moved in the x and y directions based on that angle when the 'up' key is pressed.

sjaustirni

  • Jr. Member
  • **
  • Posts: 94
    • View Profile
Re: Smart Moving Problem
« Reply #2 on: May 05, 2017, 04:44:55 pm »
Store velocity and acceleration of spaceship and iterate on that. Here's a great tutorial on this subject: http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: Smart Moving Problem
« Reply #3 on: May 05, 2017, 05:40:36 pm »
Ok I've readed it, can you give me an example how to wrote this ?
Guess Who's Back ?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Smart Moving Problem
« Reply #4 on: May 05, 2017, 06:08:38 pm »
You want to use atan2. It's a very, very common problem and a simple Google search will result in many tutorials and examples (e.g. like this: http://www.gamefromscratch.com/post/2012/11/18/GameDev-math-recipes-Rotating-to-face-a-point.aspx).
Don't expect others to write you a working example that you copy&paste, but try to understand the problem and the solution.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sjaustirni

  • Jr. Member
  • **
  • Posts: 94
    • View Profile
Re: Smart Moving Problem
« Reply #5 on: May 05, 2017, 09:15:33 pm »
Firstly, notice there are 3 parts of the tutorial, make sure you read all of them.
Secondly, as you may have noticed, this assumes you're familiar with vector math. Now, I don't know if you have learnt about vectors in school, but if you haven't, I suggest you to look up the vector math on the Internet and only once you have a fair understanding of it to come back to programming. There really are no shortcuts here, trust me, I've been there.

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: Smart Moving Problem
« Reply #6 on: May 06, 2017, 09:08:32 am »
Ok, I've readed wolffire all 3 parts, but I don't have any how to put it in my code.I tried and atan2 but it returns 0(sprite.rotate(angle) doesn't move it, I did a cout << angle and it said 0)
Guess Who's Back ?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Smart Moving Problem
« Reply #7 on: May 06, 2017, 10:37:22 am »
Don't forget that the C++ math function operate with radian and SFML uses degree, so you'll have to convert.

Not sure what you're implying with the cout. If the rotation angle is 0, nothing will be rotated.

Did you actually understand the math or did you just copy&paste the code?
« Last Edit: May 06, 2017, 10:39:44 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sjaustirni

  • Jr. Member
  • **
  • Posts: 94
    • View Profile
Re: Smart Moving Problem
« Reply #8 on: May 06, 2017, 02:03:40 pm »
Ok, I've readed wolffire all 3 parts, but I don't have any how to put it in my code.I tried and atan2 but it returns 0(sprite.rotate(angle) doesn't move it, I did a cout << angle and it said 0)

I feel like I am repeating myself, but I'll tell you anyway. If you don't know the vector math, don't bother yourself with the programming.
Go through this instead: https://www.khanacademy.org/math/precalculus/vectors-precalc

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: Smart Moving Problem
« Reply #9 on: May 06, 2017, 08:52:10 pm »
Ok, I've learened all about vectors(and  understood it), but what are the functions to do it(bad exprimation, I know... I don't have any idea how to use vectors to make the rotation(bad exprimation again).

And...
Quote
If you don't know the vector math, don't bother yourself with the programming.
Sjaustirni you don't have to be so harsh with me because I am a beginner...
Guess Who's Back ?

sjaustirni

  • Jr. Member
  • **
  • Posts: 94
    • View Profile
Re: Smart Moving Problem
« Reply #10 on: May 06, 2017, 10:58:08 pm »
I am sorry if it sounds harsh, it wasn't meant to be harsh, just bold, because it can save you a lot of time.

As to the vector rotation, you actually may not need it. What are you trying to achieve?

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: Smart Moving Problem
« Reply #11 on: May 07, 2017, 07:30:01 am »
Quote
What are you trying to achieve?
If the Player presses Left the Spaceship will rotate to Left and if the Player presses Up the Spaceship will go in that direction,not simple acceleration(I mean to simply accelerate in front, uhh it's so hard to explain that...).
Guess Who's Back ?

sjaustirni

  • Jr. Member
  • **
  • Posts: 94
    • View Profile
Re: Smart Moving Problem
« Reply #12 on: May 07, 2017, 08:57:17 am »
I am a bit confused.

In your model, what happens when you press Left key for an infinitely long time? Does the ship go round in a circle or does it go parallel with the y axis?
« Last Edit: May 07, 2017, 11:00:39 am by sjaustirni »

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: Smart Moving Problem
« Reply #13 on: May 07, 2017, 09:52:12 am »
So let say the ship is like  that up sign https://www.warningsignsdirect.co.uk/index.php?cPath=513_1068&osCsid=jivfi8rv5bkh44k565rvcrdj62
when the player will press left it will go to angle 359 (from 360, when the spaceship is up her angle is 360,no?)
,now if the player presses up the spaceship will go ahead from that angle(359) and it won't change it's angle till the player will press Left or Right. This is what I was trying to explain.So how to do that how to rotate the ship and how to accelerate in that position?
« Last Edit: May 07, 2017, 09:56:12 am by Boanc »
Guess Who's Back ?

GameBear

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
    • Email
Re: Smart Moving Problem
« Reply #14 on: May 07, 2017, 10:39:25 am »
So.. im still not 100% sure what part you want..
So, this is a compleate (if a bit hacky) way to rotate, accelerate, and move a space ship (m_Ship)
In my example I just use a ractangeShape for the ship, but you can use a sprite just fine.
you will need the values.

float m_xVel = 0, m_yVel = 0, m_accel = 0.01;
hidden somewhere where MoveShip can accec them.
the x and y Val is the current speed that the ship is moving at the x and y axis.
the m_accel is how fast it accelerates in a given direction.

in addition you will need a sf::Keyboard m_Key
This can be done differently, but for this short code that is all i made.

    //in the header//
float m_xVel = 0, m_yVel = 0, m_accel = 0.001;
sf::Keyboard m_Key;


    //In the class//
void Game::MoveShip() {
        //Rotate right and left
        if (m_Key.isKeyPressed(sf::Keyboard().Right)) {
                m_Ship.setRotation(m_Ship.getRotation() + 1);
        }
        if (m_Key.isKeyPressed(sf::Keyboard().Left)) {
                m_Ship.setRotation(m_Ship.getRotation() - 1);
        }
        //Accelerate the ship forward.
        if (m_Key.isKeyPressed(sf::Keyboard().Up)) {
                m_xVel += (cos(m_Ship.getRotation() / 180 * 3.1415)) * m_accel;
                m_yVel += (sin(m_Ship.getRotation() / 180 * 3.1415)) * m_accel;
        }

        //Make the actual movement of the ship.
        m_Ship.setPosition(m_Ship.getPosition().x + m_xVel, m_Ship.getPosition().y + m_yVel);
}
 

this code will rotate the ship and move it around using the Up, Right and Left button.
the first two If statements simply looks for pressing the Right and Left key. and apply rotation to the ship accordingly.
the third, as the first two looks for a key press, but this time the up key.
if the up key is pressed, it grasp the current rotation, and using cos and sin, it calculates how much to add to the ships x and y speed (velocity).

note that the ships rotation is divided by 180*PI, the reason for this is that SFML rotation uses 360 degree rotation, where the math con and sin is geard towards radiant (rotation = 0-1)

In this case, a rotation of 0 or 360 is horizontal fazing right.

This will give you movement like in the asteroids games where your ship will float around in whatever direction until it is accelerated in another direction.

i Hope that helps.

if you want the full code to look over, i can upload that somewhere.
« Last Edit: May 08, 2017, 04:59:23 pm by GameBear »
string message = "some random dude";
cout << "I'm just " << message;

 

anything