Unstable surface on a fixed polygon

Hi, i'm trying to make a landscape from fixed shapes. When I create a polygon with density of 0, bodies on top of it sink into the surface and snap back out repeatedly. This is not the case if the density > 0. Code to reproduce below


package {
import flash.display.Sprite;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.Joints.*;
import Box2D.Dynamics.Contacts.*;
import Box2D.Common.*;
import Box2D.Common.Math.*;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.*;

[SWF (frameRate="55", width="770", height="440", backgroundColor="#869ca7")]
public class Test_polygon extends Sprite{

public static var m_world:b2World;
public static var m_sprite:Sprite;
public static var m_physScale:Number = 30;
public static var m_iterations:int = 10;
public static var m_timeStep:Number = 1/30;

public function Test_polygon(){
m_sprite = this as Sprite;
addEventListener(Event.ENTER_FRAME, update);

//MAKE WORLD
var worldAABB:b2AABB = new b2AABB();
worldAABB.minVertex.Set(-1000, -1000);
worldAABB.maxVertex.Set(1000, 1000);
var gravity:b2Vec2 = new b2Vec2(0, 10);
var doSleep:Boolean = true;
m_world = new b2World(worldAABB, gravity, doSleep);
//

//Make bodies
var bodyDefP:b2BodyDef = new b2BodyDef();
var polyDef:b2PolyDef = new b2PolyDef();

polyDef.vertexCount = 4;
polyDef.vertices[0].Set(0 / m_physScale, -70 / m_physScale);
polyDef.vertices[1].Set( 0 / m_physScale, 0 / m_physScale);
polyDef.vertices[2].Set(-500 / m_physScale, 0 / m_physScale);
polyDef.vertices[3].Set(-200 / m_physScale, -70 / m_physScale);

//Polygon density
polyDef.density = 0;

bodyDefP.position.Set(600 / m_physScale, 260 / m_physScale);
bodyDefP.AddShape(polyDef);
m_world.CreateBody(bodyDefP);

var bodyDef:b2BodyDef = new b2BodyDef();
var boxDef:b2BoxDef = new b2BoxDef();

boxDef.density = 1.0;
boxDef.extents.Set(20 / m_physScale, 20 / m_physScale);
bodyDef.position.Set(500 / m_physScale, 50 / m_physScale);
bodyDef.AddShape(boxDef);
m_world.CreateBody(bodyDef);


var platformDef:b2BodyDef = new b2BodyDef();
var platformBoxDef:b2BoxDef = new b2BoxDef();

platformBoxDef.density = 0;
platformBoxDef.extents.Set(300 / m_physScale, 10 / m_physScale);
platformDef.position.Set(350 / m_physScale, 270 / m_physScale);
platformDef.AddShape(platformBoxDef);
m_world.CreateBody(platformDef);

}

public function update(e:Event):void{
m_sprite.graphics.clear();
m_world.Step(m_timeStep, m_iterations);

for (var jj:b2Joint = m_world.m_jointList; jj; jj = jj.m_next){
DrawJoint(jj);
}

for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
for (var s:b2Shape = bb.GetShapeList(); s != null; s = s.GetNext()){
DrawShape(s);
}
}

}

public function DrawShape(shape:b2Shape):void{
switch (shape.m_type)
{
case b2Shape.e_circleShape:
{
var circle:b2CircleShape = shape as b2CircleShape;
var pos:b2Vec2 = circle.m_position;
var r:Number = circle.m_radius;
var k_segments:Number = 16.0;
var k_increment:Number = 2.0 * Math.PI / k_segments;
m_sprite.graphics.lineStyle(1,0xffffff,1);
m_sprite.graphics.moveTo((pos.x + r) * m_physScale, (pos.y) * m_physScale);
var theta:Number = 0.0;

for (var i:int = 0; i < k_segments; ++i)
{
var d:b2Vec2 = new b2Vec2(r * Math.cos(theta), r * Math.sin(theta));
var v:b2Vec2 = b2Math.AddVV(pos , d);
m_sprite.graphics.lineTo((v.x) * m_physScale, (v.y) * m_physScale);
theta += k_increment;
}
m_sprite.graphics.lineTo((pos.x + r) * m_physScale, (pos.y) * m_physScale);

m_sprite.graphics.moveTo((pos.x) * m_physScale, (pos.y) * m_physScale);
var ax:b2Vec2 = circle.m_R.col1;
var pos2:b2Vec2 = new b2Vec2(pos.x + r * ax.x, pos.y + r * ax.y);
m_sprite.graphics.lineTo((pos2.x) * m_physScale, (pos2.y) * m_physScale);
}
break;
case b2Shape.e_polyShape:
{
var poly:b2PolyShape = shape as b2PolyShape;
var tV:b2Vec2 = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[i]));
m_sprite.graphics.lineStyle(1,0xffffff,1);
m_sprite.graphics.moveTo(tV.x * m_physScale, tV.y * m_physScale);

for (i = 0; i < poly.m_vertexCount; ++i)
{
v = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[i]));
m_sprite.graphics.lineTo(v.x * m_physScale, v.y * m_physScale);
}
m_sprite.graphics.lineTo(tV.x * m_physScale, tV.y * m_physScale);
}
break;
}
}

public function DrawJoint(joint:b2Joint):void
{
var b1:b2Body = joint.m_body1;
var b2:b2Body = joint.m_body2;

var x1:b2Vec2 = b1.m_position;
var x2:b2Vec2 = b2.m_position;
var p1:b2Vec2 = joint.GetAnchor1();
var p2:b2Vec2 = joint.GetAnchor2();

m_sprite.graphics.lineStyle(1,0x44aaff,1/1);

switch (joint.m_type)
{
case b2Joint.e_distanceJoint:
case b2Joint.e_mouseJoint:
m_sprite.graphics.moveTo(p1.x * m_physScale, p1.y * m_physScale);
m_sprite.graphics.lineTo(p2.x * m_physScale, p2.y * m_physScale);
break;

case b2Joint.e_pulleyJoint:
var pulley:b2PulleyJoint = joint as b2PulleyJoint;
var s1:b2Vec2 = pulley.GetGroundPoint1();
var s2:b2Vec2 = pulley.GetGroundPoint2();
m_sprite.graphics.moveTo(s1.x * m_physScale, s1.y * m_physScale);
m_sprite.graphics.lineTo(p1.x * m_physScale, p1.y * m_physScale);
m_sprite.graphics.moveTo(s2.x * m_physScale, s2.y * m_physScale);
m_sprite.graphics.lineTo(p2.x * m_physScale, p2.y * m_physScale);
break;

default:
if (b1 == m_world.m_groundBody){
m_sprite.graphics.moveTo(p1.x * m_physScale, p1.y * m_physScale);
m_sprite.graphics.lineTo(x2.x * m_physScale, x2.y * m_physScale);
}
else if (b2 == m_world.m_groundBody){
m_sprite.graphics.moveTo(p1.x * m_physScale, p1.y * m_physScale);
m_sprite.graphics.lineTo(x1.x * m_physScale, x1.y * m_physScale);
}
else{
m_sprite.graphics.moveTo(x1.x * m_physScale, x1.y * m_physScale);
m_sprite.graphics.lineTo(p1.x * m_physScale, p1.y * m_physScale);
m_sprite.graphics.lineTo(x2.x * m_physScale, x2.y * m_physScale);
m_sprite.graphics.lineTo(p2.x * m_physScale, p2.y * m_physScale);
}
}
}

}
}
Hey robC,

This is caused by the same problem posted in this thread.

You can fix it by either updating from SVN, or downloading this fix.
http://skatehed.ho8.com/flash9/143/Box2DFlashAS3_1.4.3_Fix.zip

See fixed swf here:
http://skatehed.ho8.com/flash9/143/PhysTestBug2.html

All the best,
-Matt
Awesome thanks a lot Matt 🙂

Original thread: https://box2d.org/forum/viewtopic.php?t=261