Moving a rigid object to mouse position

Post Reply
jhasketan
Posts: 3
Joined: Sat Sep 14, 2019 7:24 am

Moving a rigid object to mouse position

Post by jhasketan » Sat Sep 14, 2019 7:32 am

Hi there,
I am a bullet physics beginner as well as new to graphics programming. I am trying to write a simple application using bullet physics.
I have created a rigid body. on click of my mouse, this rigid body shall move to the mouse position.
I can get the mouse position in the screen co-ordinate and can normalize it to range of (-1,1).
But when I query object position of the rigid body it gives me some number which is not in the range of (-1,1).

Can somebody help ? Any sample code in C++ to achieve the above is much appreciated.

Thanks,
Jhasketan

User avatar
drleviathan
Posts: 550
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Moving a rigid object to mouse position

Post by drleviathan » Thu Sep 19, 2019 4:23 pm

This sounds a math question about how to transform from one frame to another. Given some 2D point on the screen (frameA) you want to compute the corresponding 3D point in the world-frame of the physics simulation (frameB). There exists a transform that does what you want such that:

pointInB = transformFromAtoB * pointInA

Where transformFromAtoB is a 3x3 matrix and pointA and pointB are 3D vectors. For your 2D space you would have to add a dimension but know that all points you care about there have the third component = zero.

Note, in this scenario the transform always operates from the left on vectors on the right.

But how to compute transformFromAtoB?

If you know three points in frameA (a0 through a2) and their corresponding points in frameB (b0 through b2) then you can compute it as follows:

[b0, b1, b2] is a 3x3 matirx. Stack your three points in frameB as columns of the matrix.

Similarly, [a0, a1, a2] is another 3x3 matrix. Stack your three points in frameA as columns of the matrix.

Given that relationship above for how transformFromAtoB relates points in A to points in B we can write:

[b0, b1, b2] = transformFromAtoB * [a0, a1, a2]

Multiply both sides from the right with the inverse of [a0, a1, a2] (I will call it [a0, a1, a2]^-1) and you get:

[b0, b1, b2] * [a0, a1, a2]^-1 = transformAtoB

And there you go. You just need to formulate those matrices, invert the one for frameA and multiply them together in that order.

User avatar
drleviathan
Posts: 550
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Moving a rigid object to mouse position

Post by drleviathan » Fri Sep 20, 2019 3:26 pm

I realized after writing it up: the algorithm above is limited to rotation and scale transformations. It doesn't handle translations. For that you have to use 4D vectors where each vector looks like: v = <x, y, z, 1> and you would need four pairs of corresponding points to build the two 4x4 matrices in the equation.

Post Reply