SFML community forums

Help => Graphics => Topic started by: Raincode on April 15, 2014, 06:09:00 pm

Title: Not your standard move mechanics
Post by: Raincode on April 15, 2014, 06:09:00 pm
Hello,

I need help (obviously). Usually, when implementing character movemment, as in game character, I did something like, <top> move upwards, <left> move left, ...

However, I want something different now, and I can't figure it out.

I createt an amature, terrible, picture to explain, I hope it is understandable (see attachment)

Does anyone have an idea how to implement such movement (based on arrow keys, wasd)?

Kind Regards
Raincode
Title: Re: Not your standard move mechanics
Post by: G. on April 15, 2014, 06:22:41 pm
Like a car?

Left and right are simple rotations.
To move forwards or backwards, you need the direction your sprite is facing. You can get this direction vector from the angle it is rotated with simple maths. (remember that SFML uses degrees, but cos and sin use radians)
dir.x = cos(angle);
dir.y = sin(angle); (might be -sin, I don't remember)
Once you have this direction vector, you can compute the velocity of your sprite doing direction * speed
(and backwards works the same, but with negative speed or opposite direction)

That would be a good start. :p
Title: Re: Not your standard move mechanics
Post by: Nexus on April 15, 2014, 06:42:47 pm
Are you familiar with trigonometry?

If you want to use productive code instead of fiddling around with sin/cos, you could have a look at Thor.Vectors (http://www.bromeon.ch/libraries/thor/v2.0/doc/_vector_algebra2_d_8hpp.html) (they can be used without building the library). This module allows you to rotate a vector by a specified angle, or to let a unit face another one with very few code.
Title: Re: Not your standard move mechanics
Post by: Raincode on April 16, 2014, 09:38:15 am
Yes I am very aware of that, I just find myself having a hard time using the math  techniques I learn in school in my programs :D

I will try using the  thor/aurora library for this (I don't like writing code someone already has done better)  and post a little example if it works (can't promise this)
Thanks so far for everybody's help

Title: Re: Not your standard move mechanics
Post by: didii on April 17, 2014, 12:51:33 am
If you do want to understand it you can use the rotation matrix (http://en.wikipedia.org/wiki/Rotation_matrix). You just have to know matrix calculations :)
The derivation to get to the rotation matrix is pretty straightforward when looking at a rotation of a (1,0) and (0,1) vector
Title: Re: Not your standard move mechanics
Post by: Raincode on April 18, 2014, 09:41:11 pm
Hi again,

Thanks again for the help. Something like this works for me (exactely as imagined!)

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
            {
                thor::rotate(playerMovement, -float(0.1));
                player.rotate(-0.1);
            }
            else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
            {
                thor::rotate(playerMovement, float(0.1));
                player.rotate(0.1);
            }
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
            {
                player.move(playerMovement);
            }
            else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
            {
                player.move(-playerMovement);
            }
 

 :)
Title: Re: Not your standard move mechanics
Post by: Nexus on April 18, 2014, 10:36:08 pm
You can use float literals: 0.1f