Position entity 3d from 2d mouse
Blitz3D Forums/Blitz3D Beginners Area/Position entity 3d from 2d mouse
| ||
I've been trying to convert this wonderful bit of code to work on the X/Z axis (ie. looking straight down on a plane). At the mo, this is code for X/Y (looking into the distance). Can you help. Thanks. ; ----------------------------------------------------------------------------- ; PositionEntityFrom2D () ; ----------------------------------------------------------------------------- ; Positions an entity at 3D x/y co-ords translated from given 2D co-ords, at ; specified z position. Useful for positioning an entity at mouse x/y position, ; at its current z depth... ; ----------------------------------------------------------------------------- ; PARAMETERS... ; ----------------------------------------------------------------------------- ; REQUIRED... ; ----------------------------------------------------------------------------- ; entity is the entity to be positioned. ; x2d is the 2D x position you want translated to 3D. ; y2d is the 2D y position you want translated to 3D. ; z3d is the * 3D * z depth at which the translation should occur. ; ----------------------------------------------------------------------------- ; OPTIONAL... ; ----------------------------------------------------------------------------- ; positionGlobal defaults to false, as per PositionEntity. ; camZoom must be set manually if you've used CameraZoom to change it... ; ----------------------------------------------------------------------------- Function PositionEntityFrom2D (usecam, entity, x2d#, y2d#, positionGlobal = 0, camZoom# = 1) gw = GraphicsWidth () gh = GraphicsHeight () x# = -((gw / 2) - x2d) y# = (gh / 2) - y2d parent = GetParent (entity) EntityParent entity, usecam z3d# = Abs (EntityZ (entity)) div# = (gw / (2 / camzoom)) / z3d PositionEntity entity, x / div, y / div, z3d, positionGlobal EntityParent entity, parent End Function |
| ||
I wrote this function a while ago for a project. I think it does what you are asking: ; 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 It works at any camera angle to move an entity around on the ground plane (or whatever height you pass into the function.) Funny thing when I wrote this was that I stumbled on the right math by luck. It wasn't working at first, and then I did something I thought was blatantly wrong in order to test something, and against my expectation it worked. |
| ||
Thanks jhocking. It works great. |
| ||
Ask and ye shall receive. |