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 Richard

I'm trying to mix different entity types. I don't really have a reason to make everything in my game a Box2D object, as there's no real physics I want to apply to the player for example. Plus I've got most of it working without Box2D and I don't want to change it all now! :)

I'm wondering if there's an easy way to essentially make my player (or any other entity I fancy) essentially just a solid block from the Box2D engine POV. So that my Box2D crate would just bounce off it, as it would with the background tiles?

1 decade ago by Arantor

No, you'd pretty much have to make it a Box2D entity. Box2D does all its own collisions and AFAIK it also even converts the collision map to make that work, and expressly ignores any other entity collision (or it should)

If you've got most of it working without Box2D can you not implement the remainder without Box2D in some fashion?

1 decade ago by Richard

What about creating a Box2D rectangular body over the top of my player entity, and then changing it's position whenever my player entity moves?

I tried to get something like that working last night but couldn't quite manage it. But looking through the box2d code it looks like it does something similar when it creates the collision map.

I've looked at the createBody functions in the Box2D entity code, but couldn't work out how to move it.

1 decade ago by Arantor

To move a body in Box2D, you use the ApplyForce method (see the physics demo for a good example of this)

I really wouldn't, if possible, get into the realms of trying to maintain two separate entities for this. Either do it totally in Box2D or totally outside.

1 decade ago by quidmonkey

Sure you can do it. You simply have to remember that ig.Entities won't interact with ig.Box2dEntities.

1 decade ago by Arantor

Yes you can use both - but they won't interact properly, which is the underlying problem! You can't make some entities into ig.Entities and still have them interact with Box2dEntities without them being Box2DEntities...

Which means either you create both and get into the treadmill of making sure that all state changes are synced at all times, or you make everything one kind or the other if you want them to interact.

1 decade ago by alexandre

@Richard

You could create a kinematic body as a proxy of your player entity, moving it with the SetTransform method at every update so as to match the position of its employer. Dynamic b2bodies will collide with it but static and other kinematic bodies won't.

1 decade ago by alexandre

note that the crates you mention would indeed bounce off it, but it would not bounce off any box2d objects, since kinematic bodies behave as though they have infinite mass.

1 decade ago by Richard

I created a body of rectangular shape and then update it with SetXForm on each draw of my player.

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;
	this.body.CreateShape(shapeDef);
	this.body.SetMassFromShapes();
},		

And then in the update...

this.body.SetXForm(new b2.Vec2( (this.pos.x + this.size.x / 2) * b2.SCALE, (this.pos.y + this.size.y / 2) * b2.SCALE ), 0);

It almost works. If I apply a force to the crates (a real Box2D entity) it bounces off my player correctly. But when I just move my player into the crates, they move, but I pass through half of them before they do. And moving behind them causes them to move almost correctly too, but as if the collision isn't getting applied quick enough. It's hard to explain.

1 decade ago by alexandre

You're experiencing "tunneling", the consequence of a movement so large that the simulation failed to detect the collision between the moving object and its simulation siblings. Try turning on continuous collision detection (bodyDef.isBullet = true), which by default it is not for dynamic bodies.

Also, the correct way to move dynamic bodies is through the application of forces. You should reserve manual positioning for static and kinematic bodies. You will need the latest version of the lib (2.1a) for kinematic body support: myBody.SetType(b2_kinematicBody)

1 decade ago by Richard

Upgraded to 2.1 from github.

I've renamed functions where they changed, but my SetTransform (previous setxform) no longer works...

this.body.SetTransform(new b2.Vec2( (this.pos.x + this.size.x / 2) * b2.SCALE, (this.pos.y + this.size.y / 2) * b2.SCALE ), 0 );	

I get...

Uncaught TypeError: Object [object Object] has no method 'GetAngle'

Even changing the 0 to this.body.getAngle(), same error.

Has this function been removed?

1 decade ago by alexandre

SetTransform -> SetXForm

1 decade ago by Richard

Nope, that function is renamed in the latest version to SetTransform.

Uncaught TypeError: Object [object Object] has no method 'SetXForm'

1 decade ago by alexandre

LIke I said, you need 2.1a. Updated I believe by dkollman. Search the forums for "2.1a".

1 decade ago by oravecz

Quote from alexandre
@Richard

You could create a kinematic body as a proxy of your player entity, moving it with the SetTransform method at every update so as to match the position of its employer. Dynamic b2bodies will collide with it but static and other kinematic bodies won't.


How do you change the body type to kinematic?

1 decade ago by alexandre

Provided you have the right box2d lib version (2.1a), you do something like this:

	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
			);
	    bodyDef.type = b2.Body.b2_kinematicBody;
	    
	    this.body = ig.world.CreateBody(bodyDef);
	    
	    var shapeDef = new b2.PolygonShape();
	    shapeDef.SetAsBox(
				this.size.x / 2 * b2.SCALE,
				this.size.y / 2 * b2.SCALE
	    	);
	    
	    // CreateFixture2(var shapedef, density)
	    this.body.CreateFixture2(shapeDef, 0.1);
	},

1 decade ago by oravecz

Thanks for the snippet.

So, this works for you? I seem to get a TypeError in lib.js when I try your code because you call:
new b2.PolygonShape()

with an empty constructor. Eventually, this bubbles up to the Shape constructor which takes a parameter 'b' (which is undefined). When b.userData is called, it blows up.

1 decade ago by alexandre

Works fine for me, but not until I learned to jump through hoops. IIRC, the version I cloned from clok's pispace game.

1 decade ago by oravecz

Thanks, it's working now for me again. Not sure what was going on there. Perhaps I am mixing the plugin versions.

1 decade ago by alexandre

Perhaps I am mixing the plugin versions.


Yeah getting a more recent version of box2d that plays nice with impact is touch and go. Takes quite a bit of digging around.

1 decade ago by Okerampa

Have you tried with "SetPositionAndAngle" ? I'm pretty confused with all these functions changing name...

1 decade ago by Joncom

Quote from Okerampa
Have you tried with "SetPositionAndAngle" ? I'm pretty confused with all these functions changing name...
The "Box2D Made Easy" plugin removes some of the confusion. You don't need to use differently named functions. For the most part it's exactly like any other ImpactJS game.
Page 1 of 1
« first « previous next › last »