I am trying to move a player in direction angle by speed. I need to use trigonometric functions in order to calculate this. Can anyone help me discover how to do this?

if (playerB.up == true) {
            (playerB.speed)+=((Delta*ballspeed)/1000);
            }
        if (playerB.down == true) {
            (playerB.speed)-=((Delta*ballspeed)/1000);
            }
        if (playerB.left == true) {
            (playerB.angle)-=((Delta*ballspeed)/1000);
        }
        if (playerB.right == true) {
            (playerB.angle)+=((Delta*ballspeed)/1000);
        }
		//move player in direction angle by speed

Dani AI

Generated

A practical way to do this is to turn the angle into a unit direction (cos, sin), scale that by speed and the frame Delta, then add to the position. Two common gotchas to check first: C++ trigonometric functions use radians (convert if your angle is in degrees), and many engines use Delta in milliseconds (divide by 1000.0 to get seconds).

Here is a minimal C++ example (angle in degrees, Delta in milliseconds):

// assume: playerB.x, playerB.y, playerB.speed, playerB.angle (degrees)
// Delta is milliseconds since last frame
const double PI = 3.14159265358979323846;
double dt = Delta / 1000.0; // seconds
double angleRad = playerB.angle * PI / 180.0;

playerB.x += std::cos(angleRad) * playerB.speed * dt;
playerB.y += std::sin(angleRad) * playerB.speed * dt; // invert sign if your Y axis points down

Notes and practical tips:

  • If the screen Y axis points downward (typical for 2D), subtract the sin term or invert your conventions.
  • Keep angles normalized to avoid overflow: playerB.angle = fmod(playerB.angle, 360.0); if (playerB.angle < 0) playerB.angle += 360.0;
  • Use double for smooth movement and avoid integer truncation.
  • Clamp playerB.speed (min/max) and apply simple friction or acceleration for nicer control when pressing up/down.
  • is correct that you can use a velocity vector and rotate it with a matrix if you need complex rotational motion; for simple forward/back movement trig is simpler and cheaper.
  • and : check the units you use for angle increments and speed changes — make them per second, then multiply by dt so behavior is frame-rate independent.

This covers the typical pitfalls and gives a working update step to move the player along the current angle.

Recommended Answers

All 2 Replies

>> I am trying to move a player in direction angle by speed

I am kind of hesitant of what you mean exactly by that, can you explain?

Why don't you do it with vectors? it's alot less work.
Just define a vector with a specified length (your speed) and use a rotation matrix to rotate it the amout of degrees.
Then add each component of the vector to your position.

Just google on the terms vector and rotation matrix and read wikipedia. that should be enough.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.