Groverburger's 3D Engine (g3d) v1.5.2 Release

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
groverburger
Prole
Posts: 49
Joined: Tue Oct 30, 2018 9:27 pm

Re: Groverburger's 3D Engine (g3d) v1.3 Release

Post by groverburger »

pgimeno wrote: Sun Feb 21, 2021 3:18 am No, the w of a quaternion is not the angle! It's the real component, which for a quaternion representing a pure rotation (i.e. normalized), equals the cosine of half the rotation angle. The name of the function suggests that it accepts a quaternion, but an axis/angle rotation is not a quaternion, even if the conversion to quaternion is fairly simple. That's why I suggest choosing a name that doesn't cause this confusion. A quaternion is what you use internally as rotation[1..4]. I'd expect a function called setQuaternionRotation to set rotation[1..4] to each input parameter. Something like:
Oh good point, I mixed up some math lingo :oops:. Thanks for the feedback.
Just pushed a commit to fix this by splitting the function into two:

Code: Select all

-- create a quaternion from an axis and an angle
function model:setAxisAngleRotation(x,y,z,angle)
    x,y,z = vectorNormalize(x,y,z)
    angle = angle / 2

    self.rotation[1] = x * math.sin(angle)
    self.rotation[2] = y * math.sin(angle)
    self.rotation[3] = z * math.sin(angle)
    self.rotation[4] = math.cos(angle)

    self:updateMatrix()
end

-- rotate given one quaternion
function model:setQuaternionRotation(x,y,z,w)
    self.rotation[1] = x
    self.rotation[2] = y
    self.rotation[3] = z
    self.rotation[4] = w
    self:updateMatrix()
end
I could also consolidate model:setRotation and model:setQuaternionRotation into just one function with an optional fourth argument...
User avatar
pgimeno
Party member
Posts: 3655
Joined: Sun Oct 18, 2015 2:58 pm

Re: Groverburger's 3D Engine (g3d) v1.3 Release

Post by pgimeno »

Sounds sensible.
User avatar
Nikki
Citizen
Posts: 84
Joined: Wed Jan 25, 2017 5:42 pm

Re: Groverburger's 3D Engine (g3d) v1.3 Release

Post by Nikki »

Hi Groverburger, starting to play around with your engine,

Image

Now i am wondering, i use the collision code to figure out a collision, that works.

If i would want to move an object in the 3d world, by dragging with the mouse, what steps would i need to take ?
User avatar
Nikki
Citizen
Posts: 84
Joined: Wed Jan 25, 2017 5:42 pm

Re: Groverburger's 3D Engine (g3d) v1.3 Release

Post by Nikki »

Sorry for the double posting, but another question which is a smaller step to my goal,

say you disable the firstperson controller, how do you still use the collision code ?
how should i calculate the look vector (to plug in the rayIntersectionAABB) in that case?

I would like the camera to be relatively static and be able to move the mouse and have collision with the mouse working.

Sorry for these noobish questions, but i am a 3d noob.
User avatar
groverburger
Prole
Posts: 49
Joined: Tue Oct 30, 2018 9:27 pm

Re: Groverburger's 3D Engine (g3d) v1.3 Release

Post by groverburger »

Nikki wrote: Sun Apr 18, 2021 7:54 am Hi Groverburger, starting to play around with your engine,

Image

Now i am wondering, i use the collision code to figure out a collision, that works.

If i would want to move an object in the 3d world, by dragging with the mouse, what steps would i need to take ?
Maybe you could get the mouse movement information with the love.mousemoved callback function, and move the model with the setTranslation function? You could also plug the mouse coordinates directly into the translation function, but neither of these will move the model relative to your viewport. That would require some trigonometry and math stuff.

Either way, the g3d model API wiki link might be useful to you here.
User avatar
Nikki
Citizen
Posts: 84
Joined: Wed Jan 25, 2017 5:42 pm

Re: Groverburger's 3D Engine (g3d) v1.3 Release

Post by Nikki »

HI again, ive been fiddling a whole lot, but my lack of 3d knowlegde is deep ;)

the thing i am trying to achieve:
i want to be able to move the mouse (without moving the camera) and still have correct hittesting

The way i am trying to achieve this is :

Code: Select all

function getUnitPos(x,y)
 local w,h = love.graphics.getDimensions()

 local ndcX = -1 + 2 * x / w 
 local ndcY = 1 - 2 * y / h
 return ndcX, ndcY
end

function camera:getMouseLookTarget(mx,my)
   local x,y = getUnitPos(mx,my)

   local rchange = (x * camera.fov/2 ) 
   local pchange = (y * camera.fov/2 ) 

   
   local tempR = fpsController.direction + rchange
   local tempP = fpsController.pitch + pchange

   local sign = math.cos(tempP)
   sign = (sign > 0 and 1) or (sign < 0 and -1) or 0

    -- don't let cosPitch ever hit 0, because weird camera glitches will happen
    local cosPitch = sign*math.max(math.abs(math.cos(tempP)), 0.00001)

    local t1 = camera.position[1]+math.sin(tempR)*cosPitch
    local t2 = camera.position[2]-math.sin(tempP)
    local t3 = camera.position[3]+math.cos(tempR)*cosPitch
    return t1,t2,t3

end


function camera:getMouseLookVector(mx, my)
    local t1,t2,t3 = camera:getMouseLookTarget(mx,my)
    local vx = t1 - camera.position[1]
    local vy = t2 - camera.position[2]
    local vz = t3 - camera.position[3]
    
    local length = math.sqrt(vx^2 + vy^2 + vz^2)

    -- make sure not to divide by 0
    if length > 0 then
        return vx/length, vy/length, vz/length
    end

end
basically what I am doing is :
translate the mouseposition to a range of [-1,1], [-1,1] instead of the screen position.
then multiply those unit values with the fov and add it to fpsControllers pitch and rotation.
using the same code thats already working for the fps controller

Its working a little bit, but i am missing some calculations and i dont know what.
Can anyone help me ?

oh btw, in the attached file, wasd moves the camera,
when you drag the mouse it also changes the rotation/pitch,
blocks light up red if the new collision check hits (this isnt working correctly)
blocks light up pink if the normal check is hitting (this works corretly but is limited to the center of the screen)
Attachments
g3d-mousehit.love
(3.67 MiB) Downloaded 587 times
DerpChest
Prole
Posts: 27
Joined: Tue Oct 06, 2020 1:11 pm

Re: Groverburger's 3D Engine (g3d) v1.3 Release

Post by DerpChest »

I am now blind because of the old code, I am expecting you to pay for my medical bill.
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: Groverburger's 3D Engine (g3d) v1.3 Release

Post by 4vZEROv »

Nikki wrote: Sun Apr 25, 2021 12:52 pm
This video explain how to do it well:
https://www.youtube.com/watch?v=DLKN0jExRIM

You can check my implementation here :
https://github.com/4v0v/3v3n_m0r3_3d

The interesting part is:

Code: Select all

function Camera:get_mouse_ray()
	-- viewport space
	local mouse_x, mouse_y = love.mouse.getPosition()
	local width  , height  = love.graphics.getDimensions()

	-- normalized device space
	local normalized_x = 2 * mouse_x / width  - 1
	local normalized_y = 2 * mouse_y / height - 1 

	-- clip space
	local clip_coord = {normalized_x, normalized_y, -1, 1}

	-- eye space
	local eye_coord = Matrices:transform_vector(self:get_inverse_projection_matrix(), clip_coord)
	eye_coord[3]    = -1       
	eye_coord[4]    = 0

	-- world space
	local world_coord = Matrices:transform_vector(self:get_inverse_view_matrix(), eye_coord)

	return Vectors:normalize(world_coord)
end
User avatar
Nikki
Citizen
Posts: 84
Joined: Wed Jan 25, 2017 5:42 pm

Re: Groverburger's 3D Engine (g3d) v1.3 Release

Post by Nikki »

Thats fantastic 4vZEROv !
Thanks a lot, or should i say
'7h4NK J00 50 MUcH'
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: Groverburger's 3D Engine (g3d) v1.3 Release

Post by 4vZEROv »

n0 pr0bl3m0
Post Reply

Who is online

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