SFML community forums
Help => Graphics => Topic started by: gpnk90 on March 08, 2016, 04:47:48 pm
-
Hi,
I'm looking to move an object in a similar way to that of a ship. The user is to enter the direction the ship should sail (i.e 300 degrees) and the ship should then move in the direction of WNW on a heading of 300. Is there anywhere that explains the rotation system used by sfml? As currently a heading of 0 moves my object South.
I hope that makes sense, any help or pointers would be appreciated!
-
Rotation in SFML is the rotation (in degrees) of the actual visual representation. SFML also has an actual position.
For movement of the usual transformables, you can either set a new position or use the move() method to specify just the offset.
If you wish to move an object a at a specific angle, you will need to calculate the horizontal and vertical offsets yourself.
There are lots of resources about doing so (google it!) and there are libraries that do it for you too. Thor (http://en.sfml-dev.org/forums/index.php?topic=7329), for example, has Polar Vector (http://www.bromeon.ch/libraries/thor/documentation/v2.1/structthor_1_1_polar_vector2.html)s, which could come in handy for you here.
-
there is another way using sfml sf::transform, if you multiply movement on rotation(in this order) the object will move in direction of its rotation.
Have a look at matrix multiplication.
-
The objects I'm moving have their own position and angle, the movement is dealt with and then the resulting position is then used to update the sfml position. Does this change any of the advice?
-
The objects I'm moving have their own position and angle, the movement is dealt with and then the resulting position is then used to update the sfml position. Does this change any of the advice?
This sounds like you've done what you are asking about.
What is it you are having trouble with?
there is another way using sfml sf::transform
...
Have a look at matrix multiplication.
This is much more complicated that calculating x and y for an angle.
-
Sorry, I should have explained better in my first post!
I have a ship, with a position (2D vector), angle (int), velocity (2D vector).
The objects position is updated:
vel.x = sin((PI/180)*angle) * speed * time;
vel.y = cos((PI/180)*angle)* speed * time;
pos = pos + vel;
pos.x and pos.y is then used to set the objects sfml pos. (draw.setPosition(pos.x, pos.y)
-
This sounds like you've done what you are asking about.
What is it you are having trouble with?
You know how to move the object by any amount at any angle so I don't understand the problem.
Is it that you don't know what the current rotation is or want to offset that rotation?
sprite.setRotation(x) (http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Transformable.php#a32baf2bf1a74699b03bf8c95030a38ed) - sets the current rotation to x
sprite.getRotation() (http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Transformable.php#ad783a7e9971398ec613d22455252809e) - gets the current rotation
sprite.rotate(x) (http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Transformable.php#af8a5ffddc0d93f238fee3bf8efe1ebda) - rotates by x - effectively the same as sprite.setRotation(sprite.getRotation() + x))