Ellipse - Box collision woes
Blitz3D Forums/Blitz3D Programming/Ellipse - Box collision woes
| ||
Ever since I started with Blitz3D, I've always had problems with ball->box collisions. When I completed my last project, I had to do a second test when a collision happened, to ensure the 2 objects were close enough for a collision to be possible. Basically, it's doing my head in, and I can't see any errors in any of my code but I'm hoping that there's something I don't know about how collisions work, so if anyone can take a look at the method I use to set these up, I'd appreciate it. 1. Firstly, I set up master entities: masterbullet=LoadMD2("models\bullet.md2") HideEntity masterbullet ScaleEntity masterbullet,0.3,0.3,0.3 PositionEntity masterbullet,0,0,0 EntityType masterbullet,col_bullet EntityRadius masterbullet,1 masterentities(c_master_wall) = LoadMD2("models\brickwall.md2") brickwalltex = LoadTexture("models\brickwall.bmp") EntityTexture masterentities(c_master_wall),brickwalltex ScaleEntity masterentities(c_master_wall),0.13,0.13,0.13 PositionEntity masterentities(c_master_wall),0,0,0 HideEntity masterentities(c_master_wall) EntityType masterentities(c_master_wall),col_wall EntityBox masterentities(c_master_wall),-2.5,0,-2.5,5,10,5 2. Then, I create the collision, so that I'll know if a bullet hits the wall: Collisions col_bullet,col_wall,3,1 3. Next I create my level from the master entities (in this simple case, the walls). I use a loop with CopyEntity, position the wall, unhide the entity to create the full level. 4. In the main loop, if I press fire, I use CopyEntity from the bullet master entity, position the new bullet, point it in the right direction then unhide it. And that's it. Collisions do occur when the bullets hit the walls, so it does work to some degree. However, I find that extra collisions are often generated with walls which are far from the bullet. The walls being detected as hit are actually in exactly the opposite direction to which the bullet is moving. As I say, this is a problem that's always plagued me, so it's not like I've mistyped something - it must be either something to do with my methods or there's a bug with Blitz, but the fact that no one else seems to be having the same problem makes this seem unlikely. Can anyone shed any light? |
| ||
This may help .... http://www.blitzbasic.com/Community/posts.php?topic=53187#594579 Stevie |
| ||
Thanks, but my problem is not the same. My collisions do occur, but I get extra collisions occurring too when the colliding entities are far apart. The objects are all simple too - no merging of entities, just a simple boxy MD2 being hit by a relatively spherical bullet. |
| ||
Make sure you do a ResetEntity on the bullet entity copy after you move it to its initial position (the firing point of a gun, for example) but before the next UpdateWorld command. Otherwise it will be stopped by the first collision enabled entity it collides with. Note that in your code above you shouldn't need to position your source entities at 0, 0, 0 as they will be created at this position. |
| ||
ResetEntity has no effect. The collisions do not happen as soon as the bullet is created - they happen shortly after the bullet is in motion. I think the easiest way of demonstrating this is by creating the smallest possible program I can with the problem intact. My current program is much too large for quick digestion and uses some paid-for libraries anyway. I'll try to do this tonight. Thanks for taking the time to reply, though. |
| ||
Ok, here's a short program which needs no external models - just copy and paste into Blitz. There's a cone in the middle which you can rotate with cursor left and right. Press left ctrl to fire a bullet. Firing at the wall above does what it's supposed to and destroys all the wall blocks in its path. However, if you fire a ton of bullets downwards instead (just spread em out), you'll find that collisions occur with the blocks at the top, even though they're well out of range of the bullets. If anyone can explain why this happens, I'll award them the Bankie Gold Star Award. Graphics3D 800,600,0,2 col_bullet=1 col_block=2 Type t_bullet Field id% End Type Type t_block Field id% End Type SetBuffer BackBuffer() Global camera=CreateCamera() CameraViewport camera,0,0,800,600 CameraRange camera,1,1000 PositionEntity camera,0,30,-10 RotateEntity camera,70,0,0 light=CreateLight() AmbientLight(150,150,150) player=CreateCone() RotateMesh player,90,0,0 masterbullet=CreateSphere() ScaleEntity masterbullet,0.5,0.5,0.5 EntityType masterbullet,col_bullet EntityRadius masterbullet,1 HideEntity masterbullet masterblock=CreateCube() ScaleEntity masterblock,1,0.5,0.5 ; block is now 2x1x1 EntityType masterblock,col_block EntityBox masterblock,-1,-0.5,-0.5,2,10,1 HideEntity masterblock Collisions col_bullet,col_block,3,1 For x=-30 To 30 Step 3 ; create wall For z=10 To 20 Step 2 block.t_block=New t_block block.t_block\id=CopyEntity(masterblock) PositionEntity block.t_block\id,x,0,z ShowEntity block.t_block\id ResetEntity block.t_block\id Next Next NewTime = MilliSecs() OldTime = NewTime While Not KeyHit(1) NewTime = MilliSecs() delta# = Float (NewTime - OldTime) / 1000.0 OldTime = NewTime ; process input TurnEntity player,0,((KeyDown(203) - KeyDown(205)) * delta * 50),0 If KeyHit(29) Then bullet.t_bullet=New t_bullet bullet.t_bullet\id=CopyEntity(masterbullet) PositionEntity bullet.t_bullet\id,0,0,0 RotateEntity bullet.t_bullet\id,0,EntityYaw(player),0 ShowEntity bullet.t_bullet\id ResetEntity bullet.t_bullet\id End If ; animations For bullet.t_bullet = Each t_bullet MoveEntity bullet.t_bullet\id,0,0,delta * 10 collidedblock=EntityCollided(bullet.t_bullet\id,col_block) If collidedblock Then For block.t_block = Each t_block If collidedblock=block.t_block\id Then FreeEntity block.t_block\id Delete block.t_block End If Next End If Next UpdateWorld RenderWorld Flip Wend End |
| ||
I tried repeating your problem, but it works like it is supposed to. Actually it is pretty fun game allready. :) Did you installed the latest update ? Have you tried upgrading/downgrading? And have you tried running it on another pc ? Maybe it varies on different systems. One thing I could imagine is that the bullets fly so far down, that their coords wrap and they end up above. If that is the case, you could limit the distance that a bullet may fly from the ship, before it gets deleted. Another cause could be the delta timing maybe. Try setting it to a fixed value, ie delta# = 0.001, as a test. And you could also make an even simpeler example, without types, and just 2 objects, to completely isolate the problem. Maybe it could generate the error by default |
| ||
Works fine here. Probably not the issue but, if you've had your machine on for a couple of weeks without a restart, millisecs() starts returning negative numbers. Stick an abs() on the front of your delta formula and see if that does anything. Stevie |
| ||
Thanks for the replies, particularly b32s as one of the suggestions worked. It certainly had nothing to do with the bullets flying so far down, as it would take a loooong time before coords would wrap. Delta was fine too - I tried setting that to a fixed value. HOWEVER, I was on V1.97. Upgrading to 1.98 didn't fix it, but downgrading to 1.85 did. Now I need to find the latest version that doesn't have the problem. I'd be interested in knowing what version of Blitz you guys are using as well as anyone else who's willing to give the code a quick test drive. Thanks again! |
| ||
Line 37 should be EntityBox masterblock,-1,-0.5,-0.5,2,1,1 not EntityBox masterblock,-1,-0.5,-0.5,2,10,1. Changing it doesn't fix the problem, though. I couldn't see any issues with your code that would cause this problem, so I'm thinking its a Blitz bug with box collsions. Best I can suggest is that you post a bug report and switch to using polygonal collisions (which worked fine when I used them with your code). EDIT: I'm using version 1.98. The problem occurred in this version, as you mentioned above. |
| ||
I'm glad it works, however now you still don't know what it is .. :( If you could make a version of the program with a very clear example the problem, you could post it in the bugs report, with a description of the system you are running. I have the latest version installed |
| ||
I'm still on 1.96 and as I mentioned it works just fine. |
| ||
Hello all. I've deduced that the collision bug is occurring on B3D versions 1.88 and after. This, however, seems to be contrary to what a couple of you are saying. On b32's advice, I've created a more concise program which doesn't have timing code, contains just one bullet and one block, and the error occurs 100% of the time on my PCs. One peculiar thing I noticed is that if I change the speed of the bullet, the collision occurs when the bullet is in a different position. However, it DOES seem to occur all the time, and always pretty quickly unless you slow down the bullet speed dramatically. I would be grateful to anyone who can give this a quick test to confirm that the block at the top disappears from a non-existent collision (collision should occur in less than a couple of seconds from starting unless your PC is incredibly slow). I'd especially be interested in b32 and Stevie G's results as you guys didn't see the bug. Graphics3D 800,600,0,2 col_bullet=1 col_block=2 SetBuffer BackBuffer() Global camera=CreateCamera() CameraViewport camera,0,0,800,600 CameraRange camera,1,1000 PositionEntity camera,0,30,-10 RotateEntity camera,70,0,0 light=CreateLight() AmbientLight(150,150,150) bullet=CreateSphere() ScaleEntity bullet,0.5,0.5,0.5 EntityType bullet,col_bullet EntityRadius bullet,1 HideEntity bullet block=CreateCube() ScaleEntity block,1,0.5,0.5 ; block is now 2x1x1 EntityType block,col_block EntityBox block,-1,-0.5,-0.5,2,1,1 HideEntity block PositionEntity block,12,0,12 ShowEntity block ResetEntity block ShowEntity bullet ResetEntity bullet RotateEntity bullet,0,134.8,0 Collisions col_bullet,col_block,3,1 While Not KeyHit(1) MoveEntity bullet,0,0,0.25 collidedblock=EntityCollided(bullet,col_block) If collidedblock Then If collidedblock=block Then FreeEntity block End If End If UpdateWorld RenderWorld Flip Wend End |
| ||
Bug happens here as well, with 1.96 and 1.98. Seems to occur when the sphere is on the same axis as an edge of the box. It should be fixed. Ellipsoid to box collisions were always a bit flaky as elipsoids were primarily introduced for use in polygon collisions, but this seems to pretty much invalidate using sphere to box collisions as well. Strange it has not been noticed before. Ellipsoid collisions were added back at 1.81 when one might assume the problem was introduced, however I note from versions.txt that some additional collision work to tighten things up was done at 1.88 which coincides with your findings. It would be really hideous if it works ok on some systems and not on others. |
| ||
I see the error here on v1.96. Looks like box collisions are borked! |