Hello everyone,
i am pretty new to Box2D and trying to understand what everything does and i have two problems i cant fix on my own. This one is about two circles that are lying on each other and i am rotating the lower one. With my understanding the other one should rotate, too then. Maybe someone could help me with my code.
i am pretty new to Box2D and trying to understand what everything does and i have two problems i cant fix on my own. This one is about two circles that are lying on each other and i am rotating the lower one. With my understanding the other one should rotate, too then. Maybe someone could help me with my code.
Code:
package shapetest {
import Box2D.Collision.Shapes.b2CircleShape;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2DebugDraw;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2World;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Main extends Sprite {
private var _world:b2World;
private var _worldScale:int = 30;
private var _shape1:b2Body;
private var _shape2:b2Body;
private function debugDraw():void {
var debug_draw:b2DebugDraw = new b2DebugDraw();
var debug_sprite:Sprite = new Sprite();
addChild(debug_sprite);
debug_draw.SetSprite(debug_sprite);
debug_draw.SetDrawScale(this._worldScale);
debug_draw.SetFillAlpha(0.5);
debug_draw.SetFlags(b2DebugDraw.e_shapeBit);
this._world.SetDebugDraw(debug_draw);
}
private function update(evt:Event):void {
this._world.Step(1/30,10,10);
this._world.ClearForces();
this._world.DrawDebugData();
}
private function onTimerEvent(evt:TimerEvent):void {
this._shape2.SetAngle( this._shape2.GetAngle() + 0.1 );
}
public function Main() {
this._world = new b2World( new b2Vec2(0.0, 10.0), true );
var my_body:b2BodyDef;
var my_circle:b2CircleShape;
var my_fixture:b2FixtureDef;
// shape1
my_body = new b2BodyDef();
my_body.position.Set( 275/this._worldScale, 70/this._worldScale );
my_body.type = b2Body.b2_dynamicBody;
my_circle = new b2CircleShape( 40/_worldScale );
my_fixture = new b2FixtureDef();
my_fixture.shape=my_circle;
my_fixture.friction = 1;
my_fixture.density = 0.5;
my_fixture.restitution = 0.3;
this._shape1 = this._world.CreateBody( my_body );
this._shape1.CreateFixture( my_fixture );
this._shape1.SetSleepingAllowed( false );
// shape2
my_body = new b2BodyDef();
my_body.position.Set( 275/this._worldScale, 200/this._worldScale );
my_body.type = b2Body.b2_staticBody;
my_circle = new b2CircleShape( 80/_worldScale );
my_fixture = new b2FixtureDef();
my_fixture.shape=my_circle;
my_fixture.friction = 1;
my_fixture.density = 0.5;
my_fixture.restitution = 0.3;
this._shape2 = this._world.CreateBody( my_body );
this._shape2.CreateFixture( my_fixture );
var timer:Timer = new Timer(50);
timer.addEventListener( TimerEvent.TIMER, this.onTimerEvent );
timer.start();
this.debugDraw();
this.addEventListener(Event.ENTER_FRAME, this.update);
}
}
}
import Box2D.Collision.Shapes.b2CircleShape;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2DebugDraw;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2World;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Main extends Sprite {
private var _world:b2World;
private var _worldScale:int = 30;
private var _shape1:b2Body;
private var _shape2:b2Body;
private function debugDraw():void {
var debug_draw:b2DebugDraw = new b2DebugDraw();
var debug_sprite:Sprite = new Sprite();
addChild(debug_sprite);
debug_draw.SetSprite(debug_sprite);
debug_draw.SetDrawScale(this._worldScale);
debug_draw.SetFillAlpha(0.5);
debug_draw.SetFlags(b2DebugDraw.e_shapeBit);
this._world.SetDebugDraw(debug_draw);
}
private function update(evt:Event):void {
this._world.Step(1/30,10,10);
this._world.ClearForces();
this._world.DrawDebugData();
}
private function onTimerEvent(evt:TimerEvent):void {
this._shape2.SetAngle( this._shape2.GetAngle() + 0.1 );
}
public function Main() {
this._world = new b2World( new b2Vec2(0.0, 10.0), true );
var my_body:b2BodyDef;
var my_circle:b2CircleShape;
var my_fixture:b2FixtureDef;
// shape1
my_body = new b2BodyDef();
my_body.position.Set( 275/this._worldScale, 70/this._worldScale );
my_body.type = b2Body.b2_dynamicBody;
my_circle = new b2CircleShape( 40/_worldScale );
my_fixture = new b2FixtureDef();
my_fixture.shape=my_circle;
my_fixture.friction = 1;
my_fixture.density = 0.5;
my_fixture.restitution = 0.3;
this._shape1 = this._world.CreateBody( my_body );
this._shape1.CreateFixture( my_fixture );
this._shape1.SetSleepingAllowed( false );
// shape2
my_body = new b2BodyDef();
my_body.position.Set( 275/this._worldScale, 200/this._worldScale );
my_body.type = b2Body.b2_staticBody;
my_circle = new b2CircleShape( 80/_worldScale );
my_fixture = new b2FixtureDef();
my_fixture.shape=my_circle;
my_fixture.friction = 1;
my_fixture.density = 0.5;
my_fixture.restitution = 0.3;
this._shape2 = this._world.CreateBody( my_body );
this._shape2.CreateFixture( my_fixture );
var timer:Timer = new Timer(50);
timer.addEventListener( TimerEvent.TIMER, this.onTimerEvent );
timer.start();
this.debugDraw();
this.addEventListener(Event.ENTER_FRAME, this.update);
}
}
}