Page 1 of 1

How to have a flat 3d cube always face the camera.

Posted: Sat Apr 06, 2019 3:44 am
by jinxtengu
Hi,
I've been experimenting with making Guis in Z game editor
and I'd like to make a game in the style of wolvenstein (just for a bit of fun) where the players weapon is visible in front of the camera, but I've been kinda struggling to get this to work.

Basically I want to have a flat object facing the camera on X Y and Z axis,
I've been using this code, which works for z and x axis, but not Y.
Sorry to post this again, I think Kjell might have answered this very same question a few years ago, but I can't find the code.

Code: Select all

float a, s, c, r, x, y;

a = App.CameraRotation.Y*PI*2;
s = sin(a);
c = cos(a);

r = tan(App.FOV/360*PI);  // If you're using a constant FOV, you can swap out this calculation with the resulting value.

x = App.MousePosition.X*r*App.ViewportWidth/App.ViewportHeight; // If you're using a constant aspectRatio, you can swap out "App.ViewportWidth/App.ViewportHeight" with a specific value.
y = App.MousePosition.Y*r;

cursor.position.X = App.CameraPosition.X+x*c+s;
cursor.position.Y = App.CameraPosition.Y+y;
cursor.position.Z = App.CameraPosition.Z+x*s-c;

// If you want the box to mimic the orientation of the camera, un-comment the following lines
cursor.rotation.z = 0-App.CameraRotation.z;
cursor.rotation.X = 0-App.CameraRotation.X;
cursor.rotation.Y = 0-App.CameraRotation.Y;

Re: How to have a flat 3d cube always face the camera.

Posted: Sat Apr 06, 2019 3:39 pm
by Kjell
Hi jinxtengu,
jinxtengu wrote: Sat Apr 06, 2019 3:44 amI'd like to make a game in the style of wolvenstein (just for a bit of fun) where the players weapon is visible in front of the camera, but I've been kinda struggling to get this to work.
You don't need any "fancy" calculations for that .. simply switch the camera from your 3D perspective camera to a 2D orthographic camera before you render your GUI elements. One of the examples i've posted on this topic can be found here.

However, i've also slapped together a quick & dirty Wolfenstein 3D example. Basically it renders a frame as follows ..

- Switch to 3D camera
- Set viewport to 3D region
- Render skybox ( light-grey floor + dark-grey ceiling )
- Render map
- Switch to 2D camera
- Reset viewport
- Render player weapon
- Render frame & HUD elements

And it should look something like this :wink:

Image

K

Re: How to have a flat 3d cube always face the camera.

Posted: Mon Apr 08, 2019 7:26 am
by VilleK
Poor example Kjell, where are the nazi soldiers :D

Re: How to have a flat 3d cube always face the camera.

Posted: Mon Apr 08, 2019 4:41 pm
by Kjell
Ehm,
VilleK wrote: Mon Apr 08, 2019 7:26 amPoor example Kjell, where are the nazi soldiers :D
Fine .. added a dead soldier to the example :roll:

K

Re: How to have a flat 3d cube always face the camera.

Posted: Tue Apr 09, 2019 7:03 am
by VilleK
I didn't realize before that it actually was possible to shoot and it switches to knife when out of ammo :D. Considering this is a project that you threw together as a reply to a question this is really cool.