Jump to content

Pick an object


gordonramp
 Share

Recommended Posts

Hi, I'm finally getting back into coding after a break making scenes. I've reached a block here and maybe someone can help out...

I want to pick a specific object. The following code I've adapted works but works on any object. Any ideas on how I can make it specific to a particular object (model). Note: at the moment the code will hide and show the mouse.

 

--pick an object 
if MouseHit(1)==1 then
	if picked==0 then
		pick=CameraPick(camera,Vec3(GraphicsWidth()/2,GraphicsHeight()/2,2.0),0,0)
		if pick~=nil then
			repeat
				if pick.entity:GetClass()==ENTITY_MODEL then
					break
				end
				pick.entity=pick.entity.parent
			until pick.entity==nil
			if pick.entity~=nil then
				ShowMouse()
				picked=1

			end
		end
	elseif picked==1 then
		picked = 0
	end
end--       

--Update controller        
controller:Update(camerayaw,move,strafe,jump,40,10)        
fw:Update() 

if picked == 1 then
	HideMouse()
end  

AMD Athlon x2 7750 2.7ghz, 6gb ddr2 ram, Galaxy9800GT 1gig ddr2 video card, Windows 7,64.

Link to comment
Share on other sites

instead of looping and checking for the model class, just check that the specific model's name is the one you want... try using the GetMeshModel() from the utilities.lua

 

going from memory here since i currently do not have access to LE... :)

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Ok i'll try, so many time i don't use bmax..

Anyway you can always set a different type to a mesh (EntityType)

And check if that picked object is that type. (GetEntityType)

 

If you don't need this just explain exactly what you need. Almost everything it can be done using filters, wich is a sort of callback function that return 0 to skip or 1 to check the raycast.

Intel Corei7-6700, NVIDIA GeForce GTX 980, 32GB DDR4, W-10.

Link to comment
Share on other sites

Thanks Joh, I'm writing my whole game in Lua.. and trying to learn the language as I go. This line from my example code seems to be the issue..

if pick.entity:GetClass()==ENTITY_MODEL then

I am able to pick any object as I said but not just one. If use the code as it is then the Player can pick the whole scene which is not good.

AMD Athlon x2 7750 2.7ghz, 6gb ddr2 ram, Galaxy9800GT 1gig ddr2 video card, Windows 7,64.

Link to comment
Share on other sites

Thanks Joh, I'm writing my whole game in Lua.

Just sorry me.. It's monday..

If use the code as it is then the Player can pick the whole scene which is not good.

Just a fast question.. Did you place the whole scene into a mesh?

Anyway i didn't refered to that command but to this one (entity.collisiontype finally in lua :) )

If the mesh are separated (each one) you surely used a collisiontype to let it collidable, so you could set a different collision type to check if you can pick them or not.

 

In this case you simply could do the pick testing and check if that mesh is of that specific type.

Intel Corei7-6700, NVIDIA GeForce GTX 980, 32GB DDR4, W-10.

Link to comment
Share on other sites

Just a fast question.. Did you place the whole scene into a mesh?

Not one mesh but basic indoor scenes are modelled in 3dws and are divided into two or three and imported into the Leadwerks Editor. Then objects are added.

Setting a different collision type sounds like an interesting way to do it. I will look at that too.

AMD Athlon x2 7750 2.7ghz, 6gb ddr2 ram, Galaxy9800GT 1gig ddr2 video card, Windows 7,64.

Link to comment
Share on other sites

This line from my example code seems to be the issue..

if pick.entity:GetClass()==ENTITY_MODEL then

A good way to do this would be when you can specify your own group. Instead of ENTITY_MODEL, you can use ENTITY_SPECIAL. And then for the objects you want to pick up or interact with, you would have to add something ini or lua file. Something like this: class = ENTITY_SPECIAL.

I don't know if that is possible though.

 

another approach is adding a lua script to the objects, saying wether you want to use them. If the value is true then the object can be picked.

 

see also these topics:

http://leadwerks.com/werkspace/index.php?/topic/332-get-the-valie-from-lua-option/page__p__2700__fromsearch__1entry2700

http://leadwerks.com/werkspace/index.php?/topic/296-setting-a-class-for-certain-objects/page__p__2411__fromsearch__1entry2411

Link to comment
Share on other sites

I works now via a message system, but it's not really the way I want it. I would have prefered the Lua menu. This is what you do:

1.

make a new lua file called 'Icons' with the following:

require("scripts/class")
require("scripts/hooks")
icon= LoadTexture("abstract::hand.dds")
function HookIcon() 
SetBlend(1)
DrawImage(icon,GraphicsWidth()/2-25,GraphicsHeight()/2-25,50,50)  
SetBlend(0)
end 

2.

In your main code, include the previously made lua script.

require("Scripts/icons")

3.

The main loop is going to send a message to the object that is picked. in your main loop place this:

--Check for icons!
pick=CameraPick(camera,Vec3(GraphicsWidth()/2,GraphicsHeight()/2,2.0),0,0)
               if pick~=nil then
                       repeat
                               if pick.entity:GetClass()==ENTITY_MODEL then
                                       break
                               end
                               pick.entity=pick.entity.parent
                       until pick.entity==nil
                       if pick.entity~=nil then
			pick.entity:SendMessage("icon",controller,0)
                       end
	else	
		RemoveHook("Flip",HookIcon)
               end

4.

in the class script of your object (like the switch) add the following:

require("scripts/class")
local class=CreateClass(...)


function class:CreateObject(model)
local object=self.super:CreateObject(model)

function object:ReceiveMessage(message,extra)
	if message=="icon" then
			AddHook("Flip",HookIcon)
	end
end
end

 

add the code from step 4 to every object where you want an icon to appear. Once again: this is not the best way to do it, but maybe for now it will do the job.

Link to comment
Share on other sites

Am applying this, will let you know the results...

 

Update. When I add the function in step 4 (to an object's class script), I get an error message..'Attempt to index Global object a Nil value'.

 

Aggror,

If you have the time, a zipped working example would solve a lot of headache.. <_<

AMD Athlon x2 7750 2.7ghz, 6gb ddr2 ram, Galaxy9800GT 1gig ddr2 video card, Windows 7,64.

Link to comment
Share on other sites

I'am thinking of 2 ways. first you can use the filter parameter to filter objects that collided with your pick ray. refering to wiki camerapick command is like this :

CameraPick(camera, pos [, radius=0.0, collisionType=0, Filter=Null ] )

 

for example if you use it like this :

pick=CameraPick(camera,Vec3(GraphicsWidth()/2,GraphicsHeight()/2,2.0),10,nil)

 

your ray will pick an object if its collisiontype is 10. you can set your pickable objects collision type to 10.

 

my second way:

you can send a "pick" message to any object that your ray picked. in your pickable object's code, write a code to send a "pickmeup" message back to the controller and send itself (pickable object) as an extra. then your controller will get a "pickmeup" message with that pickable object as message's extra.

 

got it?

I think I did'nt explain it the way that was in my mind :)

Link to comment
Share on other sites

Thanks for the suggestions Soamp, Although there is a lot of useful information here, I've currently abandoned this idea in favour of 'proximity triggers'. If I can get my head around 'pick object' some time in the future, I'll give it another go.

AMD Athlon x2 7750 2.7ghz, 6gb ddr2 ram, Galaxy9800GT 1gig ddr2 video card, Windows 7,64.

Link to comment
Share on other sites

If you want I'll make a video instruction of what I do. but if you wan to leave it alone for now then that s okay too.

Sure Aggror, such a video must be very useful. I'm sure people would benefit from it :)

AMD Athlon x2 7750 2.7ghz, 6gb ddr2 ram, Galaxy9800GT 1gig ddr2 video card, Windows 7,64.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...