Giter Site home page Giter Site logo

craftybox2d's Introduction

CraftyBox2D

It's a component that facilitates the use of the Physics Engine Box2D in Crafty. The Box2D implementation that the component use is box2dweb.


##Using CraftyBox2D First you must init the box2dWorld as follow Crafty.box2D.init(0, 10, 32, true);

The above code initialize the world with 0 for the world gravity in the x-axis, 10 for the world gravity in the y-axis 32 for the pixel-to-meter ratio, and allow the world sleep. The init method also attach the world.steep() function tho the stage "EnterFrame" event

Once the world has been initialized, you can start to add entitys with the Box2D component

// Setup floor
var floor = Crafty.e("2D, Canvas, Box2D")
	.attr({ x: 0, y: 0})
	.box2d({
		bodyType: 'static',
		shape: [
				[0, 300],
				[400, 300]
			]
	});
		
// Add simple box
var box = Crafty.e("2D, Canvas, Color, Box2D")
	 .attr({ x: 0, y: 0, w:20, h:20})
	 .color("#FF0000")
	 .box2d({
			bodyType: 'dynamic'
	 });

Above first we create the floor, set the bodyType to static, and set the collition box in the botom of the stage then we create a box with 20x20 pixels, red color and set the body type to dynamic and left the collition area to the default values. Running the game a see the box falling to the floor.

the are more options that you can set, please see the demo game code for more.

Is very recommended the reading of the Box2D manual for a review of all the options that you can use

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.