Yet another 2D screen coords in 3D question

Blitz3D Forums/Blitz3D Programming/Yet another 2D screen coords in 3D question

_PJ_(Posted 2009) [#1]
I know there's a few topicas and code archives on this, but some are way too complex for what I am after, and others are just kinda confusing for me.

I have nailed down the basics, and have gotten the sprite image to appear relative to the camera, but I am stuck at actually positioning the items relative to the screen:

A simplistic sample of my progress is here:

Graphics3D 1024,768,32,2
Global cam=CreateCamera()

PositionEntity cam,Rand(-50,50),Rand(-50,50),Rand(-50,50),True
RotateEntity cam,Rand(359),Rand(359),Rand(359),True
TurnEntity cam,Rand(359),Rand(359),Rand(359),True

hud=CreateSprite()
tex=CreateTexture(256,256)
SetBuffer TextureBuffer(tex)
Color 255,255,255
Text 128,128,"HELLO WORLD",True,True
SetBuffer BackBuffer()
EntityTexture hud,tex
FreeTexture tex
tex=False

CameraProject cam,EntityX(cam,True),EntityY(cam,True),EntityZ(cam,False)

PositionEntity hud,ProjectedX(),ProjectedY(),ProjectedZ()
PointEntity hud,cam,EntityRoll(cam)
EntityParent hud, cam

While Not KeyDown(1)
UpdateWorld
RenderWorld
Flip
Wend


I'm pretty sure it will involve finding the local coords of 'hud' relative to its parent (cam), but without just finding what the right numbers are, is there a formula that corresponds to , say camera range, entitydistance(cam,hud) and perhaps camerazoom values?


Nate the Great(Posted 2009) [#2]
so im not sure what you are trying to do but maybe do what I did in my short project NTG image

I had a seperate camera for rendering the 2d sprites. I also made a plane in front of that camera and made the plane pickable. After that, it does a camera pic at each corner pixel of the screen. it uses the picked x,y,and z to determine where to put stuff.

Here I have uploaded it to my site. Download


Yasha(Posted 2009) [#3]
If the camerazoom is 1, an entity will fill the screen when its width is double its distance from the camera (or equal... I forget which but it's something like that). You can thus take advantage of this for pixel-perfect systems by placing the quad at 0,0,GraphicsWidth()/2 and making it GraphicsWidth() * GraphicsHeight() in size. You should then be able to just position objects according to their desired screen coordinates (or with the origin in the centre of the screen, but still simple enough) and get them in the right place. This is how Draw3D does it (remembering of course that since the HUD will be several hundred units away it needs to either be forced to render last or rendered separately).


_PJ_(Posted 2009) [#4]
Thanks people both helps a lot.

As for your suggestion, Yasha, I assume that the quad can be resized to match the resolution h/w ratio too?

I never thought of placing the quad so far away :)

Perhaps MeshHandle could even be used to relocate the origin too?


Stevie G(Posted 2009) [#5]
See this thread where I posted a simple example ...

http://www.blitzbasic.com/Community/posts.php?topic=52321#584740


_PJ_(Posted 2009) [#6]
Thanks, Stevie - I guess my search didnt get so far back :)


Part of my intent was to have a cursor too (I'm trying to get away from having any 2D drawings at all since they can be so slow) so your code is perfect.

Really ideal and exactly what I was looking for. I see you have essentially implemented the ideas from above there, but it's much easier for me to see what's happening in the code :)

I'l get back if I have any problems, but it's looking good so far!