Page 1 of 1

stuck with simple 3d

Posted: Sat Oct 22, 2011 5:55 pm
by kraftman
So i have this fairly simple code:

Code: Select all

local cubes = {}

for i = 1, 20 do
	local c = {}
	c.x = math.random(0,800)
	c.y = math.random(0,600)
	c.z = math.random(0,255)
	tinsert(cubes, c)
end

function love.draw()
	for i = 1, #cubes do
		love.graphics.setColor(255,255,255,255-cubes[i].z)
		love.graphics.rectangle("fill", cubes[i].x, cubes[i].y,10,10)
	end
end
I want to add a basic 3d affect so that as i move the keys, the boxes move, but obviously at different speeds depending on their distance from the camera.

I'm guessing i need to add in a camera object and then work out the angles from there, but i'm not really sure how to do it.

Has anyone got a link to a decent tutorial on this kind of thing?

Re: stuck with simple 3d

Posted: Sat Oct 22, 2011 6:07 pm
by GijsB
you have a camera with a x,y and z position
for every block you draw you do like :

love.graphics.setColor(255,255,255,255-cubes.z+cameraz)
love.graphics.rectangle("fill", cubes.x+camerax/cubes.z, cubes.y+cameray/cubes.z,10,10)

i think this will give you some nice fake 3D effect :D

Re: stuck with simple 3d

Posted: Sat Oct 22, 2011 7:39 pm
by kraftman
GijsB wrote:you have a camera with a x,y and z position
for every block you draw you do like :

love.graphics.setColor(255,255,255,255-cubes.z+cameraz)
love.graphics.rectangle("fill", cubes.x+camerax/cubes.z, cubes.y+cameray/cubes.z,10,10)

i think this will give you some nice fake 3D effect :D



nice one! now how do i rotate them? ^^

Re: stuck with simple 3d

Posted: Sat Oct 22, 2011 7:43 pm
by Robin
For 3D rotation, you need matrix multiplication. I tried to do without them in Boids3D, but I couldn't figure out how it should happen then.

Re: stuck with simple 3d

Posted: Sat Oct 22, 2011 7:51 pm
by kraftman
Robin wrote:For 3D rotation, you need matrix multiplication. I tried to do without them in Boids3D, but I couldn't figure out how it should happen then.
yeh I took a look a look at your code briefly but I couldn't get my head around it, got any tips on getting started?

Re: stuck with simple 3d

Posted: Sat Oct 22, 2011 9:25 pm
by Robin
If you don't know how matrix multiplication works, I'd start with that.