Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Daniel o rei

[Resolvido] Box2d e xml

Recommended Posts

Eu andei procurando sobre box2d e xml e não achei muita coisa, e nem sei se tem para actionscript, mas ta aqui um exemplo,

Com xml fica muito mais fácil, e evita um script enorme, já que para que criar um objeto precisa de algumas linhas para colocar configurações de fricção, restituição, posição, tamanho, e com o xml cada corpo cabe em uma linha,

 

package{
import flash.events.MouseEvent;
import flash.display.Sprite ;
	import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
	import Box2D.Dynamics.*;
	import flash.events.*;
public class box2dxml extends Sprite{
	var boxarray:Array = new Array();
	var circlearray:Array = new Array();
	public var world:b2World;
	var xmlmap:XML = 

	<xmlmap><mapa num="1">
	<circulo id="0" size="200" x="50" y="50" obj="1" type="1" />
	<circulo id="1" size="100" x="55" y="100" obj="1" type="1"/>
	<box id="0" width="740" height="28" x="287" y="334" obj="3" type="0" friction2="on" friction="0" />
	<box id="1" width="224" height="20" x="12"  y="291"  obj="3" type="0" friction2="on"friction="0" angle2="on" angle="20"/>

	<objeffect id="0" type="gravity" x="50" y="50" />
	</mapa>
	<mapa num="2">
	<circulo id="0" size="15" x="107"y="133" obj="1" type="1" />
	<circulo id="1" size="15" x="142"y="130" obj="1" type="1"/>
	<circulo id="2" size="15" x="142"y="137" obj="1" type="1"/>
	<circulo id="3" size="15" x="148" y="140" obj="1" type="1"/>
	<circulo id="4" size="15" x="147" y="145" obj="1" type="1"/>
	<circulo id="5" size="15" x="150" y="133" obj="1" type="1"/>
	<circulo id="6" size="15" x="149" y="141" obj="1" type="1"/>
	<circulo id="7" size="15" x="152" y="133" obj="1" type="1"/>
	<circulo id="8" size="15" x="133" y="143" obj="1" type="1"/>
	<circulo id="9" size="15" x="135" y="135" obj="1" type="1"/>
	<circulo id="10"size="15" x="160" y="130" obj="1" type="1"/>
	<box id="0" width="31"  height="227"x="55"   y="171" obj="1" type="0" restitution2="on"restitution="1.5"/>
	<box id="1" width="199" height="31" x="142"  y="71"  obj="1" type="0" restitution2="on"restitution="1.5"  />
	<box id="2" width="202" height="31" x="141"  y="298" obj="1" type="0" restitution2="on"restitution="1.5"  />
	<box id="3" width="31"  height="134"x="227"  y="216" obj="1" type="0" restitution2="on"restitution="1.5"  />
	<box id="4" width="239" height="31" x="332"  y="98"  obj="1" type="0" restitution2="on"restitution="1.5"  />
	<box id="5" width="199" height="31" x="311"  y="163" obj="1" type="0" restitution2="on"restitution="1.5"  />
	<box id="6" width="31"  height="134"x="401"  y="215" obj="1" type="0" restitution2="on"restitution="1.5"  />
	<box id="7" width="31"  height="199"x="460"  y="182" obj="1" type="0" restitution2="on"restitution="1.5"  />



	<objeffect id="0" type="gravity" x="50" y="50" />
	</mapa>
	<mapa num="3">
	<circulo id="0" size="200" x="50" y="50" obj="1" type="1" />
	<circulo id="1" size="100" x="55" y="100" obj="1" type="1"/>
	<box id="0" width="740" height="28" x="287" y="334" obj="1" type="0"  />
	<objeffect id="0" type="gravity" x="50" y="50" />
	</mapa>

	</xmlmap>

	;

	function box2dxml(){			
		var grav=new b2Vec2(00,10);
		world = new b2World(grav,true);
		stage.addEventListener(Event.ENTER_FRAME,update);
		create_boxies(1);
		create_circles(1);
		mapa1.addEventListener(MouseEvent.CLICK, mp1);
		mapa2.addEventListener(MouseEvent.CLICK, mp2);
		mapa3.addEventListener(MouseEvent.CLICK, mp3);
	}
	function update(e:Event):void{
		for (var bb:b2Body = world.GetBodyList(); bb; bb = bb.GetNext()){
			if (bb.GetUserData() is Sprite){
				var sprite:Sprite = bb.GetUserData() as Sprite;
				sprite.x = bb.GetPosition().x * 30;
				sprite.y = bb.GetPosition().y * 30;
				}
		}
world.Step(1/30 , 10, 10);
}

	function create_boxies(number:Number):void {

		var xxboxShape:b2PolygonShape = new b2PolygonShape();
		var xfixtureDef:b2FixtureDef = new b2FixtureDef();
		var xbodyDef:b2BodyDef = new b2BodyDef();
		number -=1;
		for (var i = 0; i <= xmlmap.mapa[number].box.length() - 1; i++) {
			xmlmap.mapa.circulo.@width;
			xxboxShape = new b2PolygonShape();
			xxboxShape.SetAsBox((xmlmap.mapa[number].box[i].@width / 30)/2 , (xmlmap.mapa[number].box[i].@height / 30) / 2 );
			xfixtureDef.shape=xxboxShape;
			xfixtureDef.density=1.0;
			if (xmlmap.mapa[number].box[i].@friction2 == "on") {
			xfixtureDef.friction=xmlmap.mapa[number].box[i].@friction;
			}else{
			xfixtureDef.friction=0.5;
			}
			if (xmlmap.mapa[number].box[i].@restitution2 == "on") {
			xfixtureDef.restitution=xmlmap.mapa[number].box[i].@restitution;
			}else{
			xfixtureDef.restitution=0.2;
			}
			if(xmlmap.mapa[number].box[i].@obj == 3){
				xbodyDef.userData = new ice();
			}else{
			xbodyDef.userData = new quadra();
			}
			xbodyDef.userData.width=xmlmap.mapa[number].box[i].@width;
			xbodyDef.userData.height=xmlmap.mapa[number].box[i].@height;
			if (xmlmap.mapa[number].box[i].@angle2 == "on") {
			xbodyDef.angle = (xmlmap.mapa[number].box[i].@angle * Math.PI) / 180 ;
			xbodyDef.userData.rotation = xmlmap.mapa[number].box[i].@angle;
			}
			xbodyDef.type=0;				
			if (xmlmap.mapa[number].box[i].@type==2) {
				xbodyDef.type=2;
			}
			addChildAt(xbodyDef.userData,0);
			xbodyDef.position.Set(xmlmap.mapa[number].box[i].@x / 30 ,xmlmap.mapa[number].box[i].@y / 30);
			boxarray[i]=world.CreateBody(xbodyDef);
			boxarray[i].CreateFixture(xfixtureDef);


		}


}
function create_circles(number:Number) {
		var xcircleShape:b2CircleShape;
		number -=1;
		var xfixtureDef:b2FixtureDef = new b2FixtureDef();
		var xbodyDef:b2BodyDef = new b2BodyDef();
		for (var i:Number = 0; i <= xmlmap.mapa[number].circulo.length() - 1; i++) {
			xcircleShape=new b2CircleShape(xmlmap.mapa[number].circulo[i].@size/30/2);
			xfixtureDef.shape=xcircleShape;
			xfixtureDef.density=1.0;
			xfixtureDef.friction=0.5;
			xfixtureDef.restitution=0.2;
			xbodyDef.userData = new circle();
			xbodyDef.userData.width=xmlmap.mapa[number].circulo[i].@size;
			xbodyDef.userData.height=xmlmap.mapa[number].circulo[i].@size;
			if (xmlmap.mapa[number].circulo[i].@type=="1") {
				xbodyDef.type=b2Body.b2_dynamicBody;
			}
			addChildAt(xbodyDef.userData,0);
			xbodyDef.position.Set(xmlmap.mapa[number].circulo[i].@x / 30 ,xmlmap.mapa[number].circulo[i].@y / 30);
			circlearray[i]=world.CreateBody(xbodyDef);
			circlearray[i].CreateFixture(xfixtureDef);
		}
	}
	function detonatudo(){
		for (var i:Number = 0; i <= boxarray.length; i++) {
			removeChild(boxarray[i].GetUserData());
			world.DestroyBody(boxarray[i]);
			if (i==boxarray.length-1) {
				i=boxarray.length;
			}
		}
		for (i = 0; i <= circlearray.length; i++) {
			removeChild(circlearray[i].GetUserData());
			world.DestroyBody(circlearray[i]);
			if (i==circlearray.length-1) {
				i=circlearray.length;
			}
		}
		circlearray =[];
		boxarray =[];
	}
	function mp1(e:MouseEvent){
		detonatudo();
		create_circles(1);
		create_boxies(1);
	}
	function mp2(e:MouseEvent){
		detonatudo();
		create_circles(2);
		create_boxies(2);
	}
	function mp3(e:MouseEvent){
		detonatudo();
		create_circles(3);
		create_boxies(3);
	}

}}

 

São duas funções para criar os objetos a partir do xml,

Todos esses "bodies" são salvos num array,

Para excluir tem a função detonatudo que tem um for para remover o movieclip e remover o objeto do mundo.

Na função detonatudo os arrays com os objetos são zerados para evitar erros,

 

http://megaswf.com/serve/1260609

 

zip com os arquivos:

http://danielbelmiro...om/box2dxml.zip

http://megaswf.com/serve/1260609

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.