Jump to content

Mouse Position problems


AggrorJorn
 Share

Recommended Posts

I need to check whether I am moving my mouse up or down, while the left mouse button is held down. The first thing I noticed is that the mouse position differs half a pixel from the original pixel (image 1). Thats not terribly shocking, but just something I noticed. I use the following code to measure the mouseY position:

centerY = MouseY()
fw:Update()     
       currentY = MouseY()

 

The actual problem is as follow: When I move my mouse I can see wether it is moving upwards or down because of the value. However when I press and hold my left mouse button, the position of the mouse is no longer updated. This way I can no longer see whether the mouse is updating moving up or down.

 

Is this natural for a mouse position to occur?

Link to comment
Share on other sites

I hate to be the bearer of bad news, but with 2.0, and without Framewerk (the old spelling), MouseX() and MouseY() both update even when any mouse button is held down.

 

Are you ever checking the mouse's position more than once? (If you are checking if LMB is down, look there first.) Example (in C):

 

if(MouseDown(1) == 1)
{
  //Do "stuff" - like firing weapons
  [u]CurrentY = MouseY();[/u]
}

centerY = MouseY();
fw:Update();
currentY = MouseY();

 

In this fake example, the underline will cause problems, because it will probably cause CurrentY and centerY to have the same value. if LMB is not held down, then the underline is skipped, and so the two values should be different. Check to see if you're doing anything similar in your code.

LE Version: 2.50 (Eventually)

Link to comment
Share on other sites

Thanks for the reply Mumbles.

The main loop doesn't have any code that checks if the mouse buttons are down. It happens with every button of the mouse. So if I understand you correctly you are saying that the update MousePosition doesn't work with framewerk, but that it does when not using framewerk?

Link to comment
Share on other sites

Oh, I just noticed that you can't put an underline inside a code block - but you can see where I'm pointing to.

 

What I'm saying is, I don't use framewerk, and both MouseX() and MouseY() both update normally, even when any mouse button is pressed. But I don't think that framewerk will be causing the problem either...

LE Version: 2.50 (Eventually)

Link to comment
Share on other sites

I'm using Lua at the moment. Try this code in the editor. Holding down a mouse button (1,2 or 3 doesn't matter) results in this.

require("Scripts/constants/collision_const")
require("Scripts/constants/engine_const")
require("Scripts/LinkedList")
require("Scripts/filesystem")
require("Scripts/math/math")

--Variables
dx=0.0
dy=0.0
camerapitch=0.0
camerayaw=0.0

MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2)
FlushKeys()
FlushMouse()

local camera = fw.main.camera
currentY = MouseY()

--main function
while KeyHit(KEY_ESCAPE)==0 do

--Camera look
gx=Round(GraphicsWidth()/2)
gy=Round(GraphicsHeight()/2)
dx=Curve((MouseX()-gx)/8.0,dx,3.0/AppSpeed())
dy=Curve((MouseY()-gy)/8.0,dy,3.0/AppSpeed())
MoveMouse(gx,gy)
camerapitch=camerapitch+dy
camerayaw=camerayaw-dx
camerapitch=math.min(camerapitch,75)
camerapitch=math.max(camerapitch,-75)
fw.main.camera:SetRotationf(camerapitch,camerayaw,0,1)


--Check for mouse up or down
centerY= MouseY()
mouseDifference = centerY - currentY

fw:Update()

       --get the current Mouse Y position      
       currentY = MouseY()

fw:Render()
SetBlend(1)
DrawText("currentY",0,20)
DrawText(currentY,0,40)
DrawText("centerY",0,60)
DrawText(centerY,0,80)
DrawText("mouseDifference",0,100)
DrawText(mouseDifference,0,120)
       SetBlend(0) 

Flip(0)

end

Link to comment
Share on other sites

Well there is a lot of confusing things here.. You are calling MouseY() before the update 2 times and after it 1 time.

It's better to call it less time possible.. (As for example use a variable wich store theyr position during the loop), anyway you also put the mouseon the center of the screen too..

Couldn't you simple use dy to check it? Also i don't see where you are checking the mousebutton is held down..

Intel Corei7-6700, NVIDIA GeForce GTX 980, 32GB DDR4, W-10.

Link to comment
Share on other sites

Basically, I'm saying the same as what Joh is syaing. Try re-ordering the code slightly. Can't promise it will work though...

 

--Variables
dx=0.0
dy=0.0
camerapitch=0.0
camerayaw=0.0

--put these outside the main loop. You are calculating the same thing over and over again. The value will never change
gx=Round(GraphicsWidth()/2)
gy=Round(GraphicsHeight()/2)

--code missed out

--main function
while KeyHit(KEY_ESCAPE)==0 do

--Camera look
MousePosX=MouseX()
MousePosY=MouseY()
--For the rest of the loop, refer to these variable rather than re-checking the position

dx=Curve((MousePosX-gx)/8.0,dx,3.0/AppSpeed())
dy=Curve((MousePosY-gy)/8.0,dy,3.0/AppSpeed())
       --You now have the amount the mouse has moved by

MoveMouse(gx,gy)
camerapitch=camerapitch+dy
camerayaw=camerayaw-dx
camerapitch=math.min(camerapitch,75)
camerapitch=math.max(camerapitch,-75)
fw.main.camera:SetRotationf(camerapitch,camerayaw,0,1)

--Check for mouse up or down
mouseDifference=MousePosY-OldMousePosY
OldMousePosY=MousePosY
--But really, what is the difference between mouseDifference, and dy?
--If there is no difference, then just say mouseDifference=y and remove the OldMousePosY line

fw:Update()

fw:Render()
SetBlend(1)
DrawText("currentY",0,20)
DrawText(currentY,0,40)
DrawText("centerY",0,60)
DrawText(centerY,0,80)
DrawText("mouseDifference",0,100)
DrawText(mouseDifference,0,120)
       SetBlend(0) 

Flip(0)

end

 

I'm using Lua at the moment. Try this code in the editor.

 

That's not something I can help with...

LE Version: 2.50 (Eventually)

Link to comment
Share on other sites

Well i don't know how to explain.. This isn't a leadwerks bug as the camera is using MouseY to rotate, if this wasn't work correctly you won't be able to rotate the camera too.

So my question did you try the Mumbles code? Same things happend?

Intel Corei7-6700, NVIDIA GeForce GTX 980, 32GB DDR4, W-10.

Link to comment
Share on other sites

I know that in my own project (using C) it's fine. Maybe the problem is in the editor. But with being on 2.0, I don't have the editor, or lua. I also think that if it was a problem with the editor, Josh would have commented by now. He's normally very quick to respond when there is a problem with his code.

 

I don't like seeing questions going unanswered, or problems going unsolved, and that's why I came into this thread, but I don't think there's any more I can do without having the editor or lua. I can only provide theories, so I just hope someone else knows what the issue is, and can help you solve it.

LE Version: 2.50 (Eventually)

Link to comment
Share on other sites

I see that your code works Mumbles! I only had to add this line before the loop: local OldMousePosY = 0

 

The only thing that bothers me is that I don't understand why my code wasn't working. But I'll figure that out somewhere the evening. Thanks a lot for your help Joh and Mumbles.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...