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

All topics about ZGameEditor goes here.

Moderator: Moderators

jinxtengu
Posts: 79
Joined: Wed Oct 14, 2009 2:05 pm
Contact:

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

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;``````

Kjell
Posts: 1706
Joined: Sat Feb 23, 2008 11:15 pm

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

Hi jinxtengu,
jinxtengu wrote:
Sat Apr 06, 2019 3:44 am
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.
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

K
Attachments
Wolfenstein.zgeproj

VilleK
Posts: 1989
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

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

Poor example Kjell, where are the nazi soldiers

Kjell
Posts: 1706
Joined: Sat Feb 23, 2008 11:15 pm

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

Ehm,
VilleK wrote:
Mon Apr 08, 2019 7:26 am
Poor example Kjell, where are the nazi soldiers
Fine .. added a dead soldier to the example

K

VilleK