Pick more than one object at once?

Blitz3D Forums/Blitz3D Beginners Area/Pick more than one object at once?

Guy Fawkes(Posted 2012) [#1]
How can you pick more than 2 or 3 objects at once, highlight all of them, and control their positions, all with one key?


Thanks for any help! :)


Ross C(Posted 2012) [#2]
You can't pick multiple entities with the one key. You'll need to track the mouse location. Plus, how can you control their positions with one key? You'll need to move them, which requires at least four keys.

For selecting multiple entities, you need to store each entity (preferably in a type collection) and add an entity to this collection. Then when moving, you simply loop through each entity in the collection.

Type selected
   Field mesh
End Type


A very simple type collection. Once you've picked an entity, see if the PickedEntity() matches any of the entities stored in the type collection.

local matches_selected = 0
for s.selected = each selected
  if PickedEntity() = s\mesh then
      matches_selected = 1
  end if
next
if matches_selected = 0 then
   s.selected = new selected
      s\mesh = PickedEntity()
end if


Now any time you want to move all your selected entities, loop through the type objects and move each of them.


Matty(Posted 2012) [#3]
When a mesh or entity is picked - set a flag such as in a type to indicate that that mesh or entity is picked. When rendering change the blend mode to 3 (additive) for those entities which are 'selected' (in your type list), (remember to change them back when they become 'unselected'), when moving them - move each entity in the selected list.

Very easy.

Work it out from my description...


Guy Fawkes(Posted 2012) [#4]
Ok, I'm a little confused... I can't seem to get it to work, although, I've ALMOST got it working.... Here's what I've got so far:





Last edited 2012


Kiyoshi(Posted 2012) [#5]
Here Thundros. Is this what you're trying to do?




Guy Fawkes(Posted 2012) [#6]
More like this:





ONLY 2 problems I'm having now, are when I click on an object for the 2nd time, picked is still equal to that object's ID. and the 2nd problem i'm having, is when there are 2 objects that are on the EXACT same Z-position, meaning there is an object in front of another object, it selects both of them at the same time. I only want it to do that if i click more than ONE object, not when I click ONLY 1 object. :)



Thanks, Kiyoshi! :)

Last edited 2012


Kiyoshi(Posted 2012) [#7]
Okay, about your first problem. From looking at your code, I'd assume that your reason for wanting the value of Picked to equal 0, is because you can't move the camera unless Picked=0? Am I correct?
As for your second problem, I switched up the code a bit, to place all the cubes in a straight line on the Z-axis, and I was unable to recreate your problem. When I clicked on the cube in front, all the cubes behind it were unaffected.
Always glad to help.


Guy Fawkes(Posted 2012) [#8]
Yes, u are correct. :) Also, for the 2nd problem, JUST incase, is there a way to select the objects ONLY WHEN the camera can see BOTH objects on the screen at once? :)


Thanks, Kyoshi! :)


Kiyoshi(Posted 2012) [#9]
Alright. To fix your first problem, it would be much easier to replace Picked in this code:
While Not KeyDown(1)
	
	
	If MouseHit(1) Then inpickmode = PickAll(camera) ;Run the function for selecting an object
	If picked<>0 Then inpickmode = True : Else inpickmode = False;THIS LINE GETS REPLACED


with ObjsSelected, like this:
While Not KeyDown(1)
	
	
	If MouseHit(1) Then inpickmode = PickAll(camera) ;Run the function for selecting an object
	If ObjsSelected<>0 Then inpickmode = True : Else inpickmode = False;THIS LINE IS BETTER :)


That way, if no objects are selected, the camera can move. But if any are selected, the camera will be prevented from moving, and you can move your objects. Much easier than making Picked=0.


Also, if you want a failsafe to make sure that you'll only select an object thats visible, replace this:
If picked=c\mesh ;If the object is clicked, change the field "c\selected" to 1.


with this:
If picked=c\mesh And EntityVisible(camera,c\mesh);If the object is clicked, change the field "c\selected" to 1.


You already have the picked mode on your objects set to obscure entities, so that piece of code will make it so only objects seen by the camera can be selected.


Guy Fawkes(Posted 2012) [#10]
Thanks, Kiyoshi :) It still has the problem where if u click on an object for a second time, then picked is not reset to 0... and it still lets the camera move around if that happens.



Here's the code:



Thanks, Kiyoshi! :)


Kiyoshi(Posted 2012) [#11]
I understand. Besides not letting the camera move, was there another reason you needed Picked=0? If not, it should be fine, even if picked does not equal 0 at all times.


Guy Fawkes(Posted 2012) [#12]
Well, I would like to make sure picked = 0 so that there's not a future problem :)


Kiyoshi(Posted 2012) [#13]
Add this line.
While Not KeyDown(1)
 If ObjsSelected=0 Picked=0 
	
	If MouseHit(1) Then inpickmode = PickAll(camera) ;Run the function for selecting an object
	If ObjsSelected<>0 Then inpickmode = True : Else inpickmode = False;THIS LINE IS BETTER :)



Guy Fawkes(Posted 2012) [#14]
OMG! THAT DID IT! THANK YOU SO MUCH, KIYOSHI! :D FOR EVERYTHING! =D


Here's the code:






Thanks again, Kiyoshi! :)


Kiyoshi(Posted 2012) [#15]
You're very welcome. I'm glad I could help you out. :)


Guy Fawkes(Posted 2012) [#16]
:)


Guy Fawkes(Posted 2012) [#17]
For those of u who are a bit picky on speed like me, here's an optimization of the above version:







I did:







As opposed to:







In Function:







I also for Y-Position sake, have removed the line that positions the "ground" at -2, so that each object has the same Y Position! :)



Enjoy everyone! And once again, thanks a TON, Kiyoshi! :)

Last edited 2012


Matty(Posted 2012) [#18]
That little "optimisation" won't make the slightest bit of difference....


Kryzon(Posted 2012) [#19]
Don't use [codebox] for formatting a single line of code... use [code] [/ code]