Flash Box2D 未检测到碰撞?
编辑:我弄清楚出了什么问题,这不再是一个问题。解决方案是一个愚蠢的错误,我不小心将迭代设置为 0。抱歉!
我刚刚学习 Box2D,正在进行“hello world”式的模拟,但遇到了问题。
我有一个板条箱物体掉落,它应该撞击地面并变平/弹跳,但是,板条箱完全忽略地板主体并继续。
我正在使用 debugDraw 渲染所有内容,并且没有更改源代码。我使用的是2.0.2版本。
.swf 可以在此处找到。
我的 Main.as 类如下如下:
// 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.restitution = .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.')
}
}
}
预先感谢您的所有帮助,当我遇到问题时,你们从未让我失望。
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.restitution = .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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
迭代次数设置为 0,我将其设置为 10 并且它有效。德普对我。
Iterations was set to 0, I set it to 10 and it works. Derp on me.