header
ask question
Click here to ask Question Now Its free No registration required. Flash, Flex, Flash Media Server, ActionScript,Adobe Air. Most questions receive a response in an hour.
Manu
Points:0
Posts:0

2/3/2011 10:23:47 AM

Title: Implement Physics in Desktop


Hai all..

Actually I was trying to implement Box2D in flex and AIR.
I found somewhere on the net that you need Flash Builder to implement Box2D?
I could implement it in Flash builder but as a '.as' file and It is running in the browser.
I want to develop it as a Desktop application like AIR.
Can I do that. If yes can anyone give me a suggestion or sample code.
i.e I want to develop application using Box2D as a desktop application.
Anyone help please.
Thanks for reading.



1
Tyler
Points: 430
Posts:0
2/4/2011 7:05:00 AM



you can create a swf or application both in Flash Builder. Box2D physics engine can be used in both , The same code should work for both swf or adobe AIR application.

2
Halley
Points: 0
Posts:0
2/4/2011 7:13:07 AM



I found this question answered in this page

http://askmeflash.com/qdetail/1381/box2d-in-air

3
Manu
Points: 0
Posts:0
2/4/2011 10:43:37 AM



Thankyou for the reply.

The problem is the I have the following code.

Main.as


package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.Stage;	
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	
	[SWF(width='500', height='500', backgroundColor='#02091D', frameRate='30')]
	
	public class Main extends Sprite {
		public var m_dbgSprite:Sprite;
		public var m_world:b2World;
		public var m_phys_scale:Number = 30;
		public var m_timestep:Number = 1.0/30.0;
		public var m_iterations:Number = 10.0;
		
		//initial box coordinates when we first press mouse down
		public var initX:Number = 0.0;
		public var initY:Number = 0.0;
		public var drawing:Boolean = false;
		public var b:b2Body;
		
		// the visual that the user see's
		public var the_ball:Ball;
		
		private var colorArray:Array = new Array(0xFFFF33, 0xFFFFFF, 0x79DCF4, 0xFF3333, 0xFFCC33, 0x99CC33);
		
		public function Main() {
			/*
			A Box2D world needs three parameters: a b2AABB, gravity
			and a Booleand deciding whether or not to let bodies sleep
			when they are not being simulated.
			This saves CPU so should always be left on :)
			*/
			
			var gravity:b2Vec2 = new b2Vec2(0,9.8);
			var worldAABB:b2AABB = new b2AABB();
			worldAABB.lowerBound.Set(-1000,-1000);
			worldAABB.upperBound.Set(1000,1000);
			m_world = new b2World(worldAABB,gravity,true);
			
			//Add our ground and walls
			addStaticBox(250/m_phys_scale,510/m_phys_scale,250/m_phys_scale,10/m_phys_scale);
			addStaticBox(250/m_phys_scale,-10/m_phys_scale,250/m_phys_scale,10/m_phys_scale);
			addStaticBox(-10/m_phys_scale,250/m_phys_scale,10/m_phys_scale,250/m_phys_scale);
			addStaticBox(510/m_phys_scale,250/m_phys_scale,10/m_phys_scale,250/m_phys_scale);
			
			addEventListener(Event.ENTER_FRAME, update);
			stage.addEventListener(MouseEvent.MOUSE_DOWN,mousePressed);
			stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoved);
			stage.addEventListener(MouseEvent.MOUSE_UP,mouseReleased);
		}
		public function mousePressed(e:MouseEvent) : void {
			//Store initial X and Y position
			initX = e.localX;
			initY = e.localY;
			
			var randomColorId:Number = Math.floor(Math.random()*colorArray.length);

			the_ball = new Ball(50, colorArray[randomColorId]);
			
			the_ball.x = mouseX;
			the_ball.y = mouseY;
			the_ball.width = 1;
			the_ball.height = 1;
			addChild(the_ball);
			drawing = true;
		}
		public function mouseMoved(e:MouseEvent) : void {
			if (drawing) {
				the_ball.x = mouseX;
				the_ball.y = mouseY;
			}
		}
		public function mouseReleased(e:MouseEvent) : void {
			drawing = false;
			addCircle( mouseX,  mouseY, the_ball.width/2,the_ball);
		}
		public function addCircle(_x:Number, _y:Number, _radius:Number, ballclip:Ball) : void {
			var bd:b2BodyDef = new b2BodyDef();
			var cd:b2CircleDef = new b2CircleDef();
			var area:Number = Math.floor(_radius*_radius*Math.PI/25)/100;
			// area is the % of the entire stage filled by the circle
			// the entire stage area is 500*500 = 250000 pixels
			// circle area is radius*radius*PI
			// so the % is radius*radius*PI*100/250000
			cd.radius = Math.abs(_radius)/m_phys_scale;
			cd.density = 2;
			cd.restitution = 0.7;
			cd.friction = 2;
			bd.position.Set(_x/m_phys_scale, _y/ m_phys_scale);
			bd.userData = ballclip;
			b = m_world.CreateBody(bd);
			b.CreateShape(cd);
			b.SetMassFromShapes();
		}
		public function addStaticBox(_x:Number,_y:Number,_halfwidth:Number,_halfheight:Number) : void {
			var bodyDef:b2BodyDef = new b2BodyDef();
			bodyDef.position.Set(_x,_y);
			var boxDef:b2PolygonDef = new b2PolygonDef();
			boxDef.SetAsBox(_halfwidth,_halfheight);
			boxDef.density = 0.0;
			var body:b2Body = m_world.CreateBody(bodyDef);
			body.CreateShape(boxDef);
			body.SetMassFromShapes();
		}
		public function update(e:Event) : void {
			//We need to do this to simulate physics
			if (drawing) {
				the_ball.width+=2;
				the_ball.height+= 2;
			}
			m_world.Step(m_timestep,m_iterations);
			for (var bb:b2Body=m_world.m_bodyList; bb; bb=bb.m_next) {
				if (bb.m_userData is Sprite) {
					bb.m_userData.x=bb.GetPosition().x * 30;
					bb.m_userData.y=bb.GetPosition().y * 30;
					bb.m_userData.rotation=bb.GetAngle() * 180 / Math.PI;
				}
			}
		}
	}
}



In Ball.as file.




package
{
	import flash.display.Sprite;
	import flash.geom.Matrix;
	
	public class Ball extends Sprite
	{
		private var mc:Sprite;
		public function Ball(radius:Number, color:Number = 0x660033)
		{
			super();
			mc = new Sprite();
			setRegistrationPoint( mc, mc.width >> 1, mc.height >> 1, true);
			mc.graphics.lineStyle(1, 0x000000);
			mc.graphics.beginFill(color);
			mc.graphics.drawCircle(0, 0, radius);
			mc.graphics.endFill();
			 
			addChild(mc);

		}
		
		
		public function setRegistrationPoint(s:Sprite, regx:Number, regy:Number, showRegistration:Boolean) : void
		{
			//translate movieclip 
			s.transform.matrix = new Matrix(1, 0, 0, 1, -regx, -regy);
			
			//registration point.
			if (showRegistration)
			{
				var mark:Sprite = new Sprite();
				mark.graphics.lineStyle(1, 0x000000);
				mark.graphics.moveTo(-5, -5);
				mark.graphics.lineTo(5, 5);
				mark.graphics.moveTo(-5, 5);
				mark.graphics.lineTo(5, -5);
				s.addChild(mark);
			}
		}
		

	}
}



And in the Air.mxml file I have this.



<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
					   xmlns:s="library://ns.adobe.com/flex/spark" 
					   xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="init()">
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			
			public var main:Main;
			
			public function init():void{
				//Alert.show();
				main = new Main();
				
			}
		]]>
	</fx:Script>
	<fx:Declarations>
	</fx:Declarations>
	
</s:WindowedApplication>

But nothing is coming. And I am getting some errors also. I have imported the library. So there is no problem with the library. I have just started using Flash Builder and Actionscript.Please correct me if I am doing something wrong.


Post your Reply
Name  

Email

Type your Reply or Answer

Are you human? What is 8+3 



Members Login

Email  
Password
Forgot Password





This website focus on: Flash | Flex | FMS | RED5 | WOWZA | Flash Media Server | Adobe AIR | ActionScript,Flash Solutions | Flash Question | Flash Answers | Flash Developers | Flash Problem, Flash Help, Flash bugs, Flash workaround | Flash Blog | Flex Question Answers | Flash Forum | Flex Development | Actionscript development | Flash development | Adobe AIR development
Copyright © 2008 AskMeFlash.com. All rights reserved. Privacy Policy | Terms & Conditions