2D to 3D Cordinates and vice versa

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

Clyde(Posted 2005) [#1]
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 :)


Berserker [swe](Posted 2005) [#2]
Try changing EntityX(Ent,False) (wich is standard) to EntityX(Ent,True) and so on. I havent looked after but i think thats right.


Shifty Geezer(Posted 2005) [#3]
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.


Clyde(Posted 2005) [#4]
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 ?


GitTech(Posted 2005) [#5]
3d-to-2d: Have a look at:

CameraProject()
ProjectedX()
ProjectedY()


Berserker [swe](Posted 2005) [#6]
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^^


KuRiX(Posted 2005) [#7]
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).


Stevie G(Posted 2005) [#8]
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



BlitzSupport(Posted 2005) [#9]

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.


Naughty Alien(Posted 2005) [#10]
..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..


Berserker [swe](Posted 2005) [#11]
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!


RiverRatt(Posted 2005) [#12]
Is something like this what your after?



Clyde(Posted 2005) [#13]
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 :)


Xaron(Posted 2005) [#14]
As GitTech wrote, use CameraPick:

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

Regards - Xaron