Line data Source code
1 : function MotionBallScripted() {} 2 : 3 0 : MotionBallScripted.prototype.Schema = 4 : "<a:component type='test'/><empty/>"; 5 : 6 0 : MotionBallScripted.prototype.Init = function() { 7 0 : this.speedX = 0; 8 0 : this.speedZ = 0; 9 : }; 10 : 11 0 : MotionBallScripted.prototype.OnUpdate = function(msg) { 12 0 : var dt = msg.turnLength; 13 : 14 0 : var cmpPos = Engine.QueryInterface(this.entity, IID_Position); 15 0 : var cmpTerrain = Engine.QueryInterface(SYSTEM_ENTITY, IID_Terrain); 16 : 17 0 : var pos = cmpPos.GetPosition(); 18 : 19 0 : var normal = cmpTerrain.CalcNormal(pos.x, pos.z); 20 : 21 0 : var g = 10; 22 0 : var forceX = normal.x * g; 23 0 : var forceZ = normal.z * g; 24 : 25 0 : this.speedX += forceX * dt; 26 0 : this.speedZ += forceZ * dt; 27 : 28 0 : var drag = 0.5; // fractional decay per second 29 0 : this.speedX *= Math.pow(drag, dt); 30 0 : this.speedZ *= Math.pow(drag, dt); 31 : 32 0 : cmpPos.MoveTo(pos.x + this.speedX * dt, pos.z + this.speedZ * dt); 33 : }; 34 : 35 0 : Engine.RegisterComponentType(IID_Motion, "MotionBallScripted", MotionBallScripted);