<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>manewc &#187; Physics</title>
	<atom:link href="http://manewc.com/category/actionscript-physics/feed/" rel="self" type="application/rss+xml" />
	<link>http://manewc.com</link>
	<description>iPhone, Flash, Flex, AIR, &#38; Web Development</description>
	<lastBuildDate>Sat, 31 Oct 2009 16:47:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>[AS 3] Squishing Effect</title>
		<link>http://manewc.com/2008/01/28/as-3-squishing-effect/</link>
		<comments>http://manewc.com/2008/01/28/as-3-squishing-effect/#comments</comments>
		<pubDate>Mon, 28 Jan 2008 20:33:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Physics]]></category>

		<guid isPermaLink="false">http://manewc.com/2008/01/28/as-3-squishing-effect/</guid>
		<description><![CDATA[So Gahlord from over at www.n0d3.org has been wanting to see a squishing effect applied on an object (ball) upon a collision (right wall). Below is a simple animation to demonstrate. The effect can be influenced by changing the objects mass as it calculates the spring distance.
Squishing effect:

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_Throw_716688063"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="/projects/flash/Squish/Throw.swf" />
	<param name="bgcolor" value="#000000" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/projects/flash/Squish/Throw.swf"
			name="fm_Throw_716688063"
			width="550"
			height="400">
		<param name="bgcolor" value="#000000" />
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
Here is [...]]]></description>
			<content:encoded><![CDATA[<p>So Gahlord from over at <a href="http://n0d3.org">www.n0d3.org</a> has been wanting to see a squishing effect applied on an object (ball) upon a collision (right wall). Below is a simple animation to demonstrate. The effect can be influenced by changing the objects mass as it calculates the spring distance.</p>
<p>Squishing effect:</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_Throw_1891634770"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="/projects/flash/Squish/Throw.swf" />
	<param name="bgcolor" value="#000000" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/projects/flash/Squish/Throw.swf"
			name="fm_Throw_1891634770"
			width="550"
			height="400">
		<param name="bgcolor" value="#000000" />
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>Here is the standard Ball.as class file:</p>
<pre name="code" class="c-sharp">
package {
	import flash.display.Sprite;

	public class Ball extends Sprite {
		public var radius:Number;
		public var diameter:Number;
		private var color:uint;

		public var vx:Number = 0;
		public var vy:Number = 0;

		public function Ball(radius:Number=20, color:uint=0xb3d830) {
			this.radius = radius;
			this.color = color;
			this.diameter = radius * 2;
			init();
		}
		public function init():void {
			graphics.beginFill(color);
			graphics.drawCircle(0, 0, radius);
			graphics.endFill();
		}
	}
}</pre>
<p>Here is the document class Throw.as:</p>
<pre name="code" class="c-sharp">
package
{
	/* resources: http://ww2.slcc.edu/schools/hum_sci/physics/tutor/2210/kinetic_energy/ */

	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat; 

	public class Throw extends Sprite
	{
		private var ball:Ball;
		private var vx:Number;
		private var vy:Number;
		private var bounce:Number = -0.7;
		private var gravity:Number = .86;

		private var mass:Number = 10;
		private var springConstant:Number = 1200; // represents 1200 N/m
		private var springDistance:Number;

		private var oldX:Number;
		private var oldY:Number;

		private var ballSpringDistance:TextField;

		public function Throw()
		{
			init();
		}

		private function init():void
		{
			ball = new Ball(30, Math.random() * 0xffffff);
			ball.x = stage.stageWidth / 2;
			ball.y = stage.stageHeight / 2;

			vx = 0;
			vy = 0;
			addChild(ball);

			ball.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);

			CreateTextField();
			ballSpringDistance.text = "Throw the ball fast or slow against the right wall";
		}

		private function onEnterFrame(event:Event):void
		{
			vy += gravity;
			ball.x += vx;
			ball.y += vy;

			var ltWall:Number = 0;
			var rtWall:Number = stage.stageWidth;
			var topWall:Number = 0;
			var botWall:Number = stage.stageHeight;

			if(ball.x + ball.radius &gt; rtWall)
			{
				springDistance = Math.round((vx * Math.sqrt(mass / 1200)));

				// SQUISH
				ball.width = ball.diameter - springDistance;
				ball.height = ball.diameter + springDistance;

				ball.x = rtWall - ball.radius + (springDistance / 2);

				//trace ("Kinetic Energy: " + (-1 * .5 * mass * vx * vx));
				// distance = velocity times the square root of the mass divided by the spring constant (1200 N/m)
				// trace ("Distance: " + Math.round((vx * Math.sqrt(mass / 1200))));
				ballSpringDistance.y = ball.y - 10;
				ballSpringDistance.x = ball.x - 180;
				ballSpringDistance.text = "Squash: " + Math.round((vx * Math.sqrt(mass / 1200)));

				// stop the ball from traveling:
				removeEventListener(Event.ENTER_FRAME, onEnterFrame);

				init();

			}
			else if(ball.x - ball.radius &lt; ltWall)
			{
				ball.x = ltWall + ball.radius;
				vx *= bounce;
			}
			if(ball.y + ball.radius &gt; botWall)
			{
				ball.y = botWall - ball.radius;
				vy *= bounce;
			}
			else if(ball.y - ball.radius &lt; topWall)
			{
				ball.y = topWall + ball.radius;
				vy *= bounce;
			} else {

			}
		}

		private function onMouseDown(event:MouseEvent):void
		{
			oldX = ball.x;
			oldY = ball.y;
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			ball.startDrag();

			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			addEventListener(Event.ENTER_FRAME, trackVelocity);
		}

		private function onMouseUp(event:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			ball.stopDrag();
			removeEventListener(Event.ENTER_FRAME, trackVelocity);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function trackVelocity(event:Event):void
		{
			vx = ball.x - oldX;
			vy = ball.y - oldY;
			oldX = ball.x;
			oldY = ball.y;
		}

		private function CreateTextField():void
        {
            // pulled this from:
            // http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/text/TextField.html#includeExamplesSummary
            ballSpringDistance = new TextField();
            ballSpringDistance.autoSize = TextFieldAutoSize.LEFT;
            ballSpringDistance.background = false;
            ballSpringDistance.border = false;
			ballSpringDistance.multiline = true;

            var format:TextFormat = new TextFormat();
            format.font = "Arial";
            format.color = 0xffffff;
            format.size = 12;
            format.underline = false;  

			ballSpringDistance.defaultTextFormat = format;
            addChild(ballSpringDistance);
        }
	}
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://manewc.com/2008/01/28/as-3-squishing-effect/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>[AS 3] Keyboard Events with Acceleration</title>
		<link>http://manewc.com/2008/01/25/as-3-keyboard-events-with-acceleration/</link>
		<comments>http://manewc.com/2008/01/25/as-3-keyboard-events-with-acceleration/#comments</comments>
		<pubDate>Fri, 25 Jan 2008 23:19:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Physics]]></category>

		<guid isPermaLink="false">http://manewc.com/2008/01/25/as-3-keyboard-events-with-acceleration/</guid>
		<description><![CDATA[Here is just a simple interactive animation. You can control the wheel with your left and right keys. When the object is off the screen, it will wrap around to the other side. You can view the project here in a new window, click the flash movie to activate.I created a new wheel class that [...]]]></description>
			<content:encoded><![CDATA[<p>Here is just a simple interactive animation. You can control the wheel with your left and right keys. When the object is off the screen, it will wrap around to the other side. You can <a href="/projects/flash/WheelToGround/WheelToGround.html" target="_blank">view the project here in a new window, click the flash movie to activate</a>.I created a new wheel class that imports in a .png file:</p>
<pre name="code" class="c-sharp">
package {
	import flash.display.Sprite;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.events.ProgressEvent;	

    public class Wheel extends Sprite {
    	private var myImg:Loader = new Loader();
        public var radius:Number;
        public function Wheel(radius:Number = 100) {
        	this.radius = radius;
            init();
		}

        public function init():void {
			// let's load the image
            try {
            	myImg.load(new URLRequest("wheel.png"));
            }
            catch (error:Error)
            {
            	trace ("Unable to load requested document.");
			}			

            myImg.contentLoaderInfo.addEventListener(Event.COMPLETE,result);
		}

        public function result(evt:Event):void
        {
        	addChild(myImg);
            myImg.width = myImg.height = radius * 2;			

            // change the registration point
            myImg.x = -1 * radius;
            myImg.y = -1 * radius;
		}
	}
}</pre>
<p>Here is the document class to control all of this, named WheelToGround.as</p>
<pre name="code" class="c-sharp">
package{
	import flash.display.Sprite;
   	import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;	

    public class WheelToGround extends Sprite	{
    	private var wheel:Wheel;
        private var vx:Number = 0;
        private var accelerationRate:Number = 0
        private var line:Sprite;		

        public function WheelToGround()
        {
        	init();
		}

		private function init():void
        {
        	// Create the Ground:
            line = new Sprite();
            line.graphics.lineStyle(1);
            line.graphics.moveTo(0, stage.stageHeight - 10);
            line.graphics.lineTo(stage.stageWidth, stage.stageHeight - 10);
            addChild(line);			

            // Get the wheel
            wheel = new Wheel(50);
            addChild(wheel);
            wheel.x = stage.stageWidth / 2;
            wheel.y = stage.stageHeight - 10 - wheel.radius;			

            addEventListener(Event.ENTER_FRAME, onEnterFrame);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}		

        private function onKeyDown(event:KeyboardEvent):void
        {
        	if(event.keyCode == Keyboard.LEFT)
            {
            	accelerationRate = -0.2;
			}
            else if(event.keyCode == Keyboard.RIGHT)
            {
            	accelerationRate = 0.2;
			}
		}

        private function onKeyUp(event:KeyboardEvent):void
        {
        	accelerationRate = 0;
		}

        private function onEnterFrame(event:Event):void
        {
        	vx += accelerationRate;
            wheel.x += vx;
            wheel.rotation += vx;			

            // is it off the screen?			

            if ( wheel.x &gt; stage.stageWidth + wheel.width )
            {
            	wheel.x = 0 - wheel.width;
			}
            else if ( wheel.x &lt; 0 - wheel.width )
            {
            	wheel.x = stage.stageWidth + wheel.width;
			}
		}
	}
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://manewc.com/2008/01/25/as-3-keyboard-events-with-acceleration/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>[AS 3] Bouncing Balls with Collision Detection within Bounds</title>
		<link>http://manewc.com/2008/01/24/as-3-bouncing-balls-with-collision-detection-within-bounds/</link>
		<comments>http://manewc.com/2008/01/24/as-3-bouncing-balls-with-collision-detection-within-bounds/#comments</comments>
		<pubDate>Thu, 24 Jan 2008 23:04:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Physics]]></category>

		<guid isPermaLink="false">http://manewc.com/2008/01/24/as-3-bouncing-balls-with-collision-detection-within-bounds/</guid>
		<description><![CDATA[So, just playing around for now. Here is just some code where you can hardcode in the number of balls to animate within the stage area. You can also change the bounding area, with a few more variables, so the balls will be contained. This may serve as a little groundwork for a simple interactive [...]]]></description>
			<content:encoded><![CDATA[<p>So, just playing around for now. Here is just some code where you can hardcode in the number of balls to animate within the stage area. You can also change the bounding area, with a few more variables, so the balls will be contained. This may serve as a little groundwork for a simple interactive game I just thought of.</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_Bouncing_1446074741"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="/projects/flash/Bouncing/Bouncing.swf" />
	<param name="bgcolor" value="#eaeaea" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/projects/flash/Bouncing/Bouncing.swf"
			name="fm_Bouncing_1446074741"
			width="550"
			height="400">
		<param name="bgcolor" value="#eaeaea" />
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>Here is a new updated Ball.as class I got from my book &#8220;Foundation Actionscript 3.0 Animation: Making Things Move!  by Keith Peters&#8221;</p>
<pre name="code" class="c-sharp">
package {
	import flash.display.Sprite;

	public class Ball extends Sprite {
		public var radius:Number;
		private var color:uint;

		public var vx:Number = 0;
		public var vy:Number = 0;
		public var NewXPos:Number = 0;
		public var NewYPos:Number = 0;

		public function Ball(radius:Number=20, color:uint=0xb3d830) {
			this.radius = radius;
			this.color = color;
			init();
		}

		public function init():void {
			graphics.beginFill(color);
			graphics.drawCircle(0, 0, radius);
			graphics.endFill();
		}
	}
}</pre>
<p>The document class Bouncing.as file to make the balls bounce:</p>
<pre name="code" class="c-sharp">
package
{
	import flash.display.Sprite;
	import flash.events.Event;

	public class Bouncing extends Sprite
	{
		private var balls:Array;
		private var ttl_balls:uint = 10; // total number of balls

		public function Bouncing()
		{
			init();
		}

		private function init():void
		{
			balls = new Array();
			for(var ct:uint = 0; ct &lt; ttl_balls; ct++)
			{
				var ball:Ball = new Ball(Math.random() * 5 + 5, Math.random() * 0xffffff);
				balls.push(ball);

				ball.vx = Math.random() * 10 + (Math.random() * 5);
				ball.vy = Math.random() * 10 + (Math.random() * 5);

				ball.x = Math.random() * stage.stageWidth;
				ball.y = Math.random() * stage.stageHeight;

				addChild(ball);
			}
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(event:Event):void
		{
			for(var ct:uint = 0; ct &lt; ttl_balls; ct++)
			{
				var ball:Ball = balls[ct];

				detectAndMove(ball);
			}
		}

		private function detectAndMove(ball:Ball):void
		{
			var myRadius = ball.radius;

			ball.NewXPos=ball.x + ball.vx;
			ball.NewYPos=ball.y + ball.vy;

			// CHECK FOR COLLISIONS TO THE LEFT AND RIGHT BOUNDS
			if (ball.NewXPos + ball.radius &gt; stage.stageWidth)
			{
				ball.vx *= -1;
				ball.NewXPos = stage.stageWidth - ball.radius;
			}
			else if (ball.NewXPos - ball.radius &lt; 0)
			{
				ball.vx *= -1;
				ball.NewXPos = ball.radius;
			}

			// CHECK FOR COLLISIONS TO THE TOP AND BOTTOM BOUNDS
			if (ball.NewYPos + ball.radius &gt; stage.stageHeight)
			{
				ball.vy *= -1;
				ball.NewYPos = stage.stageHeight - ball.radius;
			}
			else if (ball.NewYPos - ball.radius &lt; 0)
			{
				ball.vy *= -1;
				ball.NewYPos = ball.radius;
			}

			// ANIMATE
			ball.x = ball.NewXPos;
			ball.y = ball.NewYPos;
		}
	}
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://manewc.com/2008/01/24/as-3-bouncing-balls-with-collision-detection-within-bounds/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>
