Mouse in 3D space

Advanced game related topics
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Mouse in 3D space

Post by Comtois »

small example

http://perso.wanadoo.fr/comtois/sources/Souris3D.zip

- PageUp and PageDown to raise or lower the height of the principal cube .

- Click to place a small cube at the position of the mouse
- Mouse changes color when there is target detection(one sphere and one cube)


Last one :

http://perso.wanadoo.fr/comtois/sources/Souris3D2.zip
Last edited by Comtois on Fri Oct 06, 2006 4:36 pm, edited 1 time in total.
Please correct my english
http://purebasic.developpez.com/
SCRJ
User
User
Posts: 93
Joined: Sun Jan 15, 2006 1:36 pm

Post by SCRJ »

Cool 8)
Thanks for sharing
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Nice. Thanks!
Dare2 cut down to size
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Post by Comtois »

Thank you .

Added calculation of the distances to select an object

http://perso.wanadoo.fr/comtois/sources/Souris3D1.zip

[EDIT]
Small corrections

Code: Select all

Global Dim ObjetBoite.s_Boite(#NbBoites)
Global Dim ObjetSphere.s_Sphere(#NbSpheres)
the file is updated.
Please correct my english
http://purebasic.developpez.com/
JonChaos
User
User
Posts: 26
Joined: Wed Nov 03, 2004 11:23 pm

Post by JonChaos »

In Ogre3D you can create a Ray Object and check if it cuts a bounding box.
Sadly it isnt exportet in the OgreDll used in Pb.

At the moment we need to create an additional bounding box or sphere for each object we want to check.
Its not only the extra resources which are wastet but mainly the enormous programming work that is simply not neccesery if only the existing functions in Ogre would be exportet.

Functions like
#Entity = MouseOverEntity()
x = MouseoverEntityPartX(#Entity)
y = MouseoverEntityPartY(#Entity)
z = MouseoverEntityPartZ(#Entity)
bool = EntityIsInfrontOfCamera(#Entity)
x = MouseOverTerrainX()
y = MouseOverTerrainY()

Would be more than usefull.
And because most of the work is already done by the creators of Ogre there would be no need to reinvent the wheel.

The MouseOverEntity() function on its own would be verry helpfull.
Hopefully someday it will be implementet as a PB standard Function.
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Post by Comtois »

Yes that would be better if it were implemented in purebasic.
But while waiting for that, it allows to advance in the projects 3D.

and it is always good to know how it work :)

Btw here is a new version , added cylinder.

http://perso.wanadoo.fr/comtois/sources/Souris3D2.zip
Please correct my english
http://purebasic.developpez.com/
JonChaos
User
User
Posts: 26
Joined: Wed Nov 03, 2004 11:23 pm

Post by JonChaos »

I worked out another aproach to this problem.

In simple words does it imitate the Ogre Raycast function.

In basics we have nearly everything we need.

We have our Camera Position in the World which can be retrieved with the
CameraX()
CameraY()
CameraZ()
Functions.

And we should know where the Camera is looking @.
( The values we give to CameraLookAt(X,Y,Z)

With this information we can create a Vector(StartPoint,Endpoint) where the Startpoint is the Camera Location and the Endpoint is where the Camera looks @.

Now we clone that vector and move both vector Points so the new vector is like Vector([Xstart+MouseOffset,Ystart+Mouseoffset,Zstart),[Xend+MouseOffset,Yend+Mouseoffset,Zend)

Along that vector we create a line/ray Entity+Mesh ( dont know how to describe it better ) with the lengt given by the maximum distence our picable objects are still rendered ( makes no sense to try to pick things we cant see ^^ )

Now we give our Ray a Physical Body with
EntityPhysicBody(#RayEntity, #PB_Entity_StaticBody)
and every pickable Object will get a suitable Physical Body.

When we do our Collission Check we can simply conpute the Object closest to the Camera which had a colission with our ray.
This object is the entity the user pointed on with his mouse.

Problems with this approach are
- we have to recompute the look@ Coordinates if we move the camera because we can set the value but cant retrieve the new value if we move our camera
- with a certain fov the approach wont work
- if the ray gets "out of the world" it will crash :?


But for things where the camera is always on a 2d plane and the angle to the ground is always the same ( like a 3d rts / rbs ) it should work fine to pick your units.
Still leaves the problem that we cant check colissions with the terrain so we cant order them to move to poin x/y ^^
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Post by citystate »

To prevent the Out_of_world_crash, you could set a maximum range; if the ray's distance from the camera is greater than this number, return a no_collision value. Of course, I *could* just be talking out of my hat :)
Post Reply