I am trying to make a game involving the calcualtion of forces needed to perform some simple mathematical operations. I have a dx
variable that represents both the speed and direction along the X-axis. Now I want to subtract friction but it would have to be an addition if the direction of dx
is negative. Is there any way to subtract friction from dx
using absolute values while returning a signed value? I was looking in to bitwise operators but I have no idea what the possibilities are...
What I could do (i think):
sign = ( dx >> 31 ) << 31;
dx = (dx << 1) >> 1;
dx = ( dx - friction ) & sign;
Not sure if this would work, just the idea... Any improvements?
Edit: this is what I did:
dxSign = Integer.signum(dx) * -1;
dySign = Integer.signum(dy) * -1;
dy = Math.abs(dy);
dx = Math.abs(dx);
fric = body.friction * mass;
dx = ( dx > fric ) ? dx - fric : 0;
dy = ( dy > fric ) ? dy - fric : 0;
dx = dx * absorption;
dy = dy * absorption;
dx *= dxSign;
dy *= dySign;