need to move object when mouse is down

I need a way for an object to follow the mouse along its x-axis when the following conditions are true

  1. the initial condition must be true when the mouse is over a specific object
  2. after condition 1 is true, it then does not matter if the mouse is over the object, but the left mouse button must be held down.
  3. When the left mouse button is depressed, both above conditions become false and repeats back to checking condition 1.

At first, I thought this would be simple, but because I need the initial condition to have the mouse over the object, it returns false after the object looses focus with the mouse, even with the left mouse button being held down. Is there any way to keep the mouse attached to the object while the left mouse button is pressed?

heres a blend. start the engine, then click on the plane and drag it.

mouse tracker.blend (470 KB)

WOW, thanks man. Just one more question though. where did you come up with multiplying msX by 16. I see that this controls the speed of the object tracking with the mouse, but why does 16 work? Was it trial and error or is it documented somewhere that missed over looked?

You could also try looking into Finite State Machines. This would help keep your mouse conditions organized in a way that they wouldn’t impede with each other.

I spoke too soon. I noticed that this script only works if the object, the plane in your example, is positioned in the origin. Once the object’s x-axis is located anywhere else other then 0, the object jumps. In addition, the further away you initially position the object from the origin, the more the object jumps at a further distance.

I remember having this problem with another scripting language. However, I use to be able to resolve it by running 2 mouse events. First, you would set the initial offset on a one time mouse down event. Then you would subtract the offset from the new mouse position inside a mouse move event. However, I do not think Blender has a mouse move event does it?

i didnt know what you wanted this for, so i optimized it for an orthographic camera with a scale of 16, simiar to a gui setup. you could add a lot more calculations for a perspective, but thats not really my area.

blender has a logic.mouse.events[events.MOUSEX/Y] that returns like a button. this way you have the cube move a constant rate if the mouse is moving. but i dont know if it says which way its going.

i didnt have the issue of jumping. it picked up right where i would leave it. perhaps your resolution needs to be set.

Daedalus_MDW,

I did not mean to put you down. True, it is my fault that I did not explain what this is for, but at the time, I thought my problem was something that could happen in any situation. On that note, I will explain. My game I am trying to develop is a Billiards pool game. At this time, I am trying to make the mechanics for the pool stick. The final result will give the user the ability to pull the pool stick back, which the further back the pool stick is pulled, the stronger the strike will be.

As for trying to fix my problem, I have found out that BGE does come with a mouse movement sensor. So to start off, I made 2 functions in my script that handles the activation of movement and the movement of the pool stick. This done by, first using your technique of checking if the mouse has been just activated. When this becomes true, I set the drag state to true. calculate the offset, and then the condition in the second function becomes true and moves the pool stick.

Now this is where anyone else can jump in. So far I have managed to get rid of the jump after the mouse moves. However, I need my pool stick to move in the direction its normal, which means it requires a bit of linear algebra. I was able to make some progress, but because logic.mouse.position moves in viewport space from 0 to 1, I can only make my pool stick move only in one direction. So with me including my project, I was hoping if someone can help me out.

Attachments

bar_scripting_almost_correct.blend (1.53 MB)

I think you felt for the “always” trap that most users here do. You do not need that. I men why would you run that code when you already know there is nothing to do?

I suggest to modify your code a little bit into three setups, like a drag and drop operation:

A) start drag
B) dragging
C) end dragging

A) one event - one frame duration: mouse over + mouse button= sensors no level trigger needed
B) always after A) - several frames duration -> True Level Triggering on mouse button
C) always after B) - one frame duration = mouse button not measured anymore