WEB开发网
开发学院图形图像Flash The engine behind Splitter Flash game – new A... 阅读

The engine behind Splitter Flash game – new AS3 prototypes

 2009-10-30 00:00:00 来源:WEB开发网   
核心提示:While Guillaume wants to refine a bit the final code before publishing it, here it is Bryce work:Here is my messy attempt at recreating the splitter engine that

While Guillaume wants to refine a bit the final code before publishing it, here it is Bryce work:

Here is my messy attempt at recreating the splitter engine that you posted about on your blog. The code is VERY messy and it doesn’t really work but I decided to send it to you incase it would help at all. The controls are a little funky, you have to move the mouse over to the lower right corner so that the red line is across one of the shapes and click. Sometimes it will (sort of)split it, sometimes it will do nothing. Gravity is disabled at first, hold g to turn it on and release it to turn it off(I added the gravity toggle to try and figure out what was going wrong).

The actionscript resides in two files, one in the timeline and one in the Cutter class

This is the one in the timeline:

import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.Joints.*;
import Box2D.Dynamics.Contacts.*;
import Box2D.Common.Math.*;
import Box2d.Common.*;
 
import flash.events.*;
 
addEventListener(Event.ENTER_FRAME, update, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mUp);
//stage.addEventListener(KeyboardEvent.KEY_DOWN, kDown);
//stage.addEventListener(KeyboardEvent.KEY_UP, kUp);
 
var MouseIsDown:Boolean = false;
 
var m_iterations:int = 10;
var m_timeStep:Number = 1/30;
var m_physScale:Number = 50;
 
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-1000.0, -1000.0);
worldAABB.upperBound.Set(1000.0, 1000.0);
 
// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2(0.0, 0.0);
 
// Allow bodies to sleep
var doSleep:Boolean = true;
 
// Construct a world object
var m_world = new b2World(worldAABB, gravity, doSleep);
 
var dbgDraw:b2DebugDraw = new b2DebugDraw();
//var dbgSprite:Sprite = new Sprite();
//m_sprite.addChild(dbgSprite);
dbgDraw.m_sprite = this;
dbgDraw.m_drawScale = 50.0;
dbgDraw.m_fillAlpha = 0.3;
dbgDraw.m_lineThickness = 1.0;
dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit;
m_world.SetDebugDraw(dbgDraw);
 
 
var wallSd:b2PolygonDef = new b2PolygonDef();
var wallBd:b2BodyDef = new b2BodyDef();
var wallB:b2Body;
 
// Left
wallBd.position.Set(-95 / m_physScale, 360/m_physScale/2);
wallSd.SetAsBox(100/m_physScale, 400/m_physScale/2);
wallB = m_world.CreateBody(wallBd);
wallB.CreateShape(wallSd);
wallB.SetMassFromShapes();
// Right
wallBd.position.Set((640+95) / m_physScale, 360/m_physScale/2);
wallB = m_world.CreateBody(wallBd);
wallB.CreateShape(wallSd);
wallB.SetMassFromShapes();
// Top
wallBd.position.Set(640/m_physScale/2, -95/m_physScale);
wallSd.SetAsBox(680/m_physScale/2, 100/m_physScale);
wallB = m_world.CreateBody(wallBd);
wallB.CreateShape(wallSd);
wallB.SetMassFromShapes();
// Bottom
wallBd.position.Set(640/m_physScale/2, (360+95)/m_physScale);
wallB = m_world.CreateBody(wallBd);
wallB.CreateShape(wallSd);
wallB.SetMassFromShapes();
 
var body:b2Body;
 
// Spawn in a bunch of crap
for (var i = 0; i < 5; i++){
 var bodyDef:b2BodyDef = new b2BodyDef();
 //bodyDef.isBullet = true;
 var boxDef:b2PolygonDef = new b2PolygonDef();
 boxDef.density = 1.0;
 // Override the default friction.
 boxDef.friction = 0.3;
 boxDef.restitution = 0.1;
 boxDef.SetAsBox((Math.random() * 5 + 10) / m_physScale, (Math.random() * 5 + 10) / m_physScale);
 bodyDef.position.Set((Math.random() * 400 + 120) / m_physScale, (Math.random() * 150 + 50) / m_physScale);
 bodyDef.angle = Math.random() * Math.PI;
 body = m_world.CreateBody(bodyDef);
 body.CreateShape(boxDef);
 body.SetMassFromShapes();
 
}
/*
for (i = 0; i < 5; i++){
 var bodyDefC:b2BodyDef = new b2BodyDef();
 //bodyDefC.isBullet = true;
 var circDef:b2CircleDef = new b2CircleDef();
 circDef.density = 1.0;
 circDef.radius = (Math.random() * 5 + 10) / m_physScale;
 // Override the default friction.
 circDef.friction = 0.3;
 circDef.restitution = 0.1;
 bodyDefC.position.Set((Math.random() * 400 + 120) / m_physScale, (Math.random() * 150 + 50) / m_physScale);
 bodyDefC.angle = Math.random() * Math.PI;
 body = m_world.CreateBody(bodyDefC);
 body.CreateShape(circDef);
 body.SetMassFromShapes();
 
}
*/
for (i = 0; i < 15; i++){
 var bodyDefP:b2BodyDef = new b2BodyDef();
 //bodyDefP.isBullet = true;
 var polyDef:b2PolygonDef = new b2PolygonDef();
 if (Math.random() > 0.66){
  polyDef.vertexCount = 4;
  polyDef.vertices[0].Set((-10 -Math.random()*10) / m_physScale, ( 10 +Math.random()*10) / m_physScale);
  polyDef.vertices[1].Set(( -5 -Math.random()*10) / m_physScale, (-10 -Math.random()*10) / m_physScale);
  polyDef.vertices[2].Set((  5 +Math.random()*10) / m_physScale, (-10 -Math.random()*10) / m_physScale);
  polyDef.vertices[3].Set(( 10 +Math.random()*10) / m_physScale, ( 10 +Math.random()*10) / m_physScale);
 }
 else if (Math.random() > 0.5){
  polyDef.vertexCount = 5;
  polyDef.vertices[0].Set(0, (10 +Math.random()*10) / m_physScale);
  polyDef.vertices[2].Set((-5 -Math.random()*10) / m_physScale, (-10 -Math.random()*10) / m_physScale);
  polyDef.vertices[3].Set(( 5 +Math.random()*10) / m_physScale, (-10 -Math.random()*10) / m_physScale);
  polyDef.vertices[1].Set((polyDef.vertices[0].x + polyDef.vertices[2].x), (polyDef.vertices[0].y + polyDef.vertices[2].y));
  polyDef.vertices[1].Multiply(Math.random()/2+0.8);
  polyDef.vertices[4].Set((polyDef.vertices[3].x + polyDef.vertices[0].x), (polyDef.vertices[3].y + polyDef.vertices[0].y));
  polyDef.vertices[4].Multiply(Math.random()/2+0.8);
 }
 else{
  polyDef.vertexCount = 3;
  polyDef.vertices[0].Set(0, (10 +Math.random()*10) / m_physScale);
  polyDef.vertices[1].Set((-5 -Math.random()*10) / m_physScale, (-10 -Math.random()*10) / m_physScale);
  polyDef.vertices[2].Set(( 5 +Math.random()*10) / m_physScale, (-10 -Math.random()*10) / m_physScale);
 }
 polyDef.density = 1.0;
 polyDef.friction = 0.3;
 polyDef.restitution = 0.1;
 bodyDefP.position.Set((Math.random() * 400 + 120) / m_physScale, (Math.random() * 150 + 50) / m_physScale);
 bodyDefP.angle = Math.random() * Math.PI;
 body = m_world.CreateBody(bodyDefP);
 body.CreateShape(polyDef);
 body.SetMassFromShapes();
}
 
function update(e:Event):void
{
 this.graphics.clear()
 m_world.Step(m_timeStep, m_iterations);
 
 var segment:b2Segment = new b2Segment();
 segment.p1.SetV(new b2Vec2(50 / m_physScale, 50 / m_physScale))
 segment.p2.SetV(new b2Vec2(mouseX / m_physScale, mouseY / m_physScale))
 //segment.ExtendForward(worldAABB);
 
 var lambda = [1];
 var normal:b2Vec2 = new b2Vec2();
 var maxShapes:Number = 64;
 var shapes:Array = new Array();
 var count:Number = m_world.Raycast(segment,shapes,maxShapes,false,null);
 
 if (MouseIsDown)
 {
  for (var i:int = 0; i < count; i++)
  {
   if (shapes[i].GetType() != b2Shape.e_polygonShape)
   {
    continue;
   }
 
   var b:b2Body = shapes[i].GetBody();
 
   if (b.GetMass() <= 0)
   {
    continue;
   }
 
   var polyShape:b2PolygonShape = shapes[i];
   var pd:Array = new Array(2);
   pd[0] = new b2PolygonDef();
   pd[0].density = 1;
   pd[1] = new b2PolygonDef();
   pd[1].density = 1;
 
   if (Cutter.SplitShape(polyShape, segment, 0.1, pd) == 0)
   {
    b.DestroyShape(shapes[i]);
    b.CreateShape(pd[0]);
    b.SetMassFromShapes();
    b.WakeUp();
 
    var bd:b2BodyDef = new b2BodyDef();
    bd.position = b.GetPosition();
    bd.angle = b.GetAngle();
 
    var newBody:b2Body = m_world.CreateBody(bd);
    newBody.CreateShape(pd[1]);
    newBody.SetMassFromShapes();
    newBody.SetAngularVelocity(b.GetAngularVelocity());
    newBody.SetLinearVelocity(b.GetLinearVelocity());
   }
  }
 }
 
 lambda=[1];
 lambda=lambda[0];
 
 this.graphics.lineStyle(1,0xff0000,1);
 this.graphics.moveTo(segment.p1.x * m_physScale, segment.p1.y * m_physScale);
 this.graphics.lineTo(  (segment.p2.x * lambda + (1-lambda) * segment.p1.x) * m_physScale,
        (segment.p2.y * lambda + (1-lambda) * segment.p1.y) * m_physScale);
}
 
function mDown(e:MouseEvent):void
{
 MouseIsDown = true;
}
 
function mUp(e:MouseEvent):void
{
 MouseIsDown = false;
}
/*
var keyToCheck:String = "g";
 
function kDown(e:KeyboardEvent):void
{
 if (e.charCode == keyToCheck.charCodeAt(0))
 {
  m_world.SetGravity(new b2Vec2(0,10));
 }
}
 
function kUpp(e:KeyboardEvent):void
{
 if (e.charCode == keyToChek.charCodeAt(0))
 {
  m_world.SetGravity(new b2Vec2(0,0));
 }
}
*/

1 2  下一页

Tags:The engine behind

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接