Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Q] Any usage examples, tests? #41

Open
trsh opened this issue Oct 2, 2019 · 3 comments
Open

[Q] Any usage examples, tests? #41

trsh opened this issue Oct 2, 2019 · 3 comments

Comments

@trsh
Copy link

trsh commented Oct 2, 2019

Especially interested in octree

@shakesoda
Copy link
Member

shakesoda commented Oct 2, 2019

the tests (for busted) are in spec, but the ones for octree are still a (long standing) todo... general usage is something like:

-- build an octree
local world = cpml.octree(1024, cpml.vec3(0, 0, 0), 5, 1.1)
for i, v in ipairs(everything) do
	local bounds = cpml.bound3(v.min, v.max) -- up to you to have bounds for your data
	world:add(v.data, bounds)
end

-- cast a ray through it
local function ray_callback(ray, objects, out)
	for _, object in ipairs(objects) do
		-- you'll need to change this with the appropriate intersection
		-- for whatever data you've actually put in the tree
		local point = cpml.intersect.ray_triangle(ray, object.data)
		if point then
			local dist = point:dist(ray.position)
			if out.dist > dist then
				out.dist = dist
				out.point = point
			end
		end
	end
end

local ray = {
	position  = cpml.vec3(0, 0, 10),
	direction = -cpml.vec3.unit_z
}
local out = { dist=math.huge }
world:cast_ray(ray, ray_callback, out)

if out.point then
	-- do something with the intersection point
end

-- get everything in provided bounds
local hits = world:get_colliding(cpml.bound3(cpml.vec3(-5, -5, -5), cpml.vec3(5, 5, 5)))
for i, v in ipairs(hits) do
	-- do something with the hit nodes
end

these are the interesting functions, at least - if you check the docs it's got the rest, but for the most part other than these functions you'll only usually use remove and maybe the draw functions if you use love (it expects a mesh object with a unit sized cube).

this is actually pretty much everything i remember about it - i haven't been using cpml regularly myself since i started writing my games in haxe, so i had to hunt through old code to write about it.

@trsh
Copy link
Author

trsh commented Oct 2, 2019

@shakesoda do you intend to support this package and finish those tests for octree?

@shakesoda
Copy link
Member

i don't personally intend to finish those tests anytime soon unless i start needing it again, but i do intend to make time to fix a few of the backlog bugs (particularly, the mat4 ones and #39) and i'm always up for answering questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants