Rendercam with 3d object clickabled

I quite new in Defold, im alredy read many of topic and try read API but still i dont know how make 3d object clickabled. I mean for now i understand i must convert mouse position from world to screen -:

posi = rendercam.screen_to_world_2d(action.screen_x, action.screen_y)

but i dont know which one transformation use for object. or how do it properly bcoz i try many way but at all no one work.

For learning i use https://github.com/rgrams/rendercam

elseif action_id == hash("right click") then

posi = rendercam.screen_to_world_2d(action.screen_x, action.screen_y)
		
		if rendercam.world_to_screen(go.get_world_position(go.get_id("model"))) == posi then
					
		go.delete(go.get_id("model"))

end

of course i add “rightclick” and also " left click" in game input binding, and i check do it works ( i mean i try just something like that :

elseif action_id == hash("left click") then
		
		go.delete(go.get_id("model"))

and that work but i dont know how make reaction when cursor will by on object and click object / model ?

1 Like

Hello @Haxquv,

Have you seen the question I have asked a while ago: Need help with interacting in 3d. It is about how to detect a 3d object (in a 3d world) when you click on it and the solution was to use ray casting. This could help you out, too.
If you get stuck, give me a shout.

2 Likes

hello @anon95708182 ,
i read that but i dont know how exactly use a rendercam.screen_to_world_ray()
i mean what put to

 if ... then

like i say i’m just begginer, i also look at Drill example but for my its still too hard teach self from deep advanced exapmle.
It’s reason why i’m just start at RenderCam example where is just one 3d object(model)

Hello @Haxquv,

yes, it is hard, I know :smiley:.
So, do I understand you right: you have a 3d project with one model in it, do you?

Hi @raetia, again.

Yes for now i try learning so have one 3d object, but of course will by more i can add more, and i wanna for now just know how make reaction when i exactly click specific model.

And what i understand when i create 3d world with objects and all objcets in same surface (plane) actually objects dont use y-coordinate (mean y is always 0 for all off objects)

and if i good understand Defold screen / mouse use only x, y but like i say in first post i dont know how convert 3d go.postion to screen - vector (which one of transform from RenderCam - use for make it )
or simpler step by step if someone can on RenderCam exaple show my properly way to make reaction when i click a “model object”

Hello again,

I am not so good at explaining, especially since I just started to experiment with 3d myself.
So, I give you my code, see if it works for you:
First of all, the go you want to detect needs a collision object with a collision shape.
You named yours “model”, so that’s what I did, too:

Screenshot 2022-02-22 at 08.25.36

Collision objects have a field called “group”. You can order objects in several groups and give each of these groups a unique name.
In your case, with just one model, one group is enough and I left the name at “default”:

Screenshot 2022-02-22 at 08.25.57

And now comes the code (put this into the input function):

if action_id == hash("right_click") then
	-- start point and end point of the ray:
	local start_point, end_point = rendercam.screen_to_world_ray(action.screen_x, action.screen_y, false)
	-- add collisonobjects to the game objects you want to detect and order them into groups as needed
	-- local groups = {hash("a_group"), hash("another_group"), hash("yet_another_group")}
	-- you want to detect just one model, so there is only one group and I left the group name at "default"
	local groups = {hash("default")}
	-- cast the ray
	local result = physics.raycast(start_point, end_point, groups)
	-- if the ray hits something in a group, it returns the id
	if result then
		-- you want to delete an object called "model":
		if result.id == hash("/model") then
			go.delete(result.id)
			-- or: 
			-- go.delete(hash("/model"))
		end
		-- if there is only one group with one game object, you don't need the if statement:
		-- go.delete(result.id)
	end
end

Try this and read a bit about raycasting in order to understand what is going on here. As I said, I am a 3d novice as well.

3 Likes

Hi @anon95708182
Yes that can work but onl when i know “id” of object.

If i will create object on world by factory.create then i don’t know theirs ids, so i cannot control reaction of click them by result.id

or

i don’t understand something

i wish exist for 3d object clickabled in 3d world something like @britzl created https://github.com/britzl/defold-input .

Maybye @ross.grams can help with RenderCam ?

Hi there again,

Aha, we are talking about factories now, not the single 3d object you had asked about and that you wanted to delete when clicking on it.
I had no need for factories so far, so, sorry that I cannot be of more help to you, maybe others can.

1 Like

Yeah, something like that would indeed be very useful! I think it’s possible to create such a helper module and base it on raycasts and some code from rendercam. You’d need to provide it with the camera view projection.

Raetia’s example checked for a specific ID, but that part isn’t required, it was just an example. You get the ID of the hit object and other info from the raycast and you can do whatever you want with it. You don’t have to know what the ID is at all, you can just send it messages.

Step 1 - Store the last hit ID on self, to remember it from frame to frame, let’s call it, self.lastHitID.

  • cursor_over - If the raycast gives a result but self.lastHitID is nil, set self.lastHitID to result.id and send it the cursor_over message.
  • cursor_out - If result is nil but self.lastHitID is not, send the cursor_out message and set self.lastHitID to nil.
  • pressed - Do this in on_input. If self.lastHitID exists when the click is pressed, send the message.
  • released - Likewise for click release. You probably want to also store (on self) the last ID that click was pressed on and check that, so you don’t send release events to objects that were never pressed (If you click on empty space, drag, and release on an object).
4 Likes