Collisions not working

Blitz3D Forums/Blitz3D Programming/Collisions not working

_PJ_(Posted 2003) [#1]
I have got sprites for bullets, yet colisions are not being registered for them.

nfortnately, my code is all broken up as included subroutines, and it's all very type-based so it's not easy to post.

In short, I have set EntityPickMode to 1 for all the objects including my bullets. I have increased the EntityRadius too.

Any ideas? I am happy to provide whatever info may be required.


DJWoodgate(Posted 2003) [#2]
Check your EntityType and Collision declarations.


jhocking(Posted 2003) [#3]
EntityPickMode is irrelevant for collisions, only for picking (eg. CameraPick, LinePick.) To setup collisions you need to assign EntityType.


_PJ_(Posted 2003) [#4]
EntityType is set correctly (I am sure)

bullets=Type 3
planets,stars and asteroids =Type 7,8,9
here is some code that I have taken from my program:


; Fire Bullet
.Bullet_Fire

;limit fire-rate
If (MilliSecs()-fire_rate)<bullet_recharge Then Return

PlaySound primarysound
fire_rate=MilliSecs()

NewBullet.Ship_Bullet = New Ship_Bullet
Newbullet\x#=EntityX#(ship)
Newbullet\y#=EntityY#(ship)
Newbullet\z#=EntityZ#(ship)
Newbullet\range=gun_range
Newbullet\bullet_sprite=CopyEntity (bullet)

EntityType newbullet\bullet_sprite,3

EntityRadius Newbullet\bullet_sprite,0.4,0.4
EntityPickMode Newbullet\bullet_sprite,3,0 
PositionEntity newbullet\bullet_sprite,newbullet\x#,newbullet\y#,newbullet\z# 
EntityType Newbullet\bullet_sprite,3 

shipp#=EntityPitch#(ship)
shipy#=EntityYaw#(ship)
shipr#=EntityRoll#(ship)
RotateEntity newbullet\bullet_sprite,shipp,shipy,shipr

EntityRadius newbullet\bullet_sprite,10,10
EntityPickMode newbullet\bullet_sprite,1

Return

;Bullet Collisions and movement

.Bullet_Coll

For ThisBullet.Ship_Bullet = Each Ship_Bullet

If thisbullet\range<1 Then FreeEntity thisbullet\bullet_sprite:Delete thisbullet:Exit

MoveEntity thisbullet\bullet_sprite,0,0,bullet_speed
Thisbullet\range=thisbullet\range-(bullet_speed) 

If CountCollisions(thisbullet\bullet_sprite)>0
Gosub Bullet_Collide
Exit
EndIf
Next

Return


.Bullet_collide

;Check collision with Stars

If EntityCollided (thisbullet\bullet_sprite,8)>0
Gosub create_new_impact
Return
EndIf

;Check collision with Planets

If EntityCollided (thisbullet\bullet_sprite,9)>0
Gosub create_new_impact
Return
EndIf


;Check collision with Roids

If EntityCollided (thisbullet\bullet_sprite,7)>0
Gosub create_new_impact
Return
EndIf

; (Create new impact)







.create_new_impact

NewImpact.Impact_explosion = New impact_explosion
Newimpact\sprite=CreateSprite()
ScaleSprite newimpact\Sprite,10,10
PaintEntity newimpact\sprite,impact_brush
newimpact\PosX=EntityX(thisbullet\bullet_sprite)
newimpact\PosY=EntityY(thisbullet\bullet_sprite)
newimpact\PosZ=EntityZ(thisbullet\bullet_sprite)
newimpact\AnimFrame=0
FreeEntity thisbullet\bullet_sprite
Delete thisbullet
Return



.impact_Anims

For Impacts.impact_explosion=Each Impact_Explosion
impacts\AnimFrame=(impacts\AnimFrame)+1
If impacts\AnimFrame>6 
FreeEntity impacts\Sprite 
Delete impacts 
Exit
EndIf
BrushTexture impact_brush,impact_texture,impacts\Animframe
PaintEntity impacts\Sprite,impact_brush
Next
Return


Global sunlight=CreateLight(2)
LightRange sunlight,10000
LightConeAngles sunlight,0,359

sectorfile=OpenFile("Libraries\Sectors\"+sector$+"\Objects.DAT")
ambient_red=ReadLine (SectorFile)
ambient_green=ReadLine (SectorFile)
ambient_blue=ReadLine (SectorFile)

LightColor sunlight,ambient_red+15,ambient_green+10,ambient_blue+5

AmbientLight ambient_red,ambient_green,ambient_blue

Faction$=ReadLine(sectorFile)

max_stars=ReadLine (SectorFile)

For sf = 1 To max_stars 

setup_star.sector_star = New sector_star 
setup_star\name$=ReadLine (SectorFile)
setup_star\class$=ReadLine (SectorFile)
setup_star\PosX=ReadLine (SectorFile)
setup_star\PosY=ReadLine (SectorFile)
setup_star\PosZ=ReadLine (SectorFile)
setup_star\ScaleX=ReadLine (SectorFile)
setup_star\ScaleZ=ReadLine (SectorFile)
setup_star\Sprite=LoadSprite("Visual\Sectors\"+sector$+"\S"+sf+".bmp")
SpriteViewMode setup_star\sprite,1
ScaleSprite setup_star\sprite,setup_star\ScaleX,setup_star\ScaleZ
PositionEntity setup_star\sprite,setup_star\posX,setup_star\PosY,setup_star\PosZ
PositionEntity sunlight,setup_star\posX,setup_star\PosY,setup_star\PosZ

NameEntity setup_star\Sprite,"Star"

EntityType setup_star\sprite,8

EntityRadius setup_star\Sprite,setup_star\ScaleX,setup_star\ScaleZ
EntityPickMode setup_star\sprite,1

 Next 



max_planets=ReadLine (SectorFile)

For pf = 1 To max_planets 

setup_planet.sector_planet = New sector_planet 
setup_planet\name$=ReadLine (SectorFile)
setup_planet\Class$=ReadLine (SectorFile)
setup_planet\PosX=ReadLine (SectorFile)
setup_planet\PosY=ReadLine (SectorFile)
setup_planet\PosZ=ReadLine (SectorFile)
setup_planet\ScaleX=ReadLine (SectorFile)
setup_planet\ScaleY=ReadLine (SectorFile)
setup_planet\ScaleZ=ReadLine (SectorFile)
ringed=ReadLine (sectorfile)

setup_planet\Texture=LoadTexture("Visual\Sectors\"+sector$+"\P"+pf+"Text.jpg",9)

setup_planet\mesh=CreateSphere(50)

PositionEntity setup_planet\mesh,setup_planet\posX,setup_planet\posY,setup_planet\posZ
EntityTexture setup_planet\mesh,setup_planet\texture
EntityShininess setup_planet\mesh,0.4
ScaleMesh setup_planet\mesh,setup_planet\scaleX,setup_planet\scaleY,setup_planet\scaleZ

If ringed=1
setup_planet\ring_sprite=LoadSprite("Visual\Sectors\"+sector$+"\P"+pf+"Ring.jpg")
SpriteViewMode setup_planet\ring_sprite,2
EntityFX setup_planet\ring_sprite,16
EntityAlpha setup_planet\ring_sprite,0.9
ScaleSprite setup_planet\ring_sprite,setup_planet\scaleX*2,setup_planet\ScaleZ*4
PositionEntity setup_planet\ring_sprite,setup_planet\posX,setup_planet\posY,setup_planet\posZ
TurnEntity setup_planet\ring_sprite,90,0,0
EntityParent setup_planet\ring_sprite,setup_planet\mesh
EndIf

atmosphere=ReadLine (SectorFile)

If atmosphere=1
setup_planet\atmos_sprite=LoadSprite("Visual\Images\Jet.jpg",11)
EntityAlpha setup_planet\atmos_sprite,0.8
EntityFX setup_planet\atmos_sprite,29
SpriteViewMode setup_planet\atmos_sprite,3
ScaleSprite setup_planet\atmos_sprite,setup_planet\scaleX*1.5,setup_planet\ScaleZ*1.5
PositionEntity setup_planet\atmos_sprite,setup_planet\posX,setup_planet\posY,setup_planet\posZ
EntityParent setup_planet\atmos_sprite,setup_planet\mesh
EndIf

NameEntity setup_planet\Mesh,"Planet"

EntityType setup_planet\mesh,9

EntityRadius setup_planet\mesh,setup_planet\ScaleX,setup_planet\ScaleZ
EntityPickMode setup_planet\mesh,1

Next


max_roids=ReadLine (SectorFile)

For rf = 1 To max_roids

setup_roid.sector_roid=New sector_roid

composition=50
setup_roid\ice=Rnd(0,70)
composition=composition-(setup_roid\ice)
setup_roid\construction_minerals=Rnd(0,composition)
composition=composition-(setup_roid\construction_minerals)
setup_roid\precious_metals=Rnd(0,composition)
composition=composition-(setup_roid\precious_metals)
setup_roid\hydrocarbons=Rnd(0,composition)
composition=composition-(setup_roid\hydrocarbons)
setup_roid\radioactives=Rnd(0,composition)
composition=composition-(setup_roid\radioactives)
setup_roid\debris=composition+30
roid_seg=Rand(3,9)
setup_roid\mesh=CreateSphere(roid_seg)
roidtxt$=Rand(1,4)
setup_roid\texture=LoadTexture("Visual\3D\roid"+roidtxt$+"txt.bmp")

setup_roid\PosX=Rnd(-50000,50000)
setup_roid\PosY=Rnd(-10000,10000)
setup_roid\PosZ=Rnd(-50000,50000)

PositionEntity setup_roid\mesh,setup_roid\posX,setup_roid\PosY,setup_roid\PosZ

scalerX#=Rnd(1,15)
scalerY#=Rnd(1,15)
scalerZ#=Rnd(1,15)
ScaleEntity setup_roid\mesh,scalerX#,scalerY#,scalerZ#

EntityType setup_roid\mesh,7

EntityRadius setup_roid\mesh,ScalerX,ScalerZ
EntityPickMode setup_roid\mesh,1

EntityTexture setup_roid\mesh,setup_roid\texture

setup_roid\name$="Asteroid "+rf+(setup_roid\ice+setup_roid\posX)

NameEntity setup_roid\mesh,"Asteroid"

Next



max_objects=ReadLine (SectorFile)

For bf=1 To max_objects

setup_object.sector_object=New sector_object

setup_object\name$=ReadLine (SectorFile)
setup_object\class$=ReadLine (SectorFile)
setup_object\scaleX=ReadLine (SectorFile)
setup_object\scaleY=ReadLine (SectorFile)
setup_object\ScaleZ=ReadLine (SectorFile)
setup_object\PosX=ReadLine (SectorFile)
setup_object\PosY=ReadLine (SectorFile)
setup_object\PosZ=ReadLine (SectorFile)
setup_object\mesh=LoadMesh("Visual\3d\"+setup_object\class$+".b3d")
setup_object\texture=LoadTexture("Visual\3d\"+setup_object\class$+"txt.bmp")
setup_object\RotX#=ReadLine (SectorFile)
setup_object\RotY#=ReadLine (SectorFile)
setup_object\RotZ#=ReadLine (SectorFile)
setup_object\Shield=ReadLine (SectorFile)
setup_object\Hull=ReadLine (SectorFile)
setup_object\Friend_or_foe=ReadLine (SectorFile)
setup_object\Dock_Here=ReadLine (SectorFile)
PositionEntity setup_object\mesh,setup_object\posX,setup_object\PosY,setup_object\PosZ
ScaleEntity setup_object\mesh,setup_object\scaleX,setup_object\scaleY,setup_object\scaleZ

NameEntity setup_object\mesh,"Construction"

EntityType setup_object\mesh,10

EntityTexture setup_object\mesh,setup_object\texture

EntityRadius setup_object\mesh,ScaleX,ScaleZ
EntityPickMode setup_object\mesh,1


 Next

CloseFile sectorfile

skyboxt=LoadTexture("Visual\Sectors\"+sector$+"\box.bmp",11)
skybox1=CreateSphere(30)
FlipMesh skybox1
EntityTexture skybox1,skyboxt
ScaleEntity skybox1,15,15,15
EntityOrder skybox1,1
EntityAlpha skybox1,0.5


For collision_value=8 To 11

For check_roid_Collisions.sector_roid=Each sector_roid

If EntityCollided (check_roid_Collisions\mesh,11) >0
FreeEntity check_roid_Collisions\mesh
Delete check_roid_Collisions
EndIf

Next

Next















jhocking(Posted 2003) [#5]
Have you used the Collisions command to establish collision relationships between types?


GNS(Posted 2003) [#6]
Do you have UpdateWorld in your main loop? Kind of a "duh!" thing but I've forgotten a few times myself.


_PJ_(Posted 2003) [#7]
Have you used the Collisions command to establish collision relationships between types?
------------------------------

In what way?
I have used EntityCollided and CountCollisions is that enough?






Do you have UpdateWorld in your main loop? Kind of a "duh!" thing but I've forgotten a few times myself.
-------------------------------------------

Yeah - Ive learnt that from previous mistakes :)


ashmantle(Posted 2003) [#8]
Collisions src_type,dest_type,method,response
Parameters
src_type - entity type to be checked for collisions.
dest_type - entity type to be collided with.

method - collision detection method.
1: ellipsoid-to-ellipsoid collisions
2: ellipsoid-to-polygon collisions
3: ellipsoid-to-box collisions

response - what the source entity does when a collision occurs.
1: stop
2: slide1 - full sliding collision
3: slide2 - prevent entities from sliding down slopes


_PJ_(Posted 2003) [#9]
Oh of course! I can't believeI haven't put that in!

Doing it now! Thanks :)


_PJ_(Posted 2003) [#10]
Does the source and destination matter?

for instance a ship can fly into a bullet the same as a bullet can be fired at and hit a ship.

The same collision will occr, but would I need to record it twice or would 2 collisions be registered then?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Okay, the collisions have made my ship stop when it hits an asteroid (which is good), but my bullet sprites still aren't registering...


ashmantle(Posted 2003) [#11]
The source will always be a ellipsoid, and the source will slide/stop on the target..


_PJ_(Posted 2003) [#12]
Okay, well all my collisions will have a response of STOP so that's no problem.

I still don't know why my sprites don't collide though...


_PJ_(Posted 2003) [#13]
Little *bump...*

Sorry, but has anyone got any ideas? the collisions just aren't recorded for sprites :(


jhocking(Posted 2003) [#14]
You seem to be applying an EntityRadius for your bullet sprites which is good but are you sure you have setup the appropriate collisions of bullet-whatever?


_PJ_(Posted 2003) [#15]
I think so: here's all the collision code relevant to my bullets:

[code]

For coll_dest=1 To 15

Collisions,3,coll_dest,1,1

Next

NewBullet.Ship_Bullet = New Ship_Bullet
Newbullet\x#=EntityX#(ship)
Newbullet\y#=EntityY#(ship)
Newbullet\z#=EntityZ#(ship)
Newbullet\range=gun_range
Newbullet\bullet_sprite=CopyEntity (bullet)

PositionEntity newbullet\bullet_sprite,newbullet\x#,newbullet\y#,newbullet\z#
EntityType Newbullet\bullet_sprite,3

shipp#=EntityPitch#(ship)
shipy#=EntityYaw#(ship)
shipr#=EntityRoll#(ship)
RotateEntity newbullet\bullet_sprite,shipp,shipy,shipr

EntityRadius newbullet\bullet_sprite,10,10
EntityPickMode newbullet\bullet_sprite,1

Return

;Bullet Collisions and movement

.Bullet_Coll

For ThisBullet.Ship_Bullet = Each Ship_Bullet

If thisbullet\range<1 Then FreeEntity thisbullet\bullet_sprite:Delete thisbullet:Exit

MoveEntity thisbullet\bullet_sprite,0,0,bullet_speed
Thisbullet\range=thisbullet\range-(bullet_speed)

If CountCollisions(thisbullet\bullet_sprite)>0
Gosub Bullet_Collide
Exit
EndIf
Next

Return


.Bullet_collide

;Check collision with Stars

If EntityCollided (thisbullet\bullet_sprite,8)>0
Gosub create_new_impact
Return
EndIf

;Check collision with Planets

If EntityCollided (thisbullet\bullet_sprite,9)>0
Gosub create_new_impact
Return
EndIf


;Check collision with Roids

If EntityCollided (thisbullet\bullet_sprite,7)>0
Gosub create_new_impact
Return
EndIf

; (Create new impact)

.create_new_impact

NewImpact.Impact_explosion = New impact_explosion
Newimpact\sprite=CopyEntity(Impact_gen_Sprite)
ScaleSprite newimpact\Sprite,10,10
PaintEntity newimpact\sprite,impact_brush
newimpact\PosX=EntityX(thisbullet\bullet_sprite)
newimpact\PosY=EntityY(thisbullet\bullet_sprite)
newimpact\PosZ=EntityZ(thisbullet\bullet_sprite)
newimpact\AnimFrame=0
FreeEntity thisbullet\bullet_sprite
Delete thisbullet
Return


big10p(Posted 2003) [#16]
Does your .bullet_collide sub ever get called?

Check by putting STOP after .bullet_collide and running in debug mode.


_PJ_(Posted 2003) [#17]
Finally sussed it.

Sorry for wasting your time, peeps!

It was complicated, because Im fairly new to Types and Collisions and I am using both here.

What happened was that I wanted to make animated sprite explosions when the bullets hit anything. I had based the animated sprites on a CreateSprite with 0,0,0 colour. So even though I textured it, it was still utterly transparent.

This was reason 1 why my collisions didnt 'look' like they worked.

Also, for some reason, I had two sets of entity radius command for my ship bullets. Although I wouldnt expect it to matter so long as the latest one was correct, but somehow it DID affect it.

Plus I needed to change the entityorder for my explosions.

The other reason why I didnt notice collisions is because they are only noticed each UpdateWorld, and reset. If you aren't checking EVERY UpdateWorld for collisions, they can be missed.