[Solved] Projection of a 3D space in LÖVE

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
DiegoG
Prole
Posts: 20
Joined: Sat Apr 29, 2017 2:43 am
Location: Venezuela

[Solved] Projection of a 3D space in LÖVE

Post by DiegoG »

As said above, I need a formula to properly simulate a 3D space in LÖVE; not necessarily in a realistic manner.
I've been successful by setting a z axis in an object and then proceed to the following:

Code: Select all

m = 50 --1 meter in-game = 50 pixels on-screen
lg = love.graphics
rect = {0,0,1,3,3}

local x,y,z,w,h = unpack(rect)
lg.draw(dot, (x*m)/z , (y*m)/z , 0 , (w*m)/z , (h*m)/z)
and it worked somewhat properly.

I quickly found out that the coordinate system didn't work for this; so I got the idea of creating a camera object to change the drawn positions without changing the object's real position; that way I could draw it in the middle of the screen and it would still be at x=0,y=0

Code: Select all

m = 50 --1 meter in-game = 50 pixels on-screen
lg = love.graphics
rect = {0,0,1,3,3}
cam = {1,1,0,1,1}
cam[1],cam[2] = 800/2,600/2

local x,y,z,w,h = unpack(rect)
local cx,cy,csx,csy = unpack(cam)
lg.draw(dot, ((x * m)/z) + cx , ((y * m)/z) + cy , 0 , ((w * m)/z) + csx , ((h * m)/z) + csy)
It worked! It worked perfectly. Until I realized that I needed the camera for something else in my real project (I made another love project just to test the meter scale and the tridimensional simulation)

Now, I got the idea of making yet another camera object, one for the z axis, and the other for everything else. But I'm not sure that would work and even if it did I think it would be somewhat redundant, unpractical and it would make the processor pretty angry.

And that's why I'm here, Does anybody know a better way to do this?
Thanks in advance!

I also attached the .love file of this; just in case you want to know /exactly/ what I mean. If you want to check exactly why I don't want the camera to be at x=0,y=0; then try it out for yourselves
3D test.love
(2.76 KiB) Downloaded 78 times
Last edited by DiegoG on Tue May 23, 2017 11:17 pm, edited 1 time in total.
function earth:destroy()
00count=5000
00while count>0 do
0000lg.draw(explosion,math.random(0,600),math.random(0,800))
0000count=count-1
00end
00earth = nil
00print("Earth has been destroyed.")
end
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Projection of a 3D space in LÖVE

Post by raidho36 »

I have a library that does just that! But at the moment I can't upload it, so that will be few hours.

In meantime you can try using CPML, but it's not very user friendly and it's standard release is pretty slow.

Here it is!
Attachments
Screenshot from 2017-05-23 01-02-34.png
Screenshot from 2017-05-23 01-02-34.png (14.14 KiB) Viewed 2019 times
projection.love
(17.46 KiB) Downloaded 96 times
User avatar
DiegoG
Prole
Posts: 20
Joined: Sat Apr 29, 2017 2:43 am
Location: Venezuela

Re: Projection of a 3D space in LÖVE

Post by DiegoG »

It looks great!
Thank you ^^
function earth:destroy()
00count=5000
00while count>0 do
0000lg.draw(explosion,math.random(0,600),math.random(0,800))
0000count=count-1
00end
00earth = nil
00print("Earth has been destroyed.")
end
Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot] and 145 guests