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

Author Topic: Not your standard move mechanics  (Read 2324 times)

0 Members and 1 Guest are viewing this topic.

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Not your standard move mechanics
« 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

G.

  • Hero Member
  • *****
  • Posts: 1590
    • View Profile
Re: Not your standard move mechanics
« Reply #1 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
« Last Edit: April 15, 2014, 06:28:15 pm by G. »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Not your standard move mechanics
« Reply #2 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 (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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Not your standard move mechanics
« Reply #3 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


didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Not your standard move mechanics
« Reply #4 on: April 17, 2014, 12:51:33 am »
If you do want to understand it you can use the 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

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Not your standard move mechanics
« Reply #5 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);
            }
 

 :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Not your standard move mechanics
« Reply #6 on: April 18, 2014, 10:36:08 pm »
You can use float literals: 0.1f
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: