Viktorelo - 3 months ago 22

C++ Question

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

`y`

I can move my object left and right by changing the

`x`

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`

`sin`

I would want to be able to change it's direction with

`SFML(Simple Fast Multimedia Library)`

`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).