entity pick

Blitz3D Forums/Blitz3D Beginners Area/entity pick

rtk(Posted 2008) [#1]
How do you tell whether a particular entity is picked or not? I tried doing this but it doesn't work.

EntityPickMode e\shape,1


If weaponout=1
picked=CameraPick(camera,378,278)
If picked<>0 Then
makehole(hole,holes)
makespark(spark,sparks)
EndIf

If picked=1
ShowEntity ball
EntityShininess ball,1
EntityColor ball,200,0,0
ScaleEntity ball,1,1,1
PositionEntity ball,PickedX(),PickedY(),PickedZ(),True
EntityType ball,1
Animate ball,3,.2
UpdateWorld()
EndIf
EndIf


Snarkbait(Posted 2008) [#2]
try EntityPickMode e\shape,2

also, why are you giving absolute coordinates to camerapick, try using MouseX and MouseY ?


Zethrax(Posted 2008) [#3]
also, why are you giving absolute coordinates to camerapick, try using MouseX and MouseY ?


That would only apply if he's picking at the mouse pointer position. If he's picking at the position of a stationary crosshair, for example, and using a fixed viewport size, using absolute coordinates should be no problem.


Snarkbait(Posted 2008) [#4]
true... however, if no entity is at those coords the pick won't work.

Also, just noticed, this is wrong:

if picked = 1

camerapick returns an object handle, or 0


Stevie G(Posted 2008) [#5]
Yes, use

If Picked or If Picked > 0

Why are you calling update world conditionally? This should be in your main render loop.


rtk(Posted 2008) [#6]
The variable e\shape is assigned to a sphere (eventually to be replaced with a model) in a separate function. I tried setting its pickmode to -2 and the environments pickmode to 2 like this:

Function
e\shape=createsphere()
EntityPickMode e\shape,-2
EntityPickMode city1,2
blah blah blah
End Function

Function
If weaponout=1
picked=CameraPick(camera,378,278)
If picked>0 Then
makehole(hole,holes)
makespark(spark,sparks)
EndIf

If picked<0
ShowEntity ball
EntityShininess ball,1
EntityColor ball,200,0,0
ScaleEntity ball,1,1,1
PositionEntity ball,PickedX(),PickedY(),PickedZ(),True
EntityType ball,1
Animate ball,3,.2
EndIf
EndIf
End Function

I still get no response when I shoot the sphere.


Snarkbait(Posted 2008) [#7]
you want picked > 0

oops you have that... not sure.

post entire code


Stevie G(Posted 2008) [#8]
Picked can never be < 0 as it will either return 0 or an integer reference to the picked mesh.

You can't set the pickmode to -2 as this is not a recognised option. The mode must be > 0 for this to work.

Post some working code and someone will fix it for you.

Stevie


rtk(Posted 2008) [#9]
This code works, I need to generate the "blood sphere" at the location of the picked sphere but I can only seem to pick the environment.

Graphics3D 800,600,32,1
Dither True
AntiAlias True
BackBuffer()


Type enemy
Field X
Field Y
Field Z
Field shape
End Type

Global pistolselected=0
Global Gravity# = -.5
Global WalkSpeed#=.5
Global FlyMode=0
Global weaponout=0
Global pistolammo=15
Global CamPivot = CreatePivot( )
Global Camera = CreateCamera( )
PositionEntity CamPivot,1010,50,1010
EntityType CamPivot, 1
EntityRadius CamPivot, 3
EntityType Camera, 1
EntityRadius Camera, 2
Global pistol=CreateCylinder()
EntityParent pistol,camera
PositionEntity pistol,EntityX(Camera)+1.5,EntityY(Camera)-2,EntityZ(Camera)+2
RotateEntity pistol,100,0,0
HideEntity pistol
Global muzzleglow2=CreateLight(pistol)
LightRange muzzleglow2,.0005
HideEntity muzzleglow2
LightColor muzzleglow2,255,255,0

Global t=0

Collisions 1,2,2,3

Function Loadlevel1()

light=CreateLight(2)
PositionEntity light,600,100,600
RotateEntity light,90,0,0
LightColor light,100,255,255

terrain=CreateTerrain(32)
PositionEntity terrain,0,-.02,0
ScaleEntity terrain,50,20,50
EntityType terrain,2
EntityPickMode terrain,2

city1=CreateCube()
PositionEntity city1,1000,1.5,1000
EntityType city1,2
EntityPickMode city1,2
ScaleEntity city1,50,20,50

enemies=0

SeedRnd 4

Repeat
e.enemy=New enemy
e\x=Rnd(1,1000)
e\y=4
e\z=Rnd(1,1000)
e\shape=CreateSphere()
PositionEntity e\shape,e\x,e\y,e\z
ScaleEntity e\shape,4,4,4
EntityType e\shape,3
EntityPickMode e\shape,3
enemies=enemies+1
Until enemies=10

CameraRange Camera,.2,1000
CameraFogMode Camera,1
CameraFogRange Camera,.2,999
CameraFogColor Camera,19,164,149
CameraClsColor Camera,0,255,255

While Not KeyHit(1)
controls()
Wend

Return
End Function

Function controls()

Color 255,0,0

If pistolselected=1
WritePixel 355,255,0
EndIf

If KeyDown( 42 ) =1
WalkSpeed# = 1.5
Else
WalkSpeed# = .5
EndIf

If KeyHit(2)
ShowEntity pistol
pistolselected=1
weaponout=1
End If

If FlyMode=0
If KeyDown(17) =1 Then MoveEntity CamPivot, 0, 0, WalkSpeed#
If KeyDown(31) =1 Then MoveEntity CamPivot, 0, 0, 0-WalkSpeed#
PositionEntity Camera, EntityX#( CamPivot ), EntityY#( CamPivot )+8, EntityZ#( CamPivot )
EndIf

If KeyDown(30) =1 Then MoveEntity CamPivot, -1, 0, 0
If KeyDown(32) =1 Then MoveEntity CamPivot, 1, 0, 0

If EntityCollided(CamPivot,2)
Gravity#=0
Else
Gravity#=-.5
EndIf

TranslateEntity CamPivot, 0, Gravity#, 0

TurnEntity CamPivot, 0, 0 -MouseXSpeed(), 0

TurnEntity Camera, MouseYSpeed(), 0, 0

RotateEntity CamPivot, EntityPitch#( CamPivot ), EntityYaw#( CamPivot ), 0

MoveMouse GraphicsWidth()/2, GraphicsHeight()/2

RotateEntity Camera, EntityPitch#( Camera ), EntityYaw#( CamPivot ), 0

If MouseHit(1)And pistolselected=1 And pistolammo>0
createbullet()
pistolammo=pistolammo-1
EndIf

If t=1
Text 0,20,"Enviroment picked"
EndIf

Text 0,40,"Press 1 to pull out pistol, aim with the dot"

Flip

UpdateWorld()

RenderWorld

HideEntity muzzleglow2

Return
ClearCollisions
End Function

Function createbullet( )

If pistolselected=1
ShowEntity muzzleglow2
EndIf

If weaponout=1
picked=CameraPick(camera,378,278)
If picked>1 Then
t=1
EndIf

If picked<2
blood=CreateSphere()
ShowEntity blood
EntityShininess blood,1
EntityColor blood,200,0,0
ScaleEntity blood,1,1,1
PositionEntity blood,PickedX(),PickedY(),PickedZ()
EntityType blood,1
EndIf

EndIf
End Function


cash(Posted 2008) [#10]
not sure exactly what you are trying to achieve as I havent read the entire post, however.

to make an entity NON pickable set entitypickmode to 0
else use entitypickmode,2 for most situations.

example

create a level called level1 and an enemy called enemy.

do your camerapick then

if pick=something non pickable goto endofpick

if pick-level1 then

do something

if pick=enemy
then do something else

.endofpick


to show a sprite where you picked like a bullethole or similar, use something like below to position the sprite
obviously the "whatever" will be referenced as a type for the sprite loaded earlier. Hope this makes some sense.

positionentity whatever,PickedX(),PickedY(),PickedZ()

aligntovector whatever,-PickedNX(),-PickedNY(),-PickedNZ(),3


rtk(Posted 2008) [#11]
That's pretty much what I have already, only I don't use a goto. The problem is to tell the difference between the environment and the enemy. Their pickmodes are different, but it always seems to choose the environment. The code works (its only a mock up of first level, the cylinder used to be an animated arm with a handgun, for example) you can run it to see what I mean about the pickmode. The real game also has bullet holes and sparks.


Stevie G(Posted 2008) [#12]
You code is a mess so I've tidied it up a bit. You should really be using indentation to make it more readable. Anyway, this is what you want ( I think ) ..

Graphics3D 800,600,32,1
Dither True
AntiAlias True
BackBuffer()


Type enemy
	Field ID
	Field X
	Field Y
	Field Z
	Field shape 
End Type

Global pistolselected=0
Global Gravity# = -.5 
Global WalkSpeed#=.5 
Global FlyMode=0
Global weaponout=0
Global pistolammo=15
Global CamPivot = CreatePivot( ) 
Global Camera = CreateCamera( )
PositionEntity CamPivot,1010,50,1010 
EntityType CamPivot, 1
EntityRadius CamPivot, 3
EntityType Camera, 1
EntityRadius Camera, 2
Global pistol=CreateCylinder()
EntityParent pistol,camera
PositionEntity pistol,EntityX(Camera)+1.5,EntityY(Camera)-2,EntityZ(Camera)+2 
RotateEntity pistol,100,0,0
HideEntity pistol
Global muzzleglow2=CreateLight(pistol)
LightRange muzzleglow2,.0005 
HideEntity muzzleglow2 
LightColor muzzleglow2,255,255,0

Global city1, terrain, t$

Collisions 1,2,2,3


LoadLevel1()

While Not KeyDown(1)

	Controls()
	
Wend


;=====================================================================================
;=====================================================================================
;=====================================================================================
;=====================================================================================
;=====================================================================================
;=====================================================================================

Function Loadlevel1()

	light=CreateLight(2)
	PositionEntity light,600,100,600
	RotateEntity light,90,0,0 
	LightColor light,100,255,255
	
	terrain=CreateTerrain(32)
	PositionEntity terrain,0,-.02,0
	ScaleEntity terrain,50,20,50
	EntityType terrain,2
	EntityPickMode terrain,2
	
	city1=CreateCube()
	PositionEntity city1,1000,1.5,1000
	EntityType city1,2
	EntityPickMode city1,2
	ScaleEntity city1,50,20,50
	
	SeedRnd 4
	For enemies = 1 To 10
		e.enemy=New enemy
		e\ID = enemies
		e\x=Rnd(1,1000)
		e\y=4
		e\z=Rnd(1,1000)
		e\shape=CreateSphere()
		PositionEntity e\shape,e\x,e\y,e\z
		ScaleEntity e\shape,4,4,4
		EntityType e\shape,3
		EntityRadius e\shape, 4
		EntityPickMode e\shape,1
		NameEntity e\shape, Handle(e)
	Next
	
	CameraRange Camera,.2,1000
	CameraFogMode Camera,1
	CameraFogRange Camera,.2,999
	CameraFogColor Camera,19,164,149
	CameraClsColor Camera,0,255,255
	
End Function

;=====================================================================================
;=====================================================================================
;=====================================================================================

Function controls()
	
	If KeyDown( 42 ) =1 
		WalkSpeed# = 1.5
	Else
		WalkSpeed# = .5
	EndIf
	
	If KeyHit(2)
		ShowEntity pistol
		pistolselected=1
		weaponout=1 
	End If
	
	If FlyMode=0
		If KeyDown(17) =1 Then MoveEntity CamPivot, 0, 0, WalkSpeed# 
		If KeyDown(31) =1 Then MoveEntity CamPivot, 0, 0, 0-WalkSpeed#
		PositionEntity Camera, EntityX#( CamPivot ), EntityY#( CamPivot )+8, EntityZ#( CamPivot )
	EndIf
	
	If KeyDown(30) =1 Then MoveEntity CamPivot, -1, 0, 0
	If KeyDown(32) =1 Then MoveEntity CamPivot, 1, 0, 0 
	
	If EntityCollided(CamPivot,2)
		Gravity#=0
	Else
		Gravity#=-.5 
	EndIf
	
	TranslateEntity CamPivot, 0, Gravity#, 0
	TurnEntity CamPivot, 0, 0 -MouseXSpeed(), 0 
	TurnEntity Camera, MouseYSpeed(), 0, 0
	RotateEntity CamPivot, EntityPitch#( CamPivot ), EntityYaw#( CamPivot ), 0
	MoveMouse GraphicsWidth()/2, GraphicsHeight()/2
	RotateEntity Camera, EntityPitch#( Camera ), EntityYaw#( CamPivot ), 0 
	
	If MouseHit(1) And pistolselected = 1 And pistolammo > 0
		createbullet()
		pistolammo=pistolammo-1
	EndIf

	UpdateWorld()
	RenderWorld()
	
	Color 255,0,255
	Text 0,20,"Picked : "+t$ 
	Text 0,40,"Press 1 to pull out pistol, aim with the dot" 
	
	;dot
	Color 255,0,0
	Rect 378-2,278-2,5,5,0
	
	Flip
	
	HideEntity muzzleglow2 
	
End Function

;=====================================================================================
;=====================================================================================
;=====================================================================================

Function createbullet( )

	ShowEntity muzzleglow2
	
	If weaponout = 1
	
		picked = CameraPick(camera,378,278)
		
		If Picked > 0 

			Select Picked
	
				Case City1
				
					t$ = "City1"
				
				Case Terrain
				
					t$ = "Terrain"
				
				Default
				
					t$ = "Enemy "
	
					blood=CreateSphere()
					ShowEntity blood
					EntityShininess blood,1
					EntityColor blood,200,0,0
					ScaleEntity blood,1,1,1
					PositionEntity blood,PickedX(),PickedY(),PickedZ()
					
					;get picked enemy type
					e.enemy = Object.enemy( EntityName( Picked ) )
					t$ = t$ + e\ID
					
			End Select
			
		Else
		
			t$ = "Nothing"
			
		EndIf
					
	EndIf

End Function 



rtk(Posted 2008) [#13]
Thanks. (Sorry about the indenting)