Moving the mask with entity : Pendulum


(Zouhair Elamrani Abou Elassad) #1

I’ve been trying t create a Pendulum with FlashPunk, this is my code :

        var pendX:Number = 0;
	var pendY:Number = 0;
	var pendRadius:Number = 600;
	var pendArc:Number = 150/360;
	var pendSpeed:Number = .004;
	var pendCount:Number = 0;

in the update function :

pendCount += pendSpeed;
		pendCount %= 1;
		
		var point = pendulum (pendX, pendY, pendRadius, pendArc, pendCount);
		
		_pendulumTrap.angle = Math.atan2(point.x, point.y) * (75 / Math.PI);

the pendulum function:

public function pendulum (centerX, centerY, radius, aoi, completionRatio){
		
		var easedOneToNegOne = Math.cos(completionRatio*2*Math.PI);
		
		var aoiRadians = aoi * 2 * Math.PI;
		
		var currentRotation = (easedOneToNegOne * aoiRadians);
		
		var x = centerX + Math.sin(currentRotation) * radius ;
		var y = centerY + Math.cos(currentRotation) * radius ;
	
		return {x:x, y:y};
	}

what i’m trying to do now is to add collision to my pendulum, i generated the mask :

mask = new Pixelmask(PENDULUMTRAP_ART);

but is there a way to move the mask in a way that follow the pendulum ?


(Martí Angelats i Ribera) #2

If i were you i would move the entire entity and calculate the position using the time.

public class Pendulum extends Entity
{
	[embed(src=)]
	public const PENDULUM_ART:Class;

	//constants of your pendulum. If cahnge this the pendulum will change
	private var _k:Number; //a constant for the movement
	private var _angleMax:Number; //the maximum angle
	private var _length:Number; //the length of the pendulum
	private var _initialX:Number; //offset, not the world position
	private var _initialY:Number; //offset, not the world position

	//the actual variables
	private var _time:Number; //a timer to create the function
	private var _angle:Number;
	
	public function Pendulum(x:Number = 0, y:Number = 0, length:Number = 50, angle:Number = Math.PI, gravity:Number = 20)
	{
		super(0, 0, PENDULUM_ART, PENDULUM_ART);
		_initialX = x;
		_initialY = y;
		_k = Math.sqrt(gravity/length);
		_angleMax = angle;
		_length = length;
		_time = 0;
		
		
		setPendulumPosition();
	}
	
	override public function update():void
	{
		_time += FP.elapsed;
		_time %= 8; //not necessary but used to avoid long-time problems making _time infinite. It's not like so but just in case
		
		setPendulumPosition();
	}
	
	private function setPendulumPosition():void
	{
		_angle = _angleMax * Math.cos(_k*_time);
		x = Math.sin(_angle) * _length + _initialX;
		y = Math.cos(_angle) * _length + _initialY;
	}
}

As a note, you’ll have to change the values acording to your game and scale. Also be carefull with the angle, it is in radiants.

Edit: found an error in the code.

PS: The gravity must be positive to make it feel like the real world (the Y axe is inverted in Flash).


(Zouhair Elamrani Abou Elassad) #3

Ooh i see, i will try your code, thanks a lot, meanwhile i was able to solve the mask issue using the render function :smile:


(Martí Angelats i Ribera) #4

Oh ok. I’m glad you found it usefull (some time without do physics… This is why i love to help people here :smiley: )


(Zouhair Elamrani Abou Elassad) #5

Thanks a lot, you’re doing a great job :smile: