Viktorelo Viktorelo - 2 months ago 14
C++ Question

How to move objects diagonally in SFML?

So I can move my object down and up by changing the

y
variable.
I can move my object left and right by changing the
x
variable.
But how do I move diagonally at the same velocity.
If i just do

x += velocity;
y += velocity;


it will move about 1.5 times faster.

Is there some kind of equation with
cos
and
sin
to make an object move in a direction it is pointed to?
I would want to be able to change it's direction with
SFML(Simple Fast Multimedia Library)
built in function
setRotation()
.

Thank you in advance.

Answer

In SFML rotating an object won't affect the direction where it is going. So you have to implement it yourself.

x+=vel*sin(angle)
y+=vel*cos(angle)

For diagonal moving angle=45 degrees or (45*PI)/180 radians depending which sin/cos library you are using(C/C++ library require radians as argument).

Comments