Hi,
I have quite a lot of programming experience (actionscript and other languages), but I know nothing about physics engines, other than what they do (well, I dabbled with NEWTON a while ago but that's it).
I'm in the process of making an topdown AS3 game. I implemented a collision system, and it all works, complete with sliding collision with diagonal walls, but I'm not entirely happy with the code, and there are some minor problems I can't seem to fix.
So, now I'm assessing whether or not it would be a good idea to try to swap my collision system with Box2d. So, in order to do that, I tried to modify the HelloWorld sample a bit. What I'm trying to achieve is a top down world (ie no gravity), with one circle moveable with the keys, a box that isn't moveable, and another circle that you can push out of the way with the first circle.
It kinda works, but the movement is very odd. It takes forever to get to speed, I don't know how to cap the speed, and I don't know how to make the circle slow down again (should happen pretty fast) once you release the key. Also, I'm not sure how to set a box to unmoveable. Also, I had to use very high values in the ApplyForce vectors in order to see any movement.
Here's what I have so far. It's messy, but I'm just trying to figure out how it works. If anyone could help me out, I'd be very grateful.
You can just replace the standard HelloWorld.as file's contents with this :
You'll need this in a seperate file :
Thanks, and sorry for my newbishness. It's probably very simple stuff, and I am willing to learn, but I'm not even sure yet if box2d would be a good choice for my game - that's what I'm trying to figure out.
If y'all prefer a zip with the classes, I can do that.
I have quite a lot of programming experience (actionscript and other languages), but I know nothing about physics engines, other than what they do (well, I dabbled with NEWTON a while ago but that's it).
I'm in the process of making an topdown AS3 game. I implemented a collision system, and it all works, complete with sliding collision with diagonal walls, but I'm not entirely happy with the code, and there are some minor problems I can't seem to fix.
So, now I'm assessing whether or not it would be a good idea to try to swap my collision system with Box2d. So, in order to do that, I tried to modify the HelloWorld sample a bit. What I'm trying to achieve is a top down world (ie no gravity), with one circle moveable with the keys, a box that isn't moveable, and another circle that you can push out of the way with the first circle.
It kinda works, but the movement is very odd. It takes forever to get to speed, I don't know how to cap the speed, and I don't know how to make the circle slow down again (should happen pretty fast) once you release the key. Also, I'm not sure how to set a box to unmoveable. Also, I had to use very high values in the ApplyForce vectors in order to see any movement.
Here's what I have so far. It's messy, but I'm just trying to figure out how it works. If anyone could help me out, I'd be very grateful.
You can just replace the standard HelloWorld.as file's contents with this :
Code:
package{
import flash.display.Sprite;
import flash.events.Event;
// Classes used in this example
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
public class HelloWorld extends Sprite{
var b:b2Body;
public function HelloWorld(){
Key.init(this.stage);
// Add event for main loop
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
// Creat world AABB
var worldAABB:b2AABB = new b2AABB();
worldAABB.minVertex.Set(-1000.0, -1000.0);
worldAABB.maxVertex.Set(1000.0, 1000.0);
// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2(0.0, 0.0);
// Allow bodies to sleep
var doSleep:Boolean = false;
// Construct a world object
m_world = new b2World(worldAABB, gravity, doSleep);
// Vars used to create bodies
var bodyDef:b2BodyDef;
var boxDef:b2BoxDef;
var circleDef:b2CircleDef;
// Add ground body
bodyDef = new b2BodyDef();
boxDef = new b2BoxDef();
boxDef.extents.Set(1000, 100);
boxDef.friction = 0.3;
bodyDef.position.Set(320, 400);
bodyDef.AddShape(boxDef);
// Add sprite to body userData
bodyDef.userData = new PhysGround();
bodyDef.userData.width = 200 * 2;
bodyDef.userData.height = boxDef.extents.y * 2;
addChild(bodyDef.userData);
m_world.CreateBody(bodyDef);
// Add a box
bodyDef = new b2BodyDef();
boxDef = new b2BoxDef();
boxDef.extents.Set(Math.random() * 15 + 10, Math.random() * 15 + 10);
boxDef.density = 80000.0;
boxDef.friction = 0.5;
boxDef.restitution = 300;
bodyDef.AddShape(boxDef);
bodyDef.userData = new PhysBox();
bodyDef.userData.width = boxDef.extents.x * 2;
bodyDef.userData.height = boxDef.extents.y * 2;
bodyDef.position.x = Math.random() * 400 + 120;
bodyDef.position.y = Math.random() * 100 + 50;
m_world.CreateBody(bodyDef);
addChild(bodyDef.userData);
// Add a circle (moveable)
bodyDef = new b2BodyDef();
circleDef = new b2CircleDef();
circleDef.radius = Math.random() * 15 + 10;
circleDef.density = 1.0;
circleDef.friction = 0.5;
circleDef.restitution = 0.2
bodyDef.AddShape(circleDef);
bodyDef.userData = new PhysCircle();
bodyDef.userData.width = circleDef.radius * 2;
bodyDef.userData.height = circleDef.radius * 2;
bodyDef.position.x = Math.random() * 400 + 120;
bodyDef.position.y = Math.random() * 100 + 50;
this.b = m_world.CreateBody(bodyDef);
addChild(bodyDef.userData);
// Add another circle
bodyDef = new b2BodyDef();
circleDef = new b2CircleDef();
circleDef.radius = Math.random() * 15 + 10;
circleDef.density = 1.0;
circleDef.friction = 0.5;
circleDef.restitution = 0.2
bodyDef.AddShape(circleDef);
bodyDef.userData = new PhysCircle();
bodyDef.userData.width = circleDef.radius * 2;
bodyDef.userData.height = circleDef.radius * 2;
bodyDef.position.x = Math.random() * 400 + 120;
bodyDef.position.y = Math.random() * 100 + 50;
m_world.CreateBody(bodyDef);
addChild(bodyDef.userData);
}
public function Update(e:Event):void{
//this.b.ApplyForce(new b2Vec2(100, 0), new b2Vec2(100, 0));
if(Key.isDown(38)) {
b.ApplyForce(new b2Vec2(this.b.m_position.x, this.b.m_position.y - 10000), new b2Vec2(this.b.m_position.x, this.b.m_position.y));
}
if(Key.isDown(40)) {
b.ApplyForce(new b2Vec2(this.b.m_position.x, this.b.m_position.y + 10000), new b2Vec2(this.b.m_position.x, this.b.m_position.y));
}
if(Key.isDown(37)) {
b.ApplyForce(new b2Vec2(this.b.m_position.x - 10000, this.b.m_position.y), new b2Vec2(this.b.m_position.x, this.b.m_position.y));
}
if(Key.isDown(39)) {
b.ApplyForce(new b2Vec2(this.b.m_position.x + 10000, this.b.m_position.y), new b2Vec2(this.b.m_position.x, this.b.m_position.y));
}
m_world.Step(m_timeStep, m_iterations);
// Go through body list and update sprite positions/rotations
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);
}
}
}
public var m_world:b2World;
public var m_iterations:int = 10;
public var m_timeStep:Number = 1/30;
}
}
import flash.display.Sprite;
import flash.events.Event;
// Classes used in this example
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
public class HelloWorld extends Sprite{
var b:b2Body;
public function HelloWorld(){
Key.init(this.stage);
// Add event for main loop
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
// Creat world AABB
var worldAABB:b2AABB = new b2AABB();
worldAABB.minVertex.Set(-1000.0, -1000.0);
worldAABB.maxVertex.Set(1000.0, 1000.0);
// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2(0.0, 0.0);
// Allow bodies to sleep
var doSleep:Boolean = false;
// Construct a world object
m_world = new b2World(worldAABB, gravity, doSleep);
// Vars used to create bodies
var bodyDef:b2BodyDef;
var boxDef:b2BoxDef;
var circleDef:b2CircleDef;
// Add ground body
bodyDef = new b2BodyDef();
boxDef = new b2BoxDef();
boxDef.extents.Set(1000, 100);
boxDef.friction = 0.3;
bodyDef.position.Set(320, 400);
bodyDef.AddShape(boxDef);
// Add sprite to body userData
bodyDef.userData = new PhysGround();
bodyDef.userData.width = 200 * 2;
bodyDef.userData.height = boxDef.extents.y * 2;
addChild(bodyDef.userData);
m_world.CreateBody(bodyDef);
// Add a box
bodyDef = new b2BodyDef();
boxDef = new b2BoxDef();
boxDef.extents.Set(Math.random() * 15 + 10, Math.random() * 15 + 10);
boxDef.density = 80000.0;
boxDef.friction = 0.5;
boxDef.restitution = 300;
bodyDef.AddShape(boxDef);
bodyDef.userData = new PhysBox();
bodyDef.userData.width = boxDef.extents.x * 2;
bodyDef.userData.height = boxDef.extents.y * 2;
bodyDef.position.x = Math.random() * 400 + 120;
bodyDef.position.y = Math.random() * 100 + 50;
m_world.CreateBody(bodyDef);
addChild(bodyDef.userData);
// Add a circle (moveable)
bodyDef = new b2BodyDef();
circleDef = new b2CircleDef();
circleDef.radius = Math.random() * 15 + 10;
circleDef.density = 1.0;
circleDef.friction = 0.5;
circleDef.restitution = 0.2
bodyDef.AddShape(circleDef);
bodyDef.userData = new PhysCircle();
bodyDef.userData.width = circleDef.radius * 2;
bodyDef.userData.height = circleDef.radius * 2;
bodyDef.position.x = Math.random() * 400 + 120;
bodyDef.position.y = Math.random() * 100 + 50;
this.b = m_world.CreateBody(bodyDef);
addChild(bodyDef.userData);
// Add another circle
bodyDef = new b2BodyDef();
circleDef = new b2CircleDef();
circleDef.radius = Math.random() * 15 + 10;
circleDef.density = 1.0;
circleDef.friction = 0.5;
circleDef.restitution = 0.2
bodyDef.AddShape(circleDef);
bodyDef.userData = new PhysCircle();
bodyDef.userData.width = circleDef.radius * 2;
bodyDef.userData.height = circleDef.radius * 2;
bodyDef.position.x = Math.random() * 400 + 120;
bodyDef.position.y = Math.random() * 100 + 50;
m_world.CreateBody(bodyDef);
addChild(bodyDef.userData);
}
public function Update(e:Event):void{
//this.b.ApplyForce(new b2Vec2(100, 0), new b2Vec2(100, 0));
if(Key.isDown(38)) {
b.ApplyForce(new b2Vec2(this.b.m_position.x, this.b.m_position.y - 10000), new b2Vec2(this.b.m_position.x, this.b.m_position.y));
}
if(Key.isDown(40)) {
b.ApplyForce(new b2Vec2(this.b.m_position.x, this.b.m_position.y + 10000), new b2Vec2(this.b.m_position.x, this.b.m_position.y));
}
if(Key.isDown(37)) {
b.ApplyForce(new b2Vec2(this.b.m_position.x - 10000, this.b.m_position.y), new b2Vec2(this.b.m_position.x, this.b.m_position.y));
}
if(Key.isDown(39)) {
b.ApplyForce(new b2Vec2(this.b.m_position.x + 10000, this.b.m_position.y), new b2Vec2(this.b.m_position.x, this.b.m_position.y));
}
m_world.Step(m_timeStep, m_iterations);
// Go through body list and update sprite positions/rotations
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);
}
}
}
public var m_world:b2World;
public var m_iterations:int = 10;
public var m_timeStep:Number = 1/30;
}
}
You'll need this in a seperate file :
Code:
package {
import flash.display.Stage;
import flash.events.ContextMenuEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.KeyboardEvent;
import flash.ui.ContextMenu;
/**
* ...
* @author Sven Magnus
*/
public class Key {
public static var stage:Stage;
private static var keys:Object = { };
public static function init(stage:Stage):void {
Key.stage = stage;
Key.addEventListener(KeyboardEvent.KEY_UP, Key.keyUp);
Key.addEventListener(KeyboardEvent.KEY_DOWN, Key.keyDown);
Key.addEventListener(Event.DEACTIVATE, Key.clearKeys);
/*var myContextMenu:ContextMenu = new ContextMenu();
myContextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, Key.clearKeys);*/
}
public static function addEventListener(type:String,func:Function):void {
Key.stage.addEventListener(type, func, false, 0, true);
}
public static function removeEventListener(type:String, func:Function):void {
Key.stage.focus = Key.stage;
Key.stage.removeEventListener(type, func);
}
private static function keyUp(e:KeyboardEvent):void {
if (e.keyCode in Key.keys) {
delete Key.keys[e.keyCode];
}
}
private static function keyDown(e:KeyboardEvent):void {
Key.keys[e.keyCode] = true;
}
public static function isDown(keyCode:int):Boolean {
if (Key.keys[keyCode]) return true;
return false;
}
private static function clearKeys(e:Event):void {
Key.keys = { };
}
}
}
import flash.display.Stage;
import flash.events.ContextMenuEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.KeyboardEvent;
import flash.ui.ContextMenu;
/**
* ...
* @author Sven Magnus
*/
public class Key {
public static var stage:Stage;
private static var keys:Object = { };
public static function init(stage:Stage):void {
Key.stage = stage;
Key.addEventListener(KeyboardEvent.KEY_UP, Key.keyUp);
Key.addEventListener(KeyboardEvent.KEY_DOWN, Key.keyDown);
Key.addEventListener(Event.DEACTIVATE, Key.clearKeys);
/*var myContextMenu:ContextMenu = new ContextMenu();
myContextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, Key.clearKeys);*/
}
public static function addEventListener(type:String,func:Function):void {
Key.stage.addEventListener(type, func, false, 0, true);
}
public static function removeEventListener(type:String, func:Function):void {
Key.stage.focus = Key.stage;
Key.stage.removeEventListener(type, func);
}
private static function keyUp(e:KeyboardEvent):void {
if (e.keyCode in Key.keys) {
delete Key.keys[e.keyCode];
}
}
private static function keyDown(e:KeyboardEvent):void {
Key.keys[e.keyCode] = true;
}
public static function isDown(keyCode:int):Boolean {
if (Key.keys[keyCode]) return true;
return false;
}
private static function clearKeys(e:Event):void {
Key.keys = { };
}
}
}
Thanks, and sorry for my newbishness. It's probably very simple stuff, and I am willing to learn, but I'm not even sure yet if box2d would be a good choice for my game - that's what I'm trying to figure out.
If y'all prefer a zip with the classes, I can do that.