# Calculating distance moved under a mouse

This topic is 4446 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys, I have translation working in the correct direction using a mouse. But The distance moved by an object is hard coded. This means that although moving in the correct direction, objects do not move directly under the mouse. How would I calculate how much to translate an object relative to 1 pixel on the screen? Thanks for any advice given!

##### Share on other sites
This question needs clarification.

Is this a 2d or 3d situation?

how are you tracking the mouse?
most APIs give you mouse cursor coordinates relative to the window...
(assuming 2d)
if you have that... it should be a simple matter of setting your object to match these coordinates.
you might have to make some adjustment of course, based on the scale of your world vs the scale of the viewport.. since pixels and whatever 'world units' you're using arent going to match

(for 3d)
there is firstly the issue of mapping a 2d input (mouse) into 3d space; which doesnt work without some kind of constraints....
and you will want to be 'unprojecting' your camera matrix....
but I'll let someone else handle that,
we dont even know if you are doing it 2d or 3d yet anyhow.

##### Share on other sites
Its a 3d situation.

For each pixel moved over, when a button is held, I translate the selected objects by a hard coded value.

I need to translate objects further back in the view frustum by a larger amount than closer objects.

I'm using the 3 different mouse buttons to translate objects in the 3 different axes with respect to my camera.

Also, if an object is situated at the origin of camera space, how do I find the translation factor for moving 1 pixel? Similarly for the rear of the frustum? Then I could work out the values in between.

When you say unproject, what is this?

##### Share on other sites
I think it is gluUnproject

##### Share on other sites
Quote:
 Original post by CacksI need to translate objects further back in the view frustum by a larger amount than closer objects.

You know the distance between camera and object. Scale the translation with that. You will also need to calculate (for example) the width and height of your view frustum for z=1 to convert mouse motion from pixels to "world units".

frustumHeight = 2*tanf(VerticalFieldOfView);
frustumWidth = frustumHeight * aspectRatio;

Works fine in my camera class.
After that it should be a simple matter of:
deltaX = distanceToCamera * frustumWidth * deltaXPixel/resolutionX

Things are so much more fun if you implement a zoom that tries to keep whatever your mouse is over under the mouse. Maybe unnecessary, but zooming towards the mouse just feels better.

1. 1
2. 2
frob
15
3. 3
4. 4
Rutin
12
5. 5

• 12
• 12
• 58
• 14
• 15
• ### Forum Statistics

• Total Topics
632115
• Total Posts
3004196

×