Collision help!?
Blitz3D Forums/Blitz3D Beginners Area/Collision help!?
| ||
i just started messing with bilitz3D a few days ago and i don't understand how to get collisions to work. can someone please tell me what is wrong with the code below. i just want the sphere to collide with the cubes.
Graphics3D 800,600,32,0
SeedRnd MilliSecs()
Const coll_sphere=1
Const coll_box=2
Type boxdata
Field x
Field y
Field z
Field cube
End Type
light=CreateLight()
sphere=CreateSphere()
EntityType (sphere, coll_sphere)
ScaleEntity sphere,.5,.5,.5
EntityRadius sphere,.5,.5
camera=CreateCamera()
PositionEntity camera ,0,0,-5
EntityParent camera,sphere
For x=1 To 20
box.boxdata=New boxdata
box\cube=CreateCube()
box\x=Rnd(-50,50)
box\y=Rnd(-50,50)
box\z=Rnd(-50,50)
PositionEntity box\cube ,box\x,box\y,box\z
EntityType(box\cube,coll_box)
EntityBox box\cube,-1,-1,-1,2,2,2
Next
Collisions (coll_box,coll_sphere,3,1)
While Not KeyHit(1)
If KeyDown(17) Then
TurnEntity sphere,-1,0,0
EndIf
If KeyDown(30) Then
TurnEntity sphere,0,1,0
EndIf
If KeyDown(31) Then
TurnEntity sphere,1,0,0
EndIf
If KeyDown(32) Then
TurnEntity sphere,0,-1,0
EndIf
If KeyDown(200) Then
MoveEntity sphere,0,0,1
EndIf
If KeyDown(208) Then
MoveEntity sphere,0,0,-1
EndIf
UpdateWorld
RenderWorld
For box.boxdata=Each boxdata
If EntityCollided(box\cube,coll_sphere) Then EntityColor box\cube,230,100,220
Next
Text 0,0,EntityX(sphere)
Text 0,20,EntityY(sphere)
Text 0,40,EntityZ(sphere)
Flip
Wend
edit: fixed my dumb typos edit:fixed entitybox |
| ||
| didnt run it but it looks like you are setting the sphere collision type to s... s is 0 cuz you didnt define it... maybe trey coll_sphere and when you define coll_sphere you spell it coll_shpere which is another possible problem :) hope that helps edit: lol you mispelled shpere twice the same way... is this on purpose? |
| ||
| @ nate the great lol yes spelling it shpere was a dumb mistake using the replace tool in blitz. i had it in there like 5 times. lol but anyways... it still doesn't collide when i run it. is there something wrong with the way i setup the collision code? |
| ||
| You got the definition of entitybox completely wrong, think of it like fit mesh: This: EntityType(box\cube,coll_box) EntityBox box\cube,box\x,box\y,box\z,1,1,1 SHould be EntityType(box\cube,coll_box) EntityBox box\cube, -1,-1,-1,2,2,2 It should then work fine. Basically you are defining the collision hull of the box from it's left/bottom/rear to right/top/front. During collisions, Blitz already knows what x,y,z coord the box is at as you told it by using positionentity. Additonally, you do not need to store the box, x, y, z positions as you can get them by using entityx( box\cube ) , entityy( box\cube) entityz( box\cube ) Hope this helps. Stevie |
| ||
i fixed entitybox in the above code but The collision still does not work. if i switch the collision from:Collisions (coll_box,coll_sphere,3,1) For box.boxdata=Each boxdata If EntityCollided(box\cube,coll_sphere) Then EntityColor box\cube,230,100,220 Next To: Collisions (coll_sphere,coll_box,3,1) For box.boxdata=Each boxdata If EntityCollided(sphere,coll_box) Then EntityColor box\cube,230,100,220 Next the collision works fine on the second one but it changes the entitycolor of all of the cubes. i need it to work the first way so i can change the color, delete, etc. of only the one i collided with. |
| ||
| You can only have sphere to [something] collisions set up with the collisions command so that makes sense. The second part should probably look more like this ... Collisions (coll_sphere,coll_box,3,1) For box.boxdata=Each boxdata If EntityCollided( box\cube, coll_sphere) Then EntityColor box\cube,230,100,220 Next The code below works better and faster as you do not need to iterate through each boxdata type to get the one you want. Also, use instancing - see MasterCube. Basically you create a master template, hide it and simply copy the entity and it's properties At present you don't really need to access the type associated with the cube you're colliding with. In fact you don't even need a type at all as, like I said above, you can access the coords by using the entityx\y & z commands. If you are planning on adding more properties to the 'boxdata' type and need access to them during the collision then this can be done with the object and handle commands. Give me a shout if you need this.
Graphics3D 800,600,32,0
SeedRnd MilliSecs()
Const coll_sphere=1
Const coll_box=2
Collisions coll_sphere,coll_box,3,1
Type boxdata
Field x
Field y
Field z
Field cube
End Type
light=CreateLight()
sphere=CreateSphere()
EntityType (sphere, coll_sphere)
ScaleEntity sphere,.5,.5,.5
EntityRadius sphere,.5,.5
camera=CreateCamera()
PositionEntity camera ,0,0,-5
EntityParent camera,sphere
;create a master cube which can be instanced
MasterCube = CreateCube()
EntityType MasterCube, coll_box
EntityBox MasterCube, -1,-1,-1,2,2,2
HideEntity MasterCube
For x=1 To 20
box.boxdata=New boxdata
box\cube= CopyEntity( MasterCube )
box\x=Rnd(-50,50)
box\y=Rnd(-50,50)
box\z=Rnd(-50,50)
PositionEntity box\cube ,box\x,box\y,box\z
Next
While Not KeyHit(1)
If KeyDown(17) Then
TurnEntity sphere,-1,0,0
EndIf
If KeyDown(30) Then
TurnEntity sphere,0,1,0
EndIf
If KeyDown(31) Then
TurnEntity sphere,1,0,0
EndIf
If KeyDown(32) Then
TurnEntity sphere,0,-1,0
EndIf
If KeyDown(200) Then
MoveEntity sphere,0,0,1
EndIf
If KeyDown(208) Then
MoveEntity sphere,0,0,-1
EndIf
UpdateWorld
RenderWorld
For c = 1 To CountCollisions( sphere )
Entity = EntityCollided( sphere, coll_box )
EntityColor Entity, 230,100,220
Next
Text 0,0,EntityX(sphere)
Text 0,20,EntityY(sphere)
Text 0,40,EntityZ(sphere)
Flip
Wend
Stevie |