Moving an object with mouse

I am using Python Show Mouse (1) to show default mouse cursor. I can click objects to do stuff, but how I can drag an object? like holding down left click and moving it to a new spot?

Oh! I’ve done this before!

What I did was put a hidden object in front of the objects I wanted to grab and set their position to the hitPosition of a mouse over sensor with some offset. The only problem is that suspend dynamics doesn’t work the way you need it to for this :\

Since then I discovered getScreenPosition, getScreenVect, and getScreenRay from the KX_Camera object which probably work better than that old hack. Also, I believe setting an impulse towards the desired point could solve the dynamics problem.

Game Engine class docs

hitPosition and all, would that be logic bricks or python?

It’s python.

pos = mouseSensor.hitPosition
obj.position = pos

That’s the first example, and it only works with static objects. With dynamic objects, this weird thing happens where the acceleration of the object accumulates so that eventually the object will fly out of control.

Everything would be a static, so I made an object, I’d use that code above as say “MoveObject” and connect the mouse click and MoveObject both to the object?

Here’s an example that’s pretty easy to follow.

If you ever move your mouse off of the object it will stop following though. Fixing that I’ll leave as an exercise for you. (It’s really easy.)

Also, just ignore the above post: that isn’t a very versatile method anyway.


mousefollow.blend (447 KB)

It crashed my blender trying to open. What version is it? I have 2.49B and 2.57B

Just tried it and it doesn’t do anything do I set it to an empty or an object?

So you moved your cursor over the cube and nothing happened?

Thanks teldredge, but your script does a little bit more than fayt asked for.

Yeah I tried

The names aren’t that atrocious, all the p’s c’s o’s and n’s are just points in space so they don’t really need a big name.

Does this line also refer to my script?

I would also use more game object specific functions to make it easier to understand the logic

If theres anything you don’t understand just ask me.

Your code should read like a story. If you make a script with the intention of sharing it, you should make it so that people reading it understand your intent at every point in the story. Those points aren’t just points, they have meaning specific to the script so their names should reflect that meaning.

It isn’t that I don’t understand the logic-- it’s that I don’t like how it reads.

Those points aren’t just points, they have meaning specific to the script so their names should reflect that meaning.

Trust me, those points are just points. Theres nothing magical about them, and seeing as how those points are points I am going to name them relevant to their purpose - that is, being points - and last time I checked: it was perfectly fine to call a point ‘p’, it was perfectly fine to call a normal ‘n’ and it was perfectly fine to say ‘let c be the camera’s position’. I’m doing you as the reader a favour by not calling them ‘camera_position’, ‘owner_position’, ‘normal’ and ‘mouse_world_position’ because what is more readable?

return c + ( / * (p-c) + own['drag_point']


return camera_position + ( / * (mouse_world_position-camera_position) + own['drag_point']

Neither one helps you understand what is going on any better, but the first one sure is a lot easier to read. If you wan’t to know my intent at each point, theres a comment at each point to let you know.

I didn’t say they weren’t points, my point is that writing out names makes the program clearer. The only place where I’ve seen that not be the case is when you have a variable to increment in a loop. A single letter is the least descriptive name possible.

Also, the line you refer to could have been broken up. ( / * (p-c) is apparently a thing that could have a name so it could have been assigned to a variable beforehand. I will admit that my confusion over what you are doing has a lot to do with my not knowing the mathematics, but cleaner code would make it easier for people similarly confused to work out what you are doing.

I’m sure your math is sound (it does work after all), but some of your code could have been encapsulated into separate functions or stuck in another file so users wouldn’t have to worry about that up front–which is my real argument. You stated in teldredge’s thread that you don’t have to bother with the math to use the script, so why is it exposed to the main part of the script’s logic? Instead of leaving comments, the code in the main function could have been called from separate methods to enhance maintainability and readability.

You are right in some terms, I had already taken the maths into separate functions (get_point_behind_mouse and get_constrained_position), but having those functions appear first may be a bit misleading. Perhaps maybe putting them after the main function may have been more ideal.