How to represent 3d objects in 2d

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.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: How to represent 3d objects in 2d

Post by pgimeno »

adge wrote:Should I maybe use matrices? Are they faster?
That depends on how you're doing your rotations now. Otherwise no one can say "faster than what".

Anyway, a rotation matrix is usually the fastest method for rotating vectors. https://en.wikipedia.org/wiki/Quaternio ... omparisons
adge
Citizen
Posts: 54
Joined: Mon Dec 14, 2015 8:50 pm

Re: How to represent 3d objects in 2d

Post by adge »

faster than common equations like this one:

Code: Select all

function rotateZ3D(alpha)
	rad = alpha * math.pi/180
	sin = math.sin(rad)
	cos = math.cos(rad)
	for a = 1, #nodes, 1 do
		node = nodes[a]
		x = node.x
		y = node.y
		node.x = (x * cos - y * sin)
		node.y = (y * cos + x * sin)
	end
end
Moreover I would like to recreate this effect:
http://www.lexaloffle.com/bbs/?tid=3181

How would this be done? Using a shader? I thought of maid64 and the faces drawing function would draw lines. But this sounds resources hungry.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: How to represent 3d objects in 2d

Post by vrld »

That bit of code

Code: Select all

      node.x = (x * cos - y * sin)
      node.y = (y * cos + x * sin)
is a matrix multiplication of the matrix \[\begin{pmatrix}
\cos\alpha & -\sin\alpha\\
\sin\alpha & \cos\alpha
\end{pmatrix}\] with the vector \[\begin{pmatrix}node.x \\ node.y\end{pmatrix}\].

Matrices (or rather matrix-vector-products) are just a way to write multiple equations using less characters (mathematicians are lazy people). There is nothing magical about them.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Tanner
Party member
Posts: 166
Joined: Tue Apr 10, 2012 1:51 am

Re: How to represent 3d objects in 2d

Post by Tanner »

Since this seems to be the direction you're headed in, here's a guide on how to write a software renderer from scratch. https://github.com/ssloy/tinyrenderer/wiki
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: How to represent 3d objects in 2d

Post by pgimeno »

vrld is right, that's actually 2D matrix rotation. But since that's a rotation over a single 3D axis and you need several of these to obtain any 3D rotation, it's likely that you can obtain faster results by doing the whole 3D rotation in one go, using a 3D rotation matrix rather than a composition of 2D rotations, because you do the sines and cosines just once and apply the result to all vectors.
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: How to represent 3d objects in 2d

Post by Davidobot »

adge wrote:Should I maybe use matrices? Are they faster?
Like pointed out by the rest, the functions present are basically identical to what you would see using matrices.
adge wrote:Add lighting. That would be cool.
I haven't worked on this project in well over 6 months, maybe I'll pick it up again if I get inspired.
adge wrote:How are faces done? Just draw a polygon or rectangle with the nodes positions?
That's right, a polygon is drawn between each set of nodes.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
adge
Citizen
Posts: 54
Joined: Mon Dec 14, 2015 8:50 pm

Re: How to represent 3d objects in 2d

Post by adge »

Tanner wrote:Since this seems to be the direction you're headed in, here's a guide on how to write a software renderer from scratch. https://github.com/ssloy/tinyrenderer/wiki
Wow that sounds extremely cool! Thank you!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 57 guests