Sign in to follow this  
Cacks

Calculating distance moved under a mouse

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Cacks
I 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this