Hello,
I found a bug when I call armory.trait.physics.PhysicsWorld.active.rayCast();
twice.
class MyTrait extends iron.Trait {
function RayCastFromTo(source:Vec4, destination:Vec4):Vec4 {
var ret = armory.trait.physics.PhysicsWorld.active.rayCast(source, destination);
trace("RayCastFromTo", source, "->", destination, "=", ret.pos);
return ret.pos;
}
public function new() {
super();
this.notifyOnInit(function() {
var obj0 = iron.Scene.active.getChild("Cube");
var obj1 = iron.Scene.active.getChild("Cube.001");
var hit1 = this.RayCastFromTo(obj0.transform.loc, obj1.transform.loc);
var hit2 = this.RayCastFromTo(obj1.transform.loc, obj0.transform.loc);
trace("hit1", hit1);
trace("hit2", hit2);
});
}
}
the output is
arm/MyTrait.hx:12: RayCastFromTo, (-1, 1, 0, 1), ->, (1, 1, 0, 1), =, (0.9219043254852295, 1, 0, 1)
arm/MyTrait.hx:12: RayCastFromTo, (1, 1, 0, 1), ->, (-1, 1, 0, 1), =, (-0.9092615842819214, 1, 0, 1)
arm/MyTrait.hx:27: hit1, (-0.9092615842819214, 1, 0, 1)
arm/MyTrait.hx:28: hit2, (-0.9092615842819214, 1, 0, 1)
the second call change result of first!
Root cause :
- PhysicsWorld resuses instances of Vec4 :
armory/PhysicsWorld.hx at 24867658f1c926be5c67a3329a5e1ede890303b4 · armory3d/armory · GitHub - and it returns references directly
armory/PhysicsWorld.hx at 24867658f1c926be5c67a3329a5e1ede890303b4 · armory3d/armory · GitHub
the workarround is easy. I changed my code with this :
new Hit(ret.rb, ret.pos.clone(), ret.normal.clone());
It’s common in the SDK code to reuse instances : but it’s mutables and sticked references.
Radical fix is transform Vec*
classes to imutable classes and we could add operator-overloading for the code readability.
for memory usage and GC concideration, I noticed Unity3D uses imutable and operator-overloading for the vector class.
It’s a major breaking change. What do you think about imutable and operator-overloading ?
we can make a “seamless” transition with both implementations, or not
!?