c# - Tracking mouse movement and magnitude -
i'm making simple game player controls tank. rotation of turret controlled mouse movements. code looks this:
if (game.mousestate.y < ymovementborder) possessedturretpitchvalue += dist; if (game.mousestate.y > ymovementborder) possessedturretpitchvalue -= dist; if (game.mousestate.x < xmovementborder) possessedturretyaw += rotationdist / 6; if (game.mousestate.x > xmovementborder) possessedturretyaw -= rotationdist / 6;
xmovementborder , ymovementborder values representing midpoint of game screen. problem movement of mouse cause turret turn until maximum pitch/yaw angle. how can make such able read mouse movement magnitude(ie: slight movement of mouse cause slight pitch/yaw movement of turret)?
in update method, should store references previous mouse state , current mouse state. use these 2 variables figure out how mouse moved since last update. take tweaking right, try this:
//define private variables private mousestate prevmousestate = null; private mousestate currmousestate = game.mousestate; public void update(gametime gt) { prevmousestate = currmousestate; currmousestate = game.mousestate; //calculate how mouse has moved since last update var dx = currmousestate.x - prevmousestate.x; var dy = currmousestate.y - prevmousestate.y; //do rotating depending on values of dx , dy }
Comments
Post a Comment