AS3: Moving a Starling object with Nape

For those who may not have been able to find anything about this or just not figured it out yet, here is an effective way to animate the coordinates of a Starling Movieclip while keeping the Nape body physics in tact:

 

setup body.graphic as a Starling Object

animate the body.graphic within an EnterFrameEvent:

public function animate(EnterFrameEvent:Event):void {      
     body.graphic.x +=  10; 
     body.graphic.y += 10; 
     //here is where the black magic begins      
     updatePos(body); 
}

 

now the update:

public static function updatePos(body:Body):void {      
     body.velocity.x = (body.graphic.x - body.position.x) / GameSpace.timeStep;     
     body.velocity.y = (body.graphic.y - body.position.y) /  GameSpace.timeStep;      
     //timeStep is determined by you, so change and tweak it until you get it right     
     //I am currently using: GameSpace.timeStep = 1 / 90; 
} 

 

This will update the body KINEMATIC or DYNAMIC body along with the associated body.graphic.
Keep in mind that your space.step will need to be updated as well as the amount of space covered by your EnterFrameEvnt to get the right balance of movement of body, speed and collision/sensor checks…

 

- Nape moves me

3 thoughts on “AS3: Moving a Starling object with Nape

  1. Wow! This could be one particular of the most beneficial blogs We’ve ever arrive across on this subject. Basically Magnificent. I am also an expert in this topic therefore I can understand your effort.

    • Nape has been updated to version 2, so therefore a slightly different method is neccessary. I will be updating with a more robust method shortly.

      • The slightly updated method is here below:

        public static function updatePos(body:Body):void 
        		{      
        			body.velocity.x = (body.userData.graphic.x - body.position.x) / GameSpace.timeStep;     
        			body.velocity.y = (body.userData.graphic.y - body.position.y) /  GameSpace.timeStep;  
        		}