Collisions WTF?!

BlitzMax Forums/MiniB3D Module/Collisions WTF?!

Sledge(Posted 2009) [#1]
I've probably missed something obvious here but it's got me scratching my head at the moment. Here's some code to illustrate the issue I'm having:

format_codebox('
SuperStrict
Import sidesign.miniB3D
AppTitle$ = "Collisions WTF?!"

Graphics3D 800,600,32,2,60
Local cam:TCamera=CreateCamera()
PositionEntity cam,0,6,0
RotateEntity cam,90,0,0

Const TYPE_ARENA:Int = 1
Local arena:TMesh = CreateCube()
EntityColor arena,200,100,100
FlipMesh arena
UpdateNormals arena
EntityType arena,TYPE_ARENA

Const Type_SPHERE:Int = 2
Local sphere:TEntity = CreateSphere()
ScaleEntity sphere,.1,.1,.1
EntityRadius sphere,.1
EntityColor sphere,100,100,200
PositionEntity sphere,0.5,0,0
EntityType sphere,TYPE_SPHERE
Local spherexVel:Float = 0.01

Local light:TLight = CreateLight(1)
PositionEntity light,40,50,10
PointEntity light,arena

Collisions TYPE_SPHERE,TYPE_ARENA,2,2
Moveentity sphere,0,0,0
UpdateWorld()

Print
Print "Results"
Print "======="

While Not KeyHit(KEY_ESCAPE)

MoveEntity sphere,0,0,spherexVel
UpdateWorld()

For Local currentCollision:Int = 1 To CountCollisions(sphere)
Print "Difference: "+Abs( CollisionZ(sphere,currentCollision)-EntityZ(sphere,True) )
Print "Collision X: "+CollisionX(sphere,currentCollision)
Print
If Abs( CollisionZ(sphere,currentCollision)-EntityZ(sphere,True) )>.5
spherexVel=-spherexVel
EndIf
Next

If KeyDown(KEY_DOWN) MoveEntity cam, 0, 0,-0.1
If KeyDown(KEY_UP) MoveEntity cam, 0, 0, 0.1
If KeyDown(KEY_RIGHT) TurnEntity cam, 0,-5, 0
If KeyDown(KEY_LEFT) TurnEntity cam, 0, 5, 0
If KeyDown(KEY_Z) TurnEntity cam,-1, 0, 0
If KeyDown(KEY_A) TurnEntity cam, 1, 0, 0

RenderWorld()
Flip
Wend
')

Now if you keep your eye on the output, you'll notice that the fist set of results is 'off': It reports CollisionX wrongly as 0.0 when it is 0.5 and, similarly, the difference between EntityZ and CollisionZ is also different to all subsequent results. I'm prototyping a project where getting these values wrong even once is fatal, so this is a bit of a spanner in the works. Anyone want to point out what idiot thing I'm doing wrong?

format_code('Results
=======
Difference: 0.899999440
Collision X: 0.000000000

Difference: 1.89999938
Collision X: 0.500000000

Difference: 1.89999938
Collision X: 0.500000000

Difference: 1.89999938
Collision X: 0.500000000

Difference: 1.89999938
Collision X: 0.500000000...
')


D4NM4N(Posted 2009) [#2]
Im not sure how the collision works in mb3d, but if you are inside a flipped mesh, is that what is causing a collision? the fact you are in the middlle of it.

try flipping it the right way and colliding against the outside :/


Sledge(Posted 2009) [#3]
The flipped cube is there as a stand-in for a .b3d mesh which, in my actual project, exhibits the same sort of anomalies.


simonh(Posted 2009) [#4]
Yes, it's a bug, well spotted.

To fix, in TCollision.bmx, change line 197:

format_code('If C_CollisionResponse(c_col_info,c_coll,response)=False Then Exit')
To this:

format_code('If x=False Then Exit')
Now add the following line directly after line 164:

format_code('Local x=C_CollisionResponse(c_col_info,c_coll,response)')
The bug was due to the C_CollisionResponse function being called too late, so that the CollisionX/Y/Z etc functions were always one call behind.


Sledge(Posted 2009) [#5]
Sweet! Thanks for the quick fix -- I guess iMiniB3D is identical and can be fixed in the same way, syntax aside?


simonh(Posted 2009) [#6]
Yes, that's correct.


D4NM4N(Posted 2009) [#7]
lol open source is awesome :D


jtfrench(Posted 2011) [#8]
I don't completely understand what transpired in this past conversation, but it makes me happy to see how awesome the community out here can put together –— and support —— some awesome stuff.

Props to open source , simonh & sledge