OK, so I have a golf-ish game where there is a ball that moves around the screen with separate x and y velocities which vary based on an angle chosen before the ball is hit. This all works perfectly fine, the ball bounces off the walls, etc. But when I try to apply friction to the ball's movement, I encounter some issues.
The process I am using to scale down my velocity vector while maintaining the same angle of motion is as follows:
1. Find the magnitude of the vector
2. Normalize the vector
3. Decrease the value of the magnitude by a friction constant
4. Multiply x and y velocity components by the new magnitude
My code looks like this:
P*16**cos(θ)->V //P represents power, used to determine how fast the ball will move
P*16**sin(θ)->W //V is the X-velocity; W is the Y-velocity
Pt-On(X+V->X/256, Y+W->Y/256, Pic0) //X and Y are the position components
127*W//sin(tan-1(V,W))->M //M is the magnitude of the velocity vector
M-F*W//M->W //F is the friction constant. I multiply the new magnitude by each component before dividing by the current magnitude
M-F*V//M->V //because W//M would give a value of 0
Depending on P and θ, the code may or may not work, and I can't figure out why; the ball does seemingly random stuff, often not even going in the right direction.
Also, I don't know if this is relevant, but when I debugged the magnitude, it changed whenever the ball bounced off the left or right of the screen, which I don't think should be happening.
Can anybody help me fix this?