Proposal, imutable Vectors / operator-overloading

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 :slight_smile:

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 :

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”:flushed: transition with both implementations, or not :woozy_face: !?

There was some miscelaneous discussion about that here:

Personally, immutable variables and operator overloading would be my preference.

1 Like

:smiley:

Math classes are frankensteins now…

1 Like