# Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

### Newcomers AppGameKit Corner / How to delete 3d objects with left mouse click?

Message
Posted: 25th Feb 2018 21:11 Edited at: 25th Feb 2018 21:35
Here I generate 200 different cubes, and I would like to delete any one that I click on... by moving my mouse arrow to each one, one by one, and clicking the left mouse button.
But I don't know what input command I should use. Any body have an idea how I could do it?

I know that 'GetSpriteHit(x,y) that works with sprites, ....... but is there a 'GetObjectHit(x,y) ' equivalent that works with objects?

Posted: 25th Feb 2018 23:02 Edited at: 25th Feb 2018 23:08
You need these:
https://www.appgamekit.com/documentation/Reference/3D/Get3DVectorXFromScreen.htm

Insert this code at the beginning of your do-loop ( this is the example code from the online help, slightly modified to meet your needs )

if GetRawMouseLeftReleased ()
// get the position of the pointer (mouse)
pointer_x = GetPointerX()
pointer_y = GetPointerY()

// get the x, y and z unit vectors based on the pointer position
unit_x# = Get3DVectorXFromScreen(pointer_x,pointer_y)
unit_y# = Get3DVectorYFromScreen(pointer_x,pointer_y)
unit_z# = Get3DVectorZFromScreen(pointer_x,pointer_y)

// calculate the start of the ray cast, which is the unit vector + the camera position
start_x# = unit_x# + 0
start_y# = unit_y# + 0
start_z# = unit_z# - 180

// calculate the end of the vector, which is the unit vector multiplied by the length of the ray cast and then add the camera position to it
end_x# = 500*unit_x# + 0
end_y# = 500*unit_y# + 0
end_z# = 500*unit_z# - 180

// determine which object has been hit
object_hit = ObjectRayCast(0,start_x#,start_y#,start_z#,end_x#,end_y#,end_z#)

// if an object has been hit then destroy it
if object_hit <> 0
DeleteObject ( object_hit )
endif
endif

PSY LABS Games
Coders don't die, they just gosub without return
Posted: 26th Feb 2018 19:12
Wow, thanks, that's quite a answer, and I'm glad I didn't try to figure it out on my own. I really appreciate it.
Posted: 27th Feb 2018 13:50
You're welcome

PSY LABS Games
Coders don't die, they just gosub without return
Posted: 27th Feb 2018 15:20
Hi
It seems it doesnt always work with an ortho camera (fov =0)?
AGK2 tier1 - http://www.dracaena-studio.com
Posted: 28th Feb 2018 18:00 Edited at: 28th Feb 2018 18:03
Probably because the code above does not update the camera location, but assumes a camera that stays in one place only. I would also add ScreenToWorldX to the pointer part of the code. Try this instead:

Posted: 1st Mar 2018 12:24 Edited at: 1st Mar 2018 12:33
Quote: "Hi
It seems it doesnt always work with an ortho camera (fov =0)?"

You need to take into account the distance of the camera from the origin point

Here is a working example using both ortho and freeflight camera

Quote: "I know that 'GetSpriteHit(x,y) that works with sprites, ....... but is there a 'GetObjectHit(x,y) ' equivalent that works with objects?"

A little reshuffle of the first 2 functions I posted here gives exactly that
Posted: 2nd Mar 2018 09:30 Edited at: 2nd Mar 2018 09:37
Hi thank you for your help, but that doesn't work ^^

@Partimecoder : you're example doesn't work because the camera isn't in ortho mode (need : SetCameraFov(1,0))

Here is a test code adapted from your example (ortho mode), but it doesn't work :

@phemox : even with ScreenToWorldX /y it odesn't work in ortho mode .
AGK2 tier1 - http://www.dracaena-studio.com
Posted: 2nd Mar 2018 11:42
@Blendman, I had a play with the camera in ortho mode and can not get any results at all, sorry but its got me beat.

This 3D camera stuff is a real pain and has me beat
Posted: 2nd Mar 2018 22:23
Quote: "
@phemox : even with ScreenToWorldX /y it odesn't work in ortho mode ."

Yeah, it works till SetCameraFOV(1,1) , but setting it to 0 seems to render the camera calculation for depth completely useless somehow. I wonder how we can calculate the correct depth.
Posted: 3rd Mar 2018 16:51
I fixed the code for orthographic mode

Also the original code wasnt right either even in projection mode so needed fixing.
Posted: 5th Mar 2018 12:03

@Bengismo : Thanks a lot, it works fine !
AGK2 tier1 - http://www.dracaena-studio.com
Posted: 7th Mar 2018 21:15
Neat trick setting the CameraZ like that for the vector Bengismo. Now it does give the correct depth.