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

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
jinxtengu
Posts: 122
Joined: Wed Oct 14, 2009 2:05 pm
Contact:

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

Post 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;
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

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

Post 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
Attachments
Wolfenstein.zgeproj
(81.51 KiB) Downloaded 425 times
User avatar
VilleK
Site Admin
Posts: 2274
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

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

Post by VilleK »

Poor example Kjell, where are the nazi soldiers :D
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

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

Post 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
User avatar
VilleK
Site Admin
Posts: 2274
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

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

Post 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.
Post Reply