Mouse pointer moving in 3D plane?
Blitz3D Forums/Blitz3D Programming/Mouse pointer moving in 3D plane?
| ||
Hi, I'm trying to make an RTS type of game and I can't figure out how to implement a mouse. The mouse will only be allowed to move on a plane but I want it to also be able to click buttons that will be rendered in 2D after RenderWorld. How can I get the 3D coordinates of where the mouse clicks on the plane? I've tried to use a entity that is controlled by the mouse but of course it moves incorrectly due to the camera perspective. Any ideas? Thanks in advance |
| ||
mouseover_entity = CameraPick(camera, MouseX(), MouseY()) |
| ||
Yeah but what about if I want to click on just the ground? Getting the ground entity doesn't help, I need the co-ordinates. |
| ||
RT*M. There's a link in my post to the documentation for CameraPick which will answer your current questions as well as most of the rest of them on this topic. |
| ||
Ahh ok thanks. I get it now, that was pretty simple actually. |
| ||
Here's some code I wrote based on a function from the Code Archives:; ----------------------------------------------------------------------------- ; Positions an entity in 3D from given 2D coordinates Function PositionEntityFrom2D(usecam,entity,x2d#,y2d#,height#=0,camZoom#=1) gw=GraphicsWidth() gh=GraphicsHeight() x#=-((gw/2)-x2d) y#=(gh/2)-y2d parent=GetParent(entity) EntityParent entity,usecam z#=Abs(EntityZ(entity)) div#=(gw/(2/camzoom))/z x=x/div y=y/div ;z distance determined by a straight line from camera, through xyz coordinates, to height TFormPoint x,y,z,usecam,0 y3d#=height x3d#=TFormedX() z3d#=TFormedZ() EntityParent entity,parent PositionEntity entity,x3d,y3d,z3d End Function ;----------------------------------------------------------------------------- This function is to place an object, but it's easy to modify to just return the coordinates in 3D space. As for clicking on 2D buttons also, just ignore these 3D coordinates when the mouse pointer is over buttons and do things the normal way. |
| ||
Hmmm interesting. So that kind of 'projects' the entity from the camera onto a flat plane at y=height. I think I'll just use CameraPick onto an zero alpha plane. Nice and simple and more direct. Thanks anyway though :) |
| ||
There are two concerns with using CameraPick for your 3D mouse: you only want to pick the alpha plane and nothing else, and the pick can be slow. Of course, if neither of these is a problem in your specific case, then yeah, using CameraPick is a lot simpler. In particular, you can get around the problem of only picking the alpha plane by changing the pickmode of entities immediately before doing the CameraPick and then changing them back after. |
| ||
If you decide later to let the player move and rotate the camera, or add hills and valleys to the ground, picking will still work for you. Also, if you make your game units pickable, you won't have problems with people clicking on the very top of one unit and having the unit behind it selected. |
| ||
If you decide later to let the player move and rotate the camera The code I wrote addresses that. That's what the TFormPoint is for. But the more I think about it, the more I think that function I posted is pointless. |
| ||
CameraPick isn't that slow is it? I'll only be doing it once per frame (with about 30 update frames per second maybe). Note: The game is set in space so there is no terrain. |
| ||
Picking is problematic if you are doing a bunch ever frame and/or if there is really detailed geometry for the pick to account for. Just one a frame is no big deal, and if your game is set in space then the pick isn't doing anything particularly complicated. Plus, it occurred to me that my method requires a pick anyway. Even if you don't use a pick to get the coordinates, you still need to determine what entity was selected. Thus, you might as well snag the coordinates from the pick. |
| ||
Ok thanks :D |