Learning 3D math/drawing/manipulation from scratch

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
togFox
Party member
Posts: 770
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Learning 3D math/drawing/manipulation from scratch

Post by togFox »

So, of course there are 3D engines out there and some of them in Love2D (Grove) but I wanted to truly learn and understand the 3D space and with a little googling and a little math I can draw a 3D cube and rotate it around three axis:

Image

It rotates just fine but it seems to rotate on more than one axis. Hard to explain. It will rotate on the Y axis as expect whilst ALSO revolving around some imaginary point. That point seems to be the screen origin - 0,0 - the top left corner of the screen.

I want the cube to rotate on the spot and not rotate around the screen origin whilst also rotating on the requested axis.

The code is super light so it's easy to share:

3DTest.love
(127.61 KiB) Downloaded 52 times
Key commands for rotating:

left arrow / right arrow
up arrow / down arrow
Del / PgDn

So, please note, all of this is very new and I only just learned about matrix multiplication so don't use long words please but I'm happy to google concepts. :crazy:

I suspect someone will say I need to factor object, world and camera co-ordinates. Or maybe something completely different.
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Learning 3D math/drawing/manipulation from scratch

Post by pgimeno »

I rather not touch that code with a long stick, but here are some tips that might be of interest to you.

You shouldn't modify the objects in place. Create a list of transformed objects to render; that will save you headaches. Also, make your objects centred: use positive and negative coordinates.

The rotation (model transform) should be applied first, and the object will rotate around its centre, but if you don't apply a "camera", it will rotate at (0,0). So then you apply the camera (view transform).

More complete 3D systems have:
  • A list of 3D models.
  • A list of which objects go into the scene and which transformation to apply to each in order to view them with their correct position and rotation within the scene. You can have repeated objects; for example several cubes from a single cube model, and each with its own transformation. This transformation is the model transform. The list is called the scene.
  • A camera transformation (the view transform). That's the position the scene is looked from, as well as the rotation.
  • A 3D transformation (the projection transform). For a certain frame, normally the projection and the view stay constant, therefore it's common to premultiply the projection and the view on every frame before starting to apply model transforms, which do vary per model.
To do this with OpenGL, you usually draw one model at a time, making the vertex shader apply the projection*view*model matrix to the vertices of the model so that they are visible in 3D, in their place within the scene, and from the camera location.
User avatar
togFox
Party member
Posts: 770
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Learning 3D math/drawing/manipulation from scratch

Post by togFox »

> if you don't apply a "camera", it will rotate at (0,0). So then you apply the camera (view transform).

I think this is my problem. I'll do this first. Baby steps!
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 2 guests