Hello everyone, just now I completed the testing of “box2d” and “nape-haxe4”
The testing objective is’ air ’
“lime test air”
box2d
nape-haxe4
You can see that the fps fluctuation is not very large, so it seems that the performance is similar
Hello everyone, just now I completed the testing of “box2d” and “nape-haxe4”
The testing objective is’ air ’
“lime test air”
box2d
nape-haxe4
You can see that the fps fluctuation is not very large, so it seems that the performance is similar
box2d Main.hx
package;
import openfl.text.TextFieldAutoSize;
import openfl.display.FPS;
import box2D.collision.shapes.B2CircleShape;
import box2D.collision.shapes.B2PolygonShape;
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 openfl.display.Sprite;
import openfl.display.StageAlign;
import openfl.display.StageScaleMode;
import openfl.events.Event;
class Main extends Sprite {
private static var PHYSICS_SCALE:Float = 1 / 30;
private var PhysicsDebug:Sprite;
private var World:B2World;
private var ii:Int = 0;
public function new() {
super();
var fps:FPS = new FPS(10, 10, 0xff0000);
fps.autoSize = TextFieldAutoSize.LEFT;
fps.scaleX = fps.scaleY = 2;
stage.addChild(fps);
stage.nativeWindow.width = 1280;
stage.nativeWindow.height = 960;
stage.nativeWindow.x = (1920 - 1280) / 2;
stage.nativeWindow.y = (1080 - 960) / 3.2;
stage.frameRate = 60;
World = new B2World(new B2Vec2(0, 10.0), true);
PhysicsDebug = new Sprite();
addChild(PhysicsDebug);
var debugDraw = new B2DebugDraw();
debugDraw.setSprite(PhysicsDebug);
debugDraw.setDrawScale(1 / PHYSICS_SCALE);
debugDraw.setFlags(B2DebugDraw.e_shapeBit);
World.setDebugDraw(debugDraw);
createBox(1280 / 2, 910, 1280, 30, false);
createBox(10, stage.stageHeight / 2, 30, stage.stageHeight, false);
createBox(stage.stageWidth - 10, stage.stageHeight / 2, 30, stage.stageHeight, false);
addEventListener(Event.ENTER_FRAME, this_onEnterFrame);
}
private function onMouseDown(e):Void {
createBox(Math.random() * 1280, -100, 30, 30, true);
}
private function createBox(x:Float, y:Float, width:Float, height:Float, dynamicBody:Bool):Void {
var bodyDefinition = new B2BodyDef();
bodyDefinition.position.set(x * PHYSICS_SCALE, y * PHYSICS_SCALE);
if (dynamicBody) {
bodyDefinition.type = B2Body.b2_dynamicBody;
}
var polygon = new B2PolygonShape();
polygon.setAsBox((width / 2) * PHYSICS_SCALE, (height / 2) * PHYSICS_SCALE);
var fixtureDefinition = new B2FixtureDef();
fixtureDefinition.shape = polygon;
var body = World.createBody(bodyDefinition);
body.createFixture(fixtureDefinition);
}
private function createCircle(x:Float, y:Float, radius:Float, dynamicBody:Bool):Void {
var bodyDefinition = new B2BodyDef();
bodyDefinition.position.set(x * PHYSICS_SCALE, y * PHYSICS_SCALE);
if (dynamicBody) {
bodyDefinition.type = B2Body.b2_dynamicBody;
}
var circle = new B2CircleShape(radius * PHYSICS_SCALE);
var fixtureDefinition = new B2FixtureDef();
fixtureDefinition.shape = circle;
var body = World.createBody(bodyDefinition);
body.createFixture(fixtureDefinition);
}
// Event Handlers
private function this_onEnterFrame(event:Event):Void {
World.step(1 / 30, 10, 10);
World.clearForces();
World.drawDebugData();
ii++;
if (ii < 600) {
onMouseDown(null);
}
}
}
nape Main.hx
package;
import openfl.text.TextFieldAutoSize;
import openfl.text.TextFieldType;
import openfl.display.FPS;
import nape.space.Space;
import nape.geom.Vec2;
import nape.phys.Body;
import nape.phys.BodyType;
import nape.phys.Material;
import nape.shape.Polygon;
import nape.util.ShapeDebug;
import openfl.display.Sprite;
import openfl.events.Event;
import openfl.events.MouseEvent;
import openfl.Lib;
class Main extends Sprite {
private var space:Space;
private var debug:ShapeDebug;
private var lastTime:Float;
private var arr:Array<Body> = [];
private var ii:Int = 0;
public function new() {
super();
var fps:FPS = new FPS(10, 10, 0xff0000);
fps.autoSize = TextFieldAutoSize.LEFT;
fps.scaleX = fps.scaleY = 2;
stage.addChild(fps);
stage.nativeWindow.width = 1280;
stage.nativeWindow.height = 960;
stage.nativeWindow.x = (1920 - 1280) / 2;
stage.nativeWindow.y = (1080 - 960) / 3.2;
stage.frameRate = 60;
var gravity:Vec2 = new Vec2(0, 400);
space = new Space(gravity);
var groundBody:Body = new Body(BodyType.STATIC, new Vec2(stage.stageWidth / 2, stage.stageHeight - 10));
var groundShape:Polygon = new Polygon(Polygon.box(stage.stageWidth, 30));
groundBody.shapes.add(groundShape);
groundBody.space = space;
var groundBody2:Body = new Body(BodyType.STATIC, new Vec2(10, stage.stageHeight / 2));
var groundShape2:Polygon = new Polygon(Polygon.box(30, stage.stageHeight));
groundBody2.shapes.add(groundShape2);
groundBody2.space = space;
var groundBody3:Body = new Body(BodyType.STATIC, new Vec2(stage.stageWidth - 10, stage.stageHeight / 2));
var groundShape3:Polygon = new Polygon(Polygon.box(30, stage.stageHeight));
groundBody3.shapes.add(groundShape3);
groundBody3.space = space;
debug = new ShapeDebug(stage.stageWidth, stage.stageHeight);
debug.thickness = 1;
addChild(debug.display);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
addEventListener(Event.ENTER_FRAME, function(_) {
ii++;
if (ii < 600) {
onMouseDown(null);
}
});
addEventListener(Event.ENTER_FRAME, loop);
lastTime = Lib.getTimer();
}
private function onMouseDown(e:MouseEvent):Void {
var body:Body = new Body(BodyType.DYNAMIC, new Vec2(Math.random() * stage.stageWidth, 100));
var size:Float = Math.random() * 30 + 20;
var shape:Polygon = new Polygon(Polygon.box(30, 30));
arr.push(body);
body.shapes.add(shape);
var material:Material = new Material();
material.elasticity = 0;
shape.material = material;
body.space = space;
}
private function loop(e:Event):Void {
space.step(1 / 30, 10, 10);
debug.clear();
debug.draw(space);
}
}
Is’ haxelib box2d ‘a ported version of’ as3box2d2.1a '?
Box2d and Nape perform worse than Air on the HTML5 target
So, do you prefer using “box2d” or “nape”?
I noticed a very interesting phenomenon, in my test, the “box2d” object does not rotate during collision? And the ‘nape’ object is easy to rotate, I want to know what is causing this?
Have you noticed that the performance of “box2d” and “nape” on the “html5” target is not as high as on the “air” target?
Under HTML5, it is also browser dependant.
Make sure you’re testing in an up-to-date version of Chrome / Chromium, with hardware acceleration enabled.
But yeah, different targets, different platforms, different browsers, different performance is a given.
This is a rendering bug in OpenFL. It has been fixed on GitHub, but the fix has not been included in a release yet.
I don’t use physics engines in my projects. I cannot answer this question for you.
Please try to avoid tagging me with @ in the future, unless you are replying to a post that I have already made in the same thread. I read every new post on the forum, so I won’t miss any questions that you have, and I will respond if I know the answer.