A Pendulum With FlashPunk


(Zouhair Elamrani Abou Elassad) #1

Hello Again , i want to thank you first for all the help you’re giving me guys, i appreciate it a lot. I’ve trying to create lately a Pendulum using FlashPunk, a one that looks a bit realistic like this one created with AS3 :

this is what i did, but is there more efficient way to handle it :

        [Embed(source = "../../assets/images/trap/pendulum.png"]
	protected static const PENDULUMTRAP_ART:Class;
	protected var pendulum:MovieClip = new PENDULUMTRAP_ART();
	
	protected var _pendulumTrap:Image
	
	protected var pAngle:Number = 1.57;
	protected var pRotate:Number = 45;
	
	
	
	
	public function PendulumTrap(x:Number=0, y:Number=0) 
	{
		this.x = x;	
		this.y = y;
		
		_pendulumTrap = new Image(PENDULUMTRAP_ART);
		
		layer = 1;		
		
	}
	
	override public function update():void 
	{
		trace("rotate "+pRotate);
		_pendulumTrap.angle = Math.sin(pAngle) * pRotate;
		pRotate += 0.1;
	}
	
	override public function added():void 
	{
		graphic = _pendulumTrap;
		
	}

This one is going 360 rotation degree, so it’s not really a pendulum .


(Jacob Albano) #2

If you haven’t started noticing slowdown problems, you don’t have to worry about optimization. The logic here is two lines of code; there’s not a lot to improve.


(Zouhair Elamrani Abou Elassad) #3

I was kinda thinking if there is like a library or something to use with FlashPunk or a Tween class that could help with job, but i see what you mean.


(Mike Evmm) #4

Are you going for a realistic pendulum though, or just a regular pendulum feel?


(Zouhair Elamrani Abou Elassad) #5

I’m still hoping for the realistic one, i’m still looking for the way to do it, is there a way to use Movie Clips in FlashPunk ?


(Mike Evmm) #6

There is, using solely the as3 framework, but there really is no need, having FP. If you’re going to simulate a pendulum, though, be sure to get familiar with the physics involved (they really aren’t too complicated). I can provide some links if you’d like, but it’s easily googled. From the top of my head, I’d say you’d need a couple of vectors, some math, one entity for the pendulum and maybe.use the Draw function for the string (or a graphic). Be warned though, if this is for simple game integration this is absolutely overkill.


(Ultima2876) #7

You’ll be able to do that millions of times per second without any issues. There’s no need to spend any more time thinking about this :smile:

However, if you did want to optimise it, you could use a lookup table for sin…


(Zouhair Elamrani Abou Elassad) #8

Yes thank you, if you can provide me with anything that can help me that would be great, but also it would be interesting if i knew how to use Movie Clips with FlashPunk, i’ve already worked on some projects with Flash CS5 and i could use some of my previous work, once again thank you.


(Zouhair Elamrani Abou Elassad) #9

I’ll do that, thank you :slight_smile:


(Mike Evmm) #10

I’ve just noticed - is the code you posted originally working well? If it isn’t (in which case I completely misinterpreted the question and went way off-topic), it’s probably some Radian/Degree problem. Anyway:
-This gives a good overview on a pendulum’s movement. It’s rather lengthy, but if you’re comfortable with physics, skim reading should be enough. Again, be warned that unless you have a very very good reason for wanting an accurate physical simulation this is overkill. (Overkill can be fun if you don’t have a deadline, though), and
-Here is an as3 lib. for vectors. It should save you some coding (not too much, but it’s also efficient).
-A much more programming oriented explanation of the pendulum movement. This one’s probably a very good bet.


(Zouhair Elamrani Abou Elassad) #11

Thank you so much, i’ll go through all of the links above and i’ll keep you posted :slight_smile: