Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by JackImpact

Hi All,

Anyone know to create a circle shape entity using box 2d? I was playing with box 2d in impactjs and managed to put a circle-shaped entity in my game, however the circle cannot be seen even all the collision is there. It is just like an invisible ghost.

My codes are as below
in Main.js

ig.module( 
	'game.main' 
)
.requires(
	'impact.game',
	'impact.font',
	//'impact.debug.debug',
	
	'game.entities.player',
	'game.entities.crate',
	'game.entities.stone',
	'game.entities.circleA',
	'game.levels.test',
	
	'plugins.box2d.game'
)
.defines(function(){

MyGame = ig.Box2DGame.extend({
	
	gravity: 100, // All entities are affected by this
	
	// Load a font
	font: new ig.Font( 'media/04b03.font.png' ),
	clearColor: '#1b2026',
	
	init: function() {
		// Bind keys
		ig.input.bind( ig.KEY.LEFT_ARROW, 'left' );
		ig.input.bind( ig.KEY.RIGHT_ARROW, 'right' );
		ig.input.bind( ig.KEY.X, 'jump' );
		ig.input.bind( ig.KEY.C, 'shoot' );
		
		if( ig.ua.mobile ) {
			ig.input.bindTouch( '#buttonLeft', 'left' );
			ig.input.bindTouch( '#buttonRight', 'right' );
			ig.input.bindTouch( '#buttonShoot', 'shoot' );
			ig.input.bindTouch( '#buttonJump', 'jump' );
		}
		
		// Load the LevelTest as required above ('game.level.test')
		this.loadLevel( LevelTest );
	},
	
	loadLevel: function( data ) {
		this.parent( data );
		for( var i = 0; i < this.backgroundMaps.length; i++ ) {
			this.backgroundMaps[i].preRender = true;
		}
	},
	
	update: function() {		
		// Update all entities and BackgroundMaps
		this.parent();
		if(ig.game.getEntitiesByType(EntityCircleA).length==0){
			ig.game.spawnEntity( EntityCircleA, 20, 100, {shape:'circle'} );
			}
		// screen follows the player
		var player = this.getEntitiesByType( EntityPlayer )[0];
		if( player ) {
			this.screen.x = player.pos.x - ig.system.width/2;
			this.screen.y = player.pos.y - ig.system.height/2;
		}
	},
	
	draw: function() {
		// Draw all entities and BackgroundMaps
		this.parent();
		
		if( !ig.ua.mobile ) {
			this.font.draw( 'Arrow Keys, X, C', 2, 2 );
		}
	}
});

in circleA.js
ig.module(
	'game.entities.circleA'
)
.requires(
	'plugins.box2d.entity'
)
.defines(function(){

EntityCircleA = ig.Box2DEntity.extend({
	size: {x: 50, y: 50},
	
	type: ig.Entity.TYPE.B,
	checkAgainst: ig.Entity.TYPE.NONE,
	collides: ig.Entity.COLLIDES.NEVER,
	
	animSheet: new ig.AnimationSheet( 'media/ball.png', 50, 50 ),
	
	init: function( x, y, settings ) {
		//this.addAnim( 'idle', 1, [0] );
		this.parent( x, y, settings );
	}
});


});


in entity.js

ig.module( 
	'plugins.box2d.entity'
)
.requires(
	'impact.entity',	
	'plugins.box2d.game'
)
.defines(function(){


ig.Box2DEntity = ig.Entity.extend({
	body: null,
	angle: 0,
	
	init: function( x, y , settings ) {
		this.parent( x, y, settings );
		
		// Only create a box2d body when we are not in Weltmeister
		if( !ig.global.wm ) { 
			if(settings.shape=='circle'){
				this.createCircleBody();
				}
			else{
				this.createBody();
				}
			
		}
	},
	
	createBody: function() {
		var bodyDef = new b2.BodyDef();
		bodyDef.position.Set(
			(this.pos.x + this.size.x / 2) * b2.SCALE,
			(this.pos.y + this.size.y / 2) * b2.SCALE
		);
		
		this.body = ig.world.CreateBody(bodyDef);
		
		var shapeDef = new b2.PolygonDef();
		shapeDef.SetAsBox(
			this.size.x / 2 * b2.SCALE,
			this.size.y / 2 * b2.SCALE
		);
		
		shapeDef.density = 1;
		//shapeDef.restitution = 0.0;
		//shapeDef.friction = 0.9;
		this.body.CreateShape(shapeDef);
		this.body.SetMassFromShapes();
	},
	
	createCircleBody: function() {
		
		 var bodyDef = new b2.BodyDef();
		 bodyDef.position.Set(
			(this.pos.x + this.size.x / 2) * b2.SCALE,
			(this.pos.y + this.size.y / 2) * b2.SCALE
		);
		
		this.body = ig.world.CreateBody(bodyDef);
	
		var ballSd = new b2.CircleDef();
		
		ballSd.density = 1.0;
		ballSd.radius = 5;
		ballSd.restitution = 0.2;
        this.body.CreateShape(ballSd);
		//this.body.SetMassFromShapes();
	},
	
	update: function() {		
		var p = this.body.GetPosition();
		this.pos = {
			x: (p.x / b2.SCALE - this.size.x / 2),
			y: (p.y / b2.SCALE - this.size.y / 2 )
		};
		this.angle = this.body.GetAngle().round(2);
		
		if( this.currentAnim ) {
			this.currentAnim.update();
			this.currentAnim.angle = this.angle;
		}
	},
	
	kill: function() {
		ig.world.DestroyBody( this.body );
		this.parent();
	}
});
	
});

Your help is very appriciated and I think Dominic should put the box 2d API for impactjs in the doc.

Thanks in advance!

1 decade ago by quidmonkey

Which version of box2d are you using?

1 decade ago by mimik

I faced the same problem.
Nice work with the createCircleBody function.
One small problem
I think you should un-comment the line
//this.body.SetMassFromShapes();

if I remove the SetMassFromShapes my entities just wont react to gravity.

Another tips is so active the box2d debugger so you can see whats going on.

1.in your game class add as you would any other plugin
'plugins.box2d.debug'

2.Then in your init code somewhere after you load a level
add:
this.debugDrawer = new ig.Box2DDebug( ig.world );

3.And then finally in your draw function add
this.debugDrawer.draw();

Now you can see all your box2d entities and shapes!

1 decade ago by JackImpact

Quote from quidmonkey
Which version of box2d are you using?


Hi Quidmonkey,
Cannot find the version of the box 2d in the source codes which I downloaded from my personal page in this site. it only says '

/*
* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
* Ported to to javascript by Jonas Wagner, 2010
*
* Some modifications for Impact by Dominic Szablewski, 2011
*
*/

will this give you a hint?

1 decade ago by JackImpact

Hi Mimik,

Many thanks for your advice! It works, now I can see the ball in the game. All the collision is correct, amazing!

Jack

1 decade ago by JackImpact

still working on how to attach the ball graphic to the circle body...any help will be appreciated.

Cheers!

Jack

1 decade ago by JackImpact

found the solution to the problem!

Step 1: in the init function of circleA add this.addAnim( 'idle', 1, [0] );
Step 2: in the createCircleBody function in entity.js change the ballSd.radius from 5 to 2.5.

Note: the way to get the correct radius is the size of the ball image(50 by 50 square) divided by 20.

Then you will get a perfect ball in the game.

Cheers!

Jack
Page 1 of 1
« first « previous next › last »