having problems with entity distance & types

Blitz3D Forums/Blitz3D Beginners Area/having problems with entity distance & types

Aussie(Posted 2009) [#1]
I have created a small test with a player & objects scattered around i am using as pickups & have been having alot of problems with returning type values within functions. I think i have fixed that problem but am now having problems with the entity distance. When the player gets within the range of the the object is supposed to disapear but nothing happens.
Any help would be greatly appreciated. :)

Graphics3D 800,600,0,2

SetBuffer BackBuffer()

Global cone
Global chase_cam
Global sphere1
Global sphere2
Global pk.pickup
Global p.player


Global player_col=1
Global ground_col=2
Global score_col=3

Global playerscore=0
Global playerhealth=100

Collisions player_col,ground_col,2,2

tex=CreateTexture(32,32,8)
	ScaleTexture tex,2,2
	SetBuffer TextureBuffer (tex)
	Color 0,0,54:Rect 0,0,32,32
	Color 0,0,255:Rect 0,0,32,32,False
SetBuffer BackBuffer()

plane=CreatePlane()
	EntityTexture plane,tex
	PositionEntity plane,0,-1,0
	EntityFX plane,1
	EntityType plane,ground_col
	
cone=CreateCone()
	HideEntity cone

chase_cam=CreateCamera()
	PositionEntity chase_cam,0,1,-5
	
sphere1=CreateSphere()
	ScaleEntity sphere1,.5,.5,.5
	EntityColor sphere1,0,0,255
	EntityFX sphere1,1
	HideEntity sphere1
	
sphere2=CreateSphere()
	ScaleEntity sphere2,.5,.5,.5
	EntityColor sphere2,255,0,0
	EntityFX sphere2,1
	HideEntity sphere2

	

create_pickup()
create_player()

While Not KeyDown (1)

update_player()


UpdateWorld
RenderWorld
Text 0,0, "HEALTH:  "
Text 0,20, " SCORE:  "
Flip
Wend
End



Function update_chase_cam(camera,entity,cam_final,speed#=30)
	PointEntity camera,Cam_final
	MoveEntity camera,0,0,EntityDistance#(Cam_final,camera)/speed
	PointEntity camera,entity
	RotateEntity camera,EntityPitch#(entity),EntityYaw#(entity),EntityRoll#(entity)
End Function



Type player
	Field cam_final,player_mesh,player_v#,player_grav#,player_health,player_score
End Type

Function create_player()
	p.player = New player
		p\player_mesh=CopyEntity(cone)
		p\cam_final=CreatePivot(p\player_mesh)
		PositionEntity p\cam_final,0,1,-5
		EntityColor p\player_mesh,0,255,0
		PositionEntity p\player_mesh,0,0,0
		EntityType p\player_mesh,player_col
		p\player_score=0
		p\player_health=100
		p\player_v#=0
		p\player_grav#=-.02
End Function

Function update_player()
	For p.player = Each player
	TranslateEntity p\player_mesh,0,p\player_grav#-.05,0
	If KeyDown(200) Then p\player_v#=p\player_v#+.01
		If p\player_v# > 1 p\player_v#=1
		If Not KeyDown(200) Then p\player_v#=p\player_v#-.05
	If KeyDown(208) Then p\player_v#=p\player_v#-.01
		If p\player_v# < 0 p\player_v#=0
	If KeyDown(203) TurnEntity p\player_mesh,0,1,0
	If KeyDown(205) TurnEntity p\player_mesh,0,-1,0
	MoveEntity p\player_mesh,0,0,p\player_v#
	update_chase_cam(chase_cam,p\player_mesh,p\cam_final,10)
	For pk.pickup = Each pickup
		If EntityDistance (p\player_mesh,pk\score_mesh) < .1 Then
		
			FreeEntity pk\score_mesh
			Delete pk
		EndIf 
	Next
Next
End Function



Type pickup
	Field score_mesh,score
End Type

Function create_pickup()
	For pkup= 1 To 100
		pk.pickup=New pickup
			pk\score_mesh=CopyEntity(sphere1)
			PositionEntity pk\score_mesh,Rnd(-200,200),-.5,Rnd(-200,200)
			pk\score=5
	Next
End Function



Sledge(Posted 2009) [#2]
It's fine -- change the distance from .1 to 1, that's all.


Aussie(Posted 2009) [#3]
Thanks for that sledge.

Now have another problem. I have created some more objects & placed them around which will cause damage when hit. The problem is when i hit the objects instead of the players health going down by 5 it goes down by around 4000. I found the reason it's because of the collisions. I have tryed clearing the collisions after the player collides with the objects & then reseting them but the player falls through the floor. Is there a way to do it so the collision is only checked once at the inital collision so that the health is only reduced by 5 everytime it collides with an object. Cheers :)

Here is the new code
Graphics3D 800,600,0,2

SetBuffer BackBuffer()

Global cone
Global chase_cam
Global sphere1
Global sphere2
Global pk.pickup
Global p.player
Global en.enemy

Global player_hit=0
Global player_col=1
Global ground_col=2
Global score_col=3
Global enemy_col=4

Global playerscore=0
Global playerhealth=100

Collisions player_col,ground_col,2,2
Collisions player_col,enemy_col,2,2

tex=CreateTexture(32,32,8)
	ScaleTexture tex,2,2
	SetBuffer TextureBuffer (tex)
	Color 0,0,54:Rect 0,0,32,32
	Color 0,0,255:Rect 0,0,32,32,False
SetBuffer BackBuffer()

plane=CreatePlane()
	EntityTexture plane,tex
	PositionEntity plane,0,-1,0
	EntityFX plane,1
	EntityType plane,ground_col
	
cone=CreateCone()
	HideEntity cone

chase_cam=CreateCamera()
	PositionEntity chase_cam,0,1,-5
	
sphere1=CreateSphere()
	ScaleEntity sphere1,.5,.5,.5
	EntityColor sphere1,0,0,255
	EntityFX sphere1,1
	HideEntity sphere1
	
sphere2=CreateSphere()
	ScaleEntity sphere2,.5,.5,.5
	EntityColor sphere2,255,0,0
	EntityFX sphere2,1
	HideEntity sphere2

	

create_pickup()
create_player()
create_enemy()


While Not KeyDown (1)

update_player()


UpdateWorld
RenderWorld
Text 0,0, "HEALTH: "+ playerhealth
Text 0,20, "SCORE: "+ playerscore
Flip
Wend
End



Function update_chase_cam(camera,entity,cam_final,speed#=30)
	PointEntity camera,Cam_final
	MoveEntity camera,0,0,EntityDistance#(Cam_final,camera)/speed
	PointEntity camera,entity
	RotateEntity camera,EntityPitch#(entity),EntityYaw#(entity),EntityRoll#(entity)
End Function



Type player
	Field cam_final,player_mesh,player_v#,player_grav#,player_health,player_score
End Type

Function create_player()
	p.player = New player
		p\player_mesh=CopyEntity(cone)
		p\cam_final=CreatePivot(p\player_mesh)
		PositionEntity p\cam_final,0,1,-5
		EntityColor p\player_mesh,0,255,0
		PositionEntity p\player_mesh,0,0,0
		EntityType p\player_mesh,player_col
		p\player_score=0
		p\player_health=100
		p\player_v#=0
		p\player_grav#=-.02
End Function

Function update_player()
	For p.player = Each player
	TranslateEntity p\player_mesh,0,p\player_grav#-.05,0
	If KeyDown(200) Then p\player_v#=p\player_v#+.01
		If p\player_v# > 1 p\player_v#=1
		If Not KeyDown(200) Then p\player_v#=p\player_v#-.05
	If KeyDown(208) Then p\player_v#=p\player_v#-.01
		If p\player_v# < 0 p\player_v#=0
	If KeyDown(203) TurnEntity p\player_mesh,0,1,0
	If KeyDown(205) TurnEntity p\player_mesh,0,-1,0
	MoveEntity p\player_mesh,0,0,p\player_v#
	update_chase_cam(chase_cam,p\player_mesh,p\cam_final,10)
	For pk.pickup = Each pickup
		If EntityDistance (p\player_mesh,pk\score_mesh) < 1.5 Then
			playerscore=playerscore+pk\score
			FreeEntity pk\score_mesh
			Delete pk
		EndIf 
	For en.enemy = Each enemy
		If EntityCollided (p\player_mesh,enemy_col) Then
			playerhealth=playerhealth-en\enemy_damage
			MoveEntity p\player_mesh,0,0,-.001	
		EndIf
		Next
	Next
Next
End Function



Type pickup
	Field score_mesh,score
End Type

Function create_pickup()
	For pkup= 1 To 100
		pk.pickup=New pickup
			pk\score_mesh=CopyEntity(sphere1)
			PositionEntity pk\score_mesh,Rnd(-200,200),-.5,Rnd(-200,200)
			pk\score=5
	Next
End Function

Type enemy
	Field enemy_mesh,enemy_damage
End Type

Function create_enemy()
	For enm= 1 To 50
		en.enemy = New enemy
		en\enemy_mesh = CopyEntity(sphere2)
		EntityType en\enemy_mesh,enemy_col
		PositionEntity en\enemy_mesh,Rnd(-200,200),-.5,Rnd(-200,200)
		en\enemy_damage=5 
	Next
End Function



Stevie G(Posted 2009) [#4]
In update_player function you could move the player away from the enemy based on the collision normals to ensure it isn't continually touching :

For en.enemy = Each enemy
		If EntityCollided (p\player_mesh,enemy_col) Then
			playerhealth=playerhealth-en\enemy_damage
			
			No = CountCollisions( p\player_mesh )
			Offset# = 2.0
			Nx# = 0 : Nz# = 0
			For c = 1 To No
				Nx# = Nx# + CollisionNX( p\player_mesh, c )
				Nz# = Nz# + CollisionNZ( p\player_mesh, c )
			Next	
			Nx = Offset * Nx / Float( No )
			Nz = Offset * Nz / Float( No )
			TranslateEntity p\player_mesh, Nx, 0 , Nz

		EndIf
	Next
Next


Play around with the Offset.

Stevie


Aussie(Posted 2009) [#5]
Thanks Stevie

I have just managed to fix that problem & am now having another one.

here is the new function

Function update_player()
	For p.player = Each player
	TranslateEntity p\player_mesh,0,p\player_grav#,0
	If KeyDown(200) Then p\player_v#=p\player_v#+.02
		If p\player_v# > 1 p\player_v#=1
		If Not KeyDown(200) Then p\player_v#=p\player_v#-.05
		If p\player_v# < 0 p\player_v#=0
	If KeyDown(203) TurnEntity p\player_mesh,0,1,0
	If KeyDown(205) TurnEntity p\player_mesh,0,-1,0
	MoveEntity p\player_mesh,0,0,p\player_v#
	update_chase_cam(chase_cam,p\player_mesh,p\cam_final,10)
	For pk.pickup = Each pickup
		If EntityDistance (p\player_mesh,pk\score_mesh) < 1.5 Then
			playerscore=playerscore+pk\score
			FreeEntity pk\score_mesh
			Delete pk
		EndIf 
	For en.enemy = Each enemy
		If EntityCollided (p\player_mesh,enemy_col)
			playerhealth=playerhealth-en\enemy_damage
			ResetEntity p\player_mesh
			MoveEntity p\player_mesh,0,.5,-10
		EndIf
		Next
	Next
Next
End Function


Now after the player has collided, the player moves away from the enemy object, but for some reason after the player has been pushed away from the enemy & the up arrow is down the player won't move until the up arrow has been released & pushed down again. Arggggggg!!!