Xojo Conferences
MBSSep2018MunichDE
XDCMay2019MiamiUSA

Getting the position of a object to match the x,y of the mouse. (Real Studio games Mailinglist archive)

Back to the thread list
Previous thread: SpriteSurface VS RB3D in oS X.3
Next thread: remove


Re: explosions   -   Jeff Quan
  Getting the position of a object to match the x,y of the mouse.   -   Chris Dillman
   Re: Getting the position of a object to match the x,y of the mouse.   -   Joseph J. Strout
   Re: Getting the position of a object to match the x,y of the mouse.   -   Lars Jensen
    Re: Getting the position of a object to match the x,y of the mouse.   -   Joseph J. Strout
     Re: Getting the position of a object to match the x,y of the mouse.   -   Lars Jensen
   Re: Getting the position of a object to match the x,y of the mouse.   -   Chris Dillman
   Re: Getting the position of a object to match the x,y of the mouse.   -   Joseph J. Strout
   Re: Getting the position of a object to match the x,y of the mouse.   -   Chris Dillman
   Re: Getting the position of a object to match the x,y of the mouse.   -   Chris Dillman
   Re: Getting the position of a object to match the x,y of the mouse.   -   Joseph J. Strout
   Re: Getting the position of a object to match the x,y of the mouse.   -   Chris Dillman

Getting the position of a object to match the x,y of the mouse.
Date: 20.01.04 20:49 (Tue, 20 Jan 2004 13:49:18 -0600)
From: Chris Dillman

Been up all night...so I hope this makes sense...

I need to get a object in 3D space to visually match the position of
mouse in 2D space.

Right now the objects do not match the mouse.

The amount they are off by scales the further from 0,0 the mouse goes.

I need to scale the movement of the 2D point by some factor in order
for the 3D point to keep up with where the mouse actually is.

I can not just move the object in 3D space.
Im actually moving the 2D points and recreating the objects on the
fly as needed.

Re: Getting the position of a object to match the x,y of the mouse.
Date: 20.01.04 21:05 (Tue, 20 Jan 2004 14:05:29 -0600)
From: Joseph J. Strout
At 1:49 PM -0600 1/20/04, Chris Dillman wrote:

>I need to get a object in 3D space to visually match the position of
>mouse in 2D space.

Well, of course, there are infinitely many positions in 3D space that
match the 2D mouse position. Which do you want? One in a plane
perpendicular to the camera's forward vector? Some other plane? On
the surface of a hemisphere?

Basically, you can just take the line between the camera position and
the point in the Hither plane indicated by the mouse, and intersect
this line with whatever shape you want to get your final 3D position.
But you do have to choose what that shape should be.

>I need to scale the movement of the 2D point by some factor in order
>for the 3D point to keep up with where the mouse actually is.
>
>I can not just move the object in 3D space.
>Im actually moving the 2D points and recreating the objects on the
>fly as needed.

Hmm, I didn't entirely follow you here, so if the above doesn't hit
the nail on the head, let me know and I'll try again.

HTH,
- Joe

Re: Getting the position of a object to match the x,y of the mouse.
Date: 20.01.04 22:31 (Tue, 20 Jan 2004 16:31:20 -0500)
From: Lars Jensen
> The amount they are off by scales the further from 0,0 the mouse goes.

Yup, that will happen...

> I need to scale the movement of the 2D point by some factor in order
> for the 3D point to keep up with where the mouse actually is.

Well, you need to somehow map 2D screen coordinates into 3D coordinates. The
right thing to do depends on what your objective is, and in general it's not
to apply a constant scaling factor.

If you're trying to drag objects along a plane that is parallel to the
screen, then you probably want to use FindPoint.

Note: if you're using Windows, there's a Quesa bug that can prevent
FindPoint from returning the right 3D coordinates. This might have been
fixed with d18; I'm not sure. Read more at:

http://ljensen.com/rb (in the 3D ReadMe section)

> I can not just move the object in 3D space. Im actually moving the 2D points
> and recreating the objects on the fly as needed.

I'm not sure what this means, or what it has to do with transforming
coordinates.

lj

- - -
Unsubscribe or switch delivery mode:
<http://support.realsoftware.com/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Getting the position of a object to match the x,y of the mouse.
Date: 20.01.04 22:48 (Tue, 20 Jan 2004 15:48:44 -0600)
From: Joseph J. Strout
At 4:31 PM -0500 1/20/04, Lars Jensen wrote:

>Note: if you're using Windows, there's a Quesa bug that can prevent
>FindPoint from returning the right 3D coordinates. This might have been
>fixed with d18; I'm not sure.

It is in fact fixed with Quesa 1.6d18, in our tests at least.

Cheers,
- Joe

Re: Getting the position of a object to match the x,y of the mouse.
Date: 20.01.04 23:56 (Tue, 20 Jan 2004 17:56:37 -0500)
From: Lars Jensen
> It is in fact fixed with Quesa 1.6d18, in our tests at least.

Great! I'll update the ReadMe.

lj

- - -
Unsubscribe or switch delivery mode:
<http://support.realsoftware.com/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Getting the position of a object to match the x,y of the mouse.
Date: 20.01.04 22:32 (Tue, 20 Jan 2004 15:32:39 -0600)
From: Chris Dillman
>At 1:49 PM -0600 1/20/04, Chris Dillman wrote:
>
>>I need to get a object in 3D space to visually match the position
>>of mouse in 2D space.
>
>Well, of course, there are infinitely many positions in 3D space
>that match the 2D mouse position. Which do you want? One in a
>plane perpendicular to the camera's forward vector?

Yes.

>
>Basically, you can just take the line between the camera position
>and the point in the Hither plane indicated by the mouse, and
>intersect this line with whatever shape you want to get your final
>3D position. But you do have to choose what that shape should be.

I think Im lost here.

>
>>I need to scale the movement of the 2D point by some factor in
>>order for the 3D point to keep up with where the mouse actually is.
>>
>>I can not just move the object in 3D space.
>>Im actually moving the 2D points and recreating the objects on the
>>fly as needed.
>
>Hmm, I didn't entirely follow you here, so if the above doesn't hit
>the nail on the head, let me know and I'll try again.

Hm maybe I should describe the process of what is going on here.
That might make it clearer about what I need to do.

The 3D space is set up so that there is a 1x1 pixel mapping ala Joes 3D FAQ.

Im taking a series of points in a polygon in 2D space.
For each point I make a 3D object that represents it.

I then place these points into 3D space.

Now I want to drag these 3D points around so that they visually show
up where the mouse is.

Re: Getting the position of a object to match the x,y of the mouse.
Date: 20.01.04 22:51 (Tue, 20 Jan 2004 15:51:48 -0600)
From: Joseph J. Strout
At 3:32 PM -0600 1/20/04, Chris Dillman wrote:

>>Well, of course, there are infinitely many positions in 3D space
>>that match the 2D mouse position. Which do you want? One in a
>>plane perpendicular to the camera's forward vector?
>
>Yes.

Ah, OK. So you want to take the equations used to convert from 2D to
3D coordinates for your 3D-sprite-sorta-thingy, and solve them for
world coordinates given screen coordinates.

That *should* be just a bit of algebra, but if it's not coming
together, let me know and I'll try to help. I've done this in the
past, but I don't have a good example handy.

Best,
- Joe

Re: Getting the position of a object to match the x,y of the mouse.
Date: 21.01.04 04:05 (Tue, 20 Jan 2004 21:05:02 -0600)
From: Chris Dillman
>At 3:32 PM -0600 1/20/04, Chris Dillman wrote:
>
>>>Well, of course, there are infinitely many positions in 3D space
>>>that match the 2D mouse position. Which do you want? One in a
>>>plane perpendicular to the camera's forward vector?
>>
>>Yes.
>
>Ah, OK. So you want to take the equations used to convert from 2D
>to 3D coordinates for your 3D-sprite-sorta-thingy, and solve them
>for world coordinates given screen coordinates.

Reading how you phrased it Im not sure if that is it or not.

BUT...

1. this is for the paving stone project.

2. I think I will need what you said above for the level editor etc
for the tactics engine.

>
>That *should* be just a bit of algebra, but if it's not coming
>together, let me know and I'll try to help. I've done this in the
>past, but I don't have a good example handy.

I keep thinking it should be some easy trig.
But so far I have failed to implement anything that works.

So if you find the time to work on the problem that would be wonderful.

Re: Getting the position of a object to match the x,y of the mouse.
Date: 25.01.04 21:52 (Sun, 25 Jan 2004 14:52:40 -0600)
From: Chris Dillman
>At 3:32 PM -0600 1/20/04, Chris Dillman wrote:
>
>>>Well, of course, there are infinitely many positions in 3D space
>>>that match the 2D mouse position. Which do you want? One in a
>>>plane perpendicular to the camera's forward vector?
>>
>>Yes.
>
>Ah, OK. So you want to take the equations used to convert from 2D
>to 3D coordinates for your 3D-sprite-sorta-thingy, and solve them
>for world coordinates given screen coordinates.
>
>That *should* be just a bit of algebra, but if it's not coming
>together, let me know and I'll try to help. I've done this in the
>past, but I don't have a good example handy.

Damn Im still getting no wear on this.

Joe if you get the time can you look into it?

Re: Getting the position of a object to match the x,y of the mouse.
Date: 25.01.04 22:40 (Sun, 25 Jan 2004 15:40:04 -0600)
From: Joseph J. Strout
At 2:52 PM -0600 1/25/04, Chris Dillman wrote:

>Damn Im still getting no wear on this.
>
>Joe if you get the time can you look into it?

OK, let's see what I can dig up. ...Here we go:

Function ScreenToWorld(view as Rb3DSpace, X as Integer, Y As Integer)
As Vector3D
// Find the point in 3D space represented by the given
// point in the 2D rendering.
Dim dist As Double
Dim screenPos As Vector3D

dist = Min(view.width, view.height) * 0.5 / tan(view.FieldOfView *
kDegrees * 0.5)

screenPos = New Vector3D
screenPos.X = (view.width/2 - X)
screenPos.Y = (view.height/2 - Y)
screenPos.Z = dist
screenPos = view.camera.orientation.Transform(screenPos)
screenPos.Add view.camera.position

return screenPos
End Function

That gives you a position within the Hither plane that corresponds to
the given X and Y.

Additionally, you may find this useful:

Function ScreenToYPlaneIntercept(view As Rb3DSpace, planeY as Double,
X as Integer, Y As Integer) As Vector3D
// Find the place at which a ray through the X,Y point in the
// given view would intercept a horizontal plane at planeY.

Dim Q,V, out As Vector3D
Dim t As Double

Q = ScreenToWorld(view, X, Y)
V = Q.Minus(view.Camera.Position)

// The desired plane is a Y plane, with normal N=<0, 1, 0>
// and offset (D) at -planeY. The intersection of
// this plane and a line Q+t*V occurs at:
// t = -(N.Dot(Q) + D) / (N.Dot(V))
//
// But for our simple Y plane, this reduces to:
// t = -(Q.y + D) / V.y

if v.y = 0 then
return nil // our ray is parallel to the floor, never intersects it
end if

t = -(Q.y - planeY) / V.y
return Q.Plus(V.Times(t))

End Function

This finds the position in a plane perpendicular to the Y axis, which
is in line with a given X and Y on screen. Useful if you're placing
or dragging items on a floor, and your floor is a Y plane.

Finally, if you need something even more general, here's the
intersection of an arbitrary plane with a line through two points
(for example, the camera position and a point found via ScreenToWorld
above):

Function PlaneIntersection(planeNormal As Vector3D, planeD As Double,
p0 As Vector3D, p1 As Vector3D) As Vector3D
// Find the intersection of the line from p0 to p1 with the given plane.

// See Lengyel p. 85.

Dim t As Double
Dim V As Vector3D

V = p1.Minus(p0)
t = -(planeNormal.dot(p0) + planeD) / (planeNormal.Dot(V))
return p0.Plus(V.Times(t))

End Function

Enjoy,
- Joe

Re: Getting the position of a object to match the x,y of the mouse.
Date: 26.01.04 00:00 (Sun, 25 Jan 2004 17:00:25 -0600)
From: Chris Dillman
>At 2:52 PM -0600 1/25/04, Chris Dillman wrote:
>
>>Damn Im still getting no wear on this.
>>
>>Joe if you get the time can you look into it?
>
>OK, let's see what I can dig up. ...Here we go:

Thanks Joe.

Will see were this gets me and I will get back to you.