2D to 3D Cordinates and vice versa

Blitz3D Forums/Blitz3D Programming/2D to 3D Cordinates and vice versa

Hiya :)

Is there a way to convert a set of 2D Co-ordinates to 3D positions. And also, the other way around 3D to 2D.

Thanks heaps,
Clyde :)

Try changing EntityX(Ent,False) (wich is standard) to EntityX(Ent,True) and so on. I havent looked after but i think thats right.

There's a function called positionentityfrom2D() that I found somewhere which I use. Don't know where it's from though (search on this forum really sucks :( ) or who wrote it though. The code I've got is
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'm pretty sure there was one for the other way round, or I'm pretty sure you can use the TForm() functions.

thanks there folks :)

Say for instance in 2D: X=120 Y=200 in 3D would be ?
And also for example in 3D: X= 1.2, Y = 3.25 as 2D positions ?

3d-to-2d: Have a look at:

CameraProject()
ProjectedX()
ProjectedY()

Well an object in 2D has no Z value so im not sure what the
z3d# = Abs(EntityZ(entity))
would do. But since the "positionGlobal" is constant of 0 the entity will be put in Local coordinates (0=False).
"true if the position should be relative to 0,0,0 rather than a parent entity's position. False by default."

check the help: http://www.blitzbasic.co.nz/b3ddocs/command.php?name=PositionEntity&ref=comments

Anyway, im not sure about the function and i cant seem to have any use of it as for now, although i snipe in my game so... I wont look in to it right now.

^^Hope that made some sence^^

When you want to convert 2d-to-3d you need to set the Z value. The function that has been posted above is from BlitzSupport from the code archives, so it takes the screen x,y parameters and put the object in front of the camera so the object is in the same x,y to the given point and the Z is selected to the needed value.

Try it for yourself positioning an object in the x,y mouse values. Very nice code!

P.D: It doesn't work with ortographic projection (as lot of things that don't work neither).

I find that this is the simplest way of converting 2d to 3d. It really depends on what your doing as camerapick is slow if youve got quite a few objects and your testing every frame.

Note that the z coord should be changed depending on what you want to do.

Graphics3D 640,480,16,1

Global Camera = CreateCamera()
Global Plane = CreatePlane()
EntityColor Plane,50,50,50
PositionEntity Plane, 0,0,10
RotateEntity Plane,-90,0,0
EntityPickMode Plane, 2

Global Cursor = CreateSphere()

While Not KeyDown(1)

	CameraPick( Camera , MouseX(), MouseY() )
	PositionEntity Cursor, PickedX(), PickedY(), PickedZ()
	RenderWorld()
	
	Text 0,0,"2d coords "+MouseX()+"  "+MouseY()
	Text 0,20,"3d coords "+PickedX()+"  "+PickedY()+"  "+PickedZ()
	
	Flip
	
Wend



Well an object in 2D has no Z value so im not sure what the "z3d# = Abs(EntityZ(entity))" would do



I did that 2D-to-3D thing, here: "2D-to-3D entity positioning (or something)". It uses an entity's current z position, because, as you say, there can be no Z position in 2D, yet you have to put the 3D object somewhere z-wise. Try the demo given to see the effect.

..answer is simple..let say for instance you have X and Y positions....to make it easy, let say this X and Y positions is value of mouse position on screen...now you wanna move some object in 3D according to mouse movement in 2D..calculation will be simple, you have to recalculate new X and Y positions for object in 3D you wanna move...it will be like this..

xloc1=mxs*Cos(hor_rot)
zloc1=mxs*Sin(hor_rot)
xloc2=mys*Sin(hor_rot)
zloc2=-mys*Cos(hor_rot)

where is
Xloc1, and Zloc1 position of first coordinate(X) for your object in 3D

Xloc2, Zloc2 position of second coordinate (Z) for your object in 3D

mxs is mouse speed on x axis (MouseXspeed comand)
mys is mouse speed on y axis (Mouseyspeed comand)
hor_rot is horizontal camera rotation angle (around Y axis)

after you did this calculation and select your object
simply with next comands your object will be moved in
3D world acording to your mouse movement until you release it from mouse pointer.

TranslateEntity selected_object,xloc1*0.05,0,zloc1*0.05
TranslateEntity selected_object,xloc2*0.05,0,zloc2*0.05

this will move your object according to mouse movement in X,Z direction. For Y direction you can simply use terrain height or something else .. I'm using this in few industrial programs for IBS and architecture bussines and working well..I hope it will help..

Well thank you, this was never wreally my question though i was just trying to help Clyde. I think i know what you mean, but it still looks alittlebit more complex then it has to be.

Thanxs anyways!

Is something like this what your after?
; uses ModifyTerrain Example
; ---------------------

Graphics3D 800,600,16,3
MoveMouse 400,300
;HidePointer()
SetBuffer BackBuffer()
terra_size=128 ; initial size of terrain, and no. of grids segments, along each  side
x_scale=10 ; x scale of terrain
y_scale=10 ; y scale of terrain
z_scale=10 ; z scale of terrain
marker_x=terra_size/2 ; initial x position of marker
marker_z=terra_size/2 ; initial z position of marker
marker_y=0


camera=CreateCamera()
PositionEntity camera,(terra_size*x_scale)/2,50,0 ; position wherever; just  try and get good view of terrain!
RotateEntity camera,30,0,0 ; again, try and get good view of terrain
CameraClsColor camera, 70,70,250
light=CreateLight()
RotateEntity light,90,0,0

; Create terrain
terra=CreateTerrain(terra_size)
ScaleEntity terra,x_scale,y_scale,z_scale
EntityPickMode terra,2,True
EntityFX terra,4
EntityAlpha terra,.5

; Texture terrain
;grid_tex=LoadTexture("Maper_grid.bmp",4)
;EntityTexture terra,grid_tex
;EntityFX terra,1
;EntityAlpha terra,.5

;SetBuffer TextureBuffer()
grid_tex=CreateTexture( 32,32,4+8 )
ScaleTexture grid_tex,1,1
SetBuffer TextureBuffer( grid_tex )
Color 0,0,255:Rect 0,0,32,32
Color 0,0,0:Rect 0,0,32,32,False

SetBuffer BackBuffer()

EntityTexture terra,grid_tex


; Create marker
marker=CreateSphere()
ScaleEntity marker,1,1,1
EntityColor marker,255,0,0

While Not KeyDown(1)
;left\right
If KeyDown( 205 )=True Then TurnEntity camera,0,-1,0
If KeyDown( 203 )=True Then TurnEntity camera,0,1,0
;up\down
If KeyDown( 208 )=True Then TurnEntity camera,-1,0,0
If KeyDown( 200 )=True Then TurnEntity camera,1,0,0

If KeyDown( 30 )=True Then MoveEntity camera,0,0,1
If KeyDown( 44 )=True Then MoveEntity camera,0,0,-1

If MouseHit(3) Then
	PositionEntity camera,marker_x,marker_y+40,marker_z
	
EndIf
 




;marker_x=Int(PickedX#())/x_scale*x_scale
;marker_y=Int(PickedY#())/y_scale*y_scale
;marker_z=Int(PickedZ#())/z_scale*z_scale
marker_x=Int(PickedX#())
marker_y=Int(PickedY#())
marker_z=Int(PickedZ#())


CameraPick(camera,MouseX(),MouseY())
 

PositionEntity marker,marker_x,marker_y,marker_z

UpdateWorld 
RenderWorld

Color 250,210,150
Text 0,40,"PickedX: "+PickedX#()
Text 0,60,"PickedY: "+PickedY#()
Text 0,80,"PickedZ: "+PickedZ#()
Text 180,40,"marker_X: "+marker_X
Text 180,60,"marker_Y: "+marker_y
Text 180,80,"marker_Y: "+marker_z

Flip

Wend

End


Neat. And thankyou all.

What I wanted ideally, was a way of converting 2D Position to a 3D Position.

Say for arguments sake, I have a sprite at positions -1,2,3 ( X#/Y#/Z# ) and I then wanted to draw a rectangle / image with the 2D commands (x,y) at the same spot as the sprite.

Is there a simple-ish bit of math that could work this out?

Thanks kindly,
Clyde :)

As GitTech wrote, use CameraPick:

CameraProject(camera, EntityX(e), EntityY(e), EntityZ(e))
x# = ProjectedX()
y# = ProjectedY()

Regards - Xaron