Flash Box2D not detecting collisions?
EDIT: I figured out what was wrong, and this is no longer a question. The solution was a stupid error, and I had accidentally set iterations to 0. Sorry!
I'm just learning Box2D and am doing a "hello world"-esque simulation, but having a problem.
I have a crate object falling, and it's supposed to hit the ground and flatten out/bounce around, however, the crate completely ignores the floor body and continues.
I'm rendering everything using debugDraw, and havn't changed the source code. I'm using version 2.0.2.
The .swf can be found here.
My Main.as class is as follows:
// Main.as
//
//
/*
/ TODO:
/
/ *Collision not working
/
/ *Correct todo list size
/
/ *Fill empty area of todo list with useless items
/
/ *Stop writing comments at 2AM.
/
*/
package
{
import flash.display.*
import flash.events.*
import flash.display.MovieClip;
import Box2D.Collision.*
import Box2D.Collision.Shapes.*
import Box2D.Common.*
import Box2D.Common.Math.*
import Box2D.Dynamics.*
public class Main extends MovieClip
{
public static const RATIO:int = 30
private var world:b2World
private var crate:b2Body
private var timeStep:Number = 1/60
private var iterations:int = 0
public function Main()
{
createWorld()
createShapes()
addCrate()
startDebug()
stage.addEventListener(Event.ENTER_FRAME, loop)
}
private function startDebug()
{
var drawing:Sprite = new Sprite()
addChild(drawing)
var drawer:b2DebugDraw = new b2DebugDraw()
drawer.m_sprite = drawing
drawer.m_drawScale = RATIO
drawer.SetFlags(b2DebugDraw.e_shapeBit)
drawer.m_lineThickness = 2
drawer.m_fillAlpha = .8
world.SetDebugDraw(drawer)
}
private function loop(e:Event)
{
world.Step(timeStep, iterations)
//tracing
trace('X: ' + crate.GetPosition().x * RATIO + '; Y: ' + crate.GetPosition().y * RATIO)
}
private function addCrate()
{
//Add falling block and shit, IDK.
// Coords should be 230, 15.
var crateDef:b2BodyDef = new b2BodyDef()
crateDef.position.Set(230 / RATIO, -40 / RATIO)
crateDef.angle = 30
var crateShapeDef:b2PolygonDef = new b2PolygonDef()
crateShapeDef.SetAsBox(25 / RATIO ,25 / RATIO)
crateShapeDef.density = .7
crateShapeDef.friction = .4
crateShapeDef.res开发者_运维百科titution = .6
crate = world.CreateBody(crateDef)
crate.CreateShape(crateShapeDef)
crate.SetMassFromShapes()
trace('Crate added, biatch.')
stage.addEventListener(Event.ENTER_FRAME, loop)
}
private function createWorld()
{
var universeAABB:b2AABB = new b2AABB()
universeAABB.lowerBound.Set(-3000 / RATIO,-3000 / RATIO)
universeAABB.upperBound.Set(3000 / RATIO,3000 / RATIO)
var gravity:b2Vec2 = new b2Vec2(0, 15)
var ignoreSleep:Boolean = true
world = new b2World(universeAABB, gravity, ignoreSleep)
trace('World created, ' + world.GetBodyCount() + ' bodies contained within.')
}
private function createShapes()
{
var floorRectDef:b2PolygonDef = new b2PolygonDef()
floorRectDef.SetAsBox(275 / RATIO, 5 / RATIO)
floorRectDef.friction = 0.4
floorRectDef.restitution = 0.4
floorRectDef.density = 0
var floorBodyDef:b2BodyDef = new b2BodyDef()
floorBodyDef.position.Set(275 / RATIO, 395 / RATIO)
var floorBody:b2Body = world.CreateBody(floorBodyDef)
floorBody.CreateShape(floorRectDef)
floorBody.SetMassFromShapes()
var wallShapeDef:b2PolygonDef = new b2PolygonDef()
wallShapeDef.SetAsBox(5 / RATIO, 195 / RATIO)
wallShapeDef.friction = 0.4
wallShapeDef.restitution = 0.4
wallShapeDef.density = 0
var wallBodyDef:b2BodyDef = new b2BodyDef()
wallBodyDef.position.Set(5 / RATIO, 195 / RATIO)
var leftWall:b2Body = world.CreateBody(wallBodyDef)
leftWall.CreateShape(wallShapeDef)
wallBodyDef.position.Set(545 / RATIO, 195 / RATIO)
var rightWall:b2Body = world.CreateBody(wallBodyDef)
rightWall.CreateShape(wallShapeDef)
trace('Walls created, simulation now has ' + world.GetBodyCount() + ' bodies.')
}
}
}
Thanks in advance for all your help, you all have never let me down when I'm stuck on a problem.
Iterations was set to 0, I set it to 10 and it works. Derp on me.
精彩评论