Hey, I've been working on this for a while and my head Hurts. I am a designer trying to learn to program and I keep having the same problem with setting up joints. No matter what I do, I just can't get them to work right. I am more than confident that whatever I am doing wrong is pretty obvious and I would really appreciate it if some pro would take the time to explain it to me before I throw myself in front of a bus.
here is the problem section:
And the entire project:
here is the problem section:
Code:
bodyDef = new b2BodyDef();
boxDef = new b2BoxDef();
boxDef.extents.Set(20,20);
boxDef.friction = 1;
boxDef.density = 1;
boxDef.restitution = 0.5;
bodyDef.position.Set(350,0);
bodyDef.AddShape(boxDef);
bodyDef.userData = new boxImage();
bodyDef.userData.width = boxDef.extents.x * 2;
bodyDef.userData.height = boxDef.extents.y * 1.6;
addChild(bodyDef.userData);
var tCar:b2Body = m_world.CreateBody(bodyDef);
bodyDef = new b2BodyDef();
cirDef = new b2CircleDef();
cirDef.radius = 20;
cirDef.friction = 1;
cirDef.density = 1;
cirDef.restitution = 0.4;
bodyDef.position.Set(400, 0);
bodyDef.AddShape(cirDef)
bodyDef.userData = new cirImage();
bodyDef.userData.width = cirDef.radius * 2;
bodyDef.userData.height = cirDef.radius * 2;
addChild(bodyDef.userData);
var tFront:b2Body = m_world.CreateBody(bodyDef);
jd.anchorPoint.Set( 350, 0);
jd.collideConnected = true;
jd.body1 = tCar;
jd.body2 = tFront;
m_world.CreateJoint(jd);
boxDef = new b2BoxDef();
boxDef.extents.Set(20,20);
boxDef.friction = 1;
boxDef.density = 1;
boxDef.restitution = 0.5;
bodyDef.position.Set(350,0);
bodyDef.AddShape(boxDef);
bodyDef.userData = new boxImage();
bodyDef.userData.width = boxDef.extents.x * 2;
bodyDef.userData.height = boxDef.extents.y * 1.6;
addChild(bodyDef.userData);
var tCar:b2Body = m_world.CreateBody(bodyDef);
bodyDef = new b2BodyDef();
cirDef = new b2CircleDef();
cirDef.radius = 20;
cirDef.friction = 1;
cirDef.density = 1;
cirDef.restitution = 0.4;
bodyDef.position.Set(400, 0);
bodyDef.AddShape(cirDef)
bodyDef.userData = new cirImage();
bodyDef.userData.width = cirDef.radius * 2;
bodyDef.userData.height = cirDef.radius * 2;
addChild(bodyDef.userData);
var tFront:b2Body = m_world.CreateBody(bodyDef);
jd.anchorPoint.Set( 350, 0);
jd.collideConnected = true;
jd.body1 = tCar;
jd.body2 = tFront;
m_world.CreateJoint(jd);
And the entire project:
Code:
package {
import flash.display.*;
import flash.events.*;
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.*;
public class trial extends Sprite{
var bodyDef:b2BodyDef;
var boxDef:b2BoxDef;
var cirDef:b2CircleDef;
var jd:b2RevoluteJointDef;
public var m_world:b2World;
public var m_timeStep:Number = 1/30;
public var m_iterations:int = 10;
public function trial(){
var worldAABB:b2AABB = new b2AABB();
worldAABB.minVertex.Set(0.0, 0.0);
worldAABB.maxVertex.Set(600.0, 600.0);
var gravity:b2Vec2 = new b2Vec2(0, 300.0)
var doSleep:Boolean = true;
m_world = new b2World(worldAABB, gravity, doSleep);
bodyDef = new b2BodyDef();
boxDef = new b2BoxDef();
boxDef.extents.Set(300,10);
boxDef.friction = 0.3;
bodyDef.position.Set(200,375);
bodyDef.AddShape(boxDef);
bodyDef.userData.width = boxDef.extents.x * 2;
bodyDef.userData.height = boxDef.extents.y * 2;
addChild(bodyDef.userData);
m_world.CreateBody(bodyDef);
bodyDef = new b2BodyDef();
boxDef = new b2BoxDef();
boxDef.extents.Set(20,20);
boxDef.friction = 1;
boxDef.density = 1;
boxDef.restitution = 0.5;
bodyDef.position.Set(350,0);
bodyDef.AddShape(boxDef);
bodyDef.userData = new boxImage();
bodyDef.userData.width = boxDef.extents.x * 2;
bodyDef.userData.height = boxDef.extents.y * 1.6;
addChild(bodyDef.userData);
var tCar:b2Body = m_world.CreateBody(bodyDef);
bodyDef = new b2BodyDef();
cirDef = new b2CircleDef();
cirDef.radius = 20;
cirDef.friction = 1;
cirDef.density = 1;
cirDef.restitution = 0.4;
bodyDef.position.Set(400, 0);
bodyDef.AddShape(cirDef)
bodyDef.userData = new cirImage();
bodyDef.userData.width = cirDef.radius * 2;
bodyDef.userData.height = cirDef.radius * 2;
addChild(bodyDef.userData);
var tFront:b2Body = m_world.CreateBody(bodyDef);
jd.anchorPoint.Set( 350, 0);
jd.collideConnected = true;
jd.body1 = tCar;
jd.body2 = tFront;
m_world.CreateJoint(jd);
bodyDef.position.Set(300,0);
bodyDef.AddShape(cirDef);
bodyDef.userData = new cirImage();
bodyDef.userData.width = cirDef.radius * 2;
bodyDef.userData.height = cirDef.radius * 2;
addChild(bodyDef.userData);
var back:b2Body = m_world.CreateBody(bodyDef);
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
}
public function Update(e:Event):void{
m_world.Step(m_timeStep, m_iterations);
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
if (bb.m_userData is Sprite){
bb.m_userData.x = bb.m_position.x;
bb.m_userData.y = bb.m_position.y;
bb.m_userData.rotation = bb.m_rotation * (180/Math.PI);
}
}
}
}
}
import flash.display.*;
import flash.events.*;
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.*;
public class trial extends Sprite{
var bodyDef:b2BodyDef;
var boxDef:b2BoxDef;
var cirDef:b2CircleDef;
var jd:b2RevoluteJointDef;
public var m_world:b2World;
public var m_timeStep:Number = 1/30;
public var m_iterations:int = 10;
public function trial(){
var worldAABB:b2AABB = new b2AABB();
worldAABB.minVertex.Set(0.0, 0.0);
worldAABB.maxVertex.Set(600.0, 600.0);
var gravity:b2Vec2 = new b2Vec2(0, 300.0)
var doSleep:Boolean = true;
m_world = new b2World(worldAABB, gravity, doSleep);
bodyDef = new b2BodyDef();
boxDef = new b2BoxDef();
boxDef.extents.Set(300,10);
boxDef.friction = 0.3;
bodyDef.position.Set(200,375);
bodyDef.AddShape(boxDef);
bodyDef.userData.width = boxDef.extents.x * 2;
bodyDef.userData.height = boxDef.extents.y * 2;
addChild(bodyDef.userData);
m_world.CreateBody(bodyDef);
bodyDef = new b2BodyDef();
boxDef = new b2BoxDef();
boxDef.extents.Set(20,20);
boxDef.friction = 1;
boxDef.density = 1;
boxDef.restitution = 0.5;
bodyDef.position.Set(350,0);
bodyDef.AddShape(boxDef);
bodyDef.userData = new boxImage();
bodyDef.userData.width = boxDef.extents.x * 2;
bodyDef.userData.height = boxDef.extents.y * 1.6;
addChild(bodyDef.userData);
var tCar:b2Body = m_world.CreateBody(bodyDef);
bodyDef = new b2BodyDef();
cirDef = new b2CircleDef();
cirDef.radius = 20;
cirDef.friction = 1;
cirDef.density = 1;
cirDef.restitution = 0.4;
bodyDef.position.Set(400, 0);
bodyDef.AddShape(cirDef)
bodyDef.userData = new cirImage();
bodyDef.userData.width = cirDef.radius * 2;
bodyDef.userData.height = cirDef.radius * 2;
addChild(bodyDef.userData);
var tFront:b2Body = m_world.CreateBody(bodyDef);
jd.anchorPoint.Set( 350, 0);
jd.collideConnected = true;
jd.body1 = tCar;
jd.body2 = tFront;
m_world.CreateJoint(jd);
bodyDef.position.Set(300,0);
bodyDef.AddShape(cirDef);
bodyDef.userData = new cirImage();
bodyDef.userData.width = cirDef.radius * 2;
bodyDef.userData.height = cirDef.radius * 2;
addChild(bodyDef.userData);
var back:b2Body = m_world.CreateBody(bodyDef);
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
}
public function Update(e:Event):void{
m_world.Step(m_timeStep, m_iterations);
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
if (bb.m_userData is Sprite){
bb.m_userData.x = bb.m_position.x;
bb.m_userData.y = bb.m_position.y;
bb.m_userData.rotation = bb.m_rotation * (180/Math.PI);
}
}
}
}
}