Skip to main content
gamedev.net
🔒 Locked

[.net] DirectInput problem - using the mouse

Started by Fallen_Angel Jan 15, 2005 at 1:29 AM 5 replies 1.3k views
Original Post
Fallen_Angel
Fallen_Angel
I have been through the mouse tutorial in Tom Miller's Managed DirectX9 book, however I cannot seem to expand on that to do anything useful. What I am trying to do at the moment is rotate a gun turret on a tank when the mouse is moved left or right. What I want to happen is for the turret to turn while the mouse is being moved and then stop when it isn't. However what is currently happening is that the turret turns when I move the mouse but then continues to spin once I have stopped using it. This is my current mouse input code:

private Device deviceMouse = null;
private MouseState stateMouse;
private long lMousePosCurrent, lMousePosOld;

stateMouse = deviceMouse.CurrentMouseState;
lMousePosCurrent = stateMouse.X;

if (lMousePosOld == 0)
{
	lMousePosOld = lMousePosCurrent;
}

if (lMousePosCurrent != lMousePosOld)
{
	clsObjects.TurnTurret(0, (int)(lMousePosCurrent - lMousePosOld), true);
	lMousePosOld = lMousePosCurrent;
}

Any ideas where I'm going wrong? Thanks
Oluseyi
Oluseyi
Looks like the error is in your TurnTurret function, because the code snippet you posted is fine. Keep in mind that if your TurnTurret function is expecting an angle, then lMousePosCurrent - lMousePosOld could effectively be a very large one.
Fallen_Angel
Fallen_Angel
Possibly, however after looking at that function I can't see why it would be causing it. The TurnTurret function simply sets boolean values for the left/right rotations as follows:
public void TurnTurret(int nIndex, int nDir, bool bSwitch){	clsObject = (ClassObject)objectList[nIndex];	if (nDir == 1)		clsObject.TurretRight = bSwitch;	else		clsObject.TurretLeft = bSwitch;}

The TurretRight and TurretLeft are simply get/set methods. These values are set to false every time that I check for my input as follows:
clsObjects.TurnTurret(0, -1, false);clsObjects.TurnTurret(0, 1, false);

On my frame update function I then turn the turret based on these values with this:
if (clsObjects.TurretLeft){	clsObjects.DirectionTurret -= 4.0f;	if (clsObjects.DirectionTurret < 0.0f)		clsObjects.DirectionTurret += 360.0f;}if (clsObjects.TurretRight){	clsObjects.DirectionTurret += 4.0f;	if (clsObjects.DirectionTurret > 360.0f)		clsObjects.DirectionTurret -= 360.0f;}	
Fallen_Angel
Fallen_Angel
Slight update on this. I've narrowed the problem down to the fact that I'm setting my mouse input flag to false within my GetInput function, however this only gets called whenever any input is actually detected. So my question is where should I be setting these values to false? I tried within my OnPaint function however that resulted in it always being false and no input being processed at all.
DaWanderer
DaWanderer
Are you sure this isn't a tolerance issue? Your mouse positions may be off a few units during each read, so it's registering as movement. Perhaps it would be better to do this:

stateMouse = deviceMouse.CurrentMouseState;lMousePosCurrent = stateMouse.X;if (lMousePosOld == 0){	lMousePosOld = lMousePosCurrent;}//Using a tolerance value of 10 units. Adjust as necessaryint posDifference = Math.Abs(lMousePosCurrent - lMousePosOld);if (posDifference >= 10){	clsObjects.TurnTurret(0, (int)(lMousePosCurrent - lMousePosOld), true);	lMousePosOld = lMousePosCurrent;}


The other possibility I could see is if you're getting relative position rather than absolute position coordinates. In that case, you're always going to be moving the turrent since lMousePosCurrent will return to zero (give or take a few units) and will almost always be different than your lMousePosCurrent.
Fallen_Angel
Fallen_Angel
I'm using absolute coordinates, and I don't think the tolerance is an issue. I moved my code into a new project and output the values getting the following results:



However this is wrong as the difference should be 0 since there has been no movement and the moved also should be set to false. I'm still thinking that it has to do with me not setting the turn variables back to false at the right place, however I'm not quite sure where to be doing that now. In my GetInput function is pointless as I've realised since it doesn't get called when there is no input. And in my OnPaint function doesn't work either as it overrides the values and every frame and prevents all movement.
Fallen_Angel
Fallen_Angel
Have finally managed to get this working by setting my turn variables back to false on my frame update after quite a few trial-and-error attempts. Thanks guys for pushing me in the right direction.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.