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 riceje7

I am attempting to use box2d to create a soft-body blob. I found this tutorial here that shows how to make one using the as3 version of box2d so I ported it to work with impact's box2d provided in the physics demo on the downloads page. however i am getting this error: Uncaught TypeError: undefined is not a function on this line of my code: var fixtureDef = new b2.FixtureDef();

here is my blob entity:

ig.module('game.entities.blob').requires('plugins.box2d.entity').defines(function () {
	EntityBlob = ig.Box2DEntity.extend({
		blobMassParticles: 16,
		blobMassParticleDist: 50,
		blobs: [],
		worldScale: null,
		init: function (x, y, settings) {
			this.parent(x, y, settings);
			this.worldScale = settings.worldScale;
			this.blobs.push(this.blob(50, 50, 15));
			for(var i = 0; i < this.blobMassParticles; i++){
				var angle = (2*Math.PI)/this.blobMassParticles *i;
				var posX = this.pos.x+this.blobMassParticleDist+Math.cos(angle);
				var posY = this.pos.y+this.blobMassParticleDist+Math.sin(angle);
				this.blobs.push(this.blob(posX, posY, 2));
				var dJoint = new b2.DistanceJointDef();
				dJoint.bodyA = this.blobs[0];
				dJoint.bodyB = this.blobs[this.blobs.length-1];
				dJoint.localAnchorA = new b2.Vec2(0, 0);
				dJoint.localAnchorB = new b2.Vec2(0, 0);
				dJoint.length = this.blobMassParticleDist/wordlScale;
				dJoint.dampingRatio = .5;
				dJoint.frequencyHz = 5;
				var distanceJoint = ig.world.CreateJoint(dJoint);
				if(i > 0){
					var distanceX = posX/this.worldScale-this.blobs[this.blobs.length-2].pos.x;
					var distanceY = posY/this.worldScale-this.blobs[this.blobs.length-2].pos.y;
					var distance = Math.sqrt(distance*distanceX+distanceY*distanceY);
					dJoint.bodyA = this.blobs[this.blobs.length-2];
					dJoint.bodyB = this.blobs[this.blobs.length-1];
					dJoint.localAnchorA = new b2.Vec2(0, 0);
					dJoint.localAnchorB = new b2.Vec2(0, 0);
					dJoint.length = distance;
					distanceJoint = ig.world.CreateJoint(dJoint);
				}
				if(i === blobMassParticles-1){
					var distanceX = posX/this.worldScale-this.blobs[1].pos.x;
					var distanceY = posY/this.worldScale-this.blobs[1].pos.y;
					var distance = Math.sqrt(distance*distanceX+distanceY*distanceY);
					dJoint.bodyA = this.blobs[1];
					dJoint.bodyB = this.blobs[this.blobs.length-1];
					dJoint.localAnchorA = new b2.Vec2(0, 0);
					dJoint.localAnchorB = new b2.Vec2(0, 0);
					dJoint.length = distance;
					distanceJoint = ig.world.CreateJoint(dJoint);
				}
			}
		},
		update: function () {
			this.parent();
		},
		draw: function () {
			this.parent();
		},
		blob: function(x, y, r){
			var bodyDef = new b2.BodyDef();
			bodyDef.position.Set(x/this.worldScale, y/this.worldScale);
			bodyDef.type = b2.Body.b2_dynamicBody;
			var circleDef = new b2.CircleDef();
			circleDef.density = 1;
			circleDef.friction = .5;
			circleDef.restitution = .4;
			circleDef.radius = 10;
			circleDef.type = b2.Body.b2_dynamicBody;
			var circleShape = new b2.CircleShape(circleDef); 
			var fixtureDef = new b2.FixtureDef();    //Thrown Here
			fixtureDef.shape = circleShape;
			fixtureDef.density = 1;
			fixtureDef.restitution = .4;
			fixtureDef.friction = .5;
			var blob = ig.world.CreateBody(bodyDef);
			blob.CreateFixture(fixtureDef);
			return blob;
		}
	});
});

and here is my main.js:

ig.module('game.main').requires('impact.game', 'impact.font', 'impact.image', 'plugins.box2d.game', 'game.entities.blob', 'plugins.Button', 'game.levels.splashscreen', 'plugins.impact-splash-loader').defines(function () {
	SplashScreen = ig.Box2DGame.extend({
		titlefont: new ig.Font("media/titlefont.font.png"),
		buttons: [],
		buttonsData: {
			xlocs: [832, 960, 1792, 1920, 2752, 2880],
			w: 128,
			h: 85
		},
		boundingBox: null,

		gravity: new b2.Vec2(0, 30),
		worldScale: 30,

		player: null,

		init: function () {
			ig.game.loadLevel(LevelSplashscreen);
			ig.input.bind(ig.KEY.MOUSE1, 'click');
			for (var i = 0; i < 6; i++) {
				var button = ig.game.spawnEntity(Button, this.buttonsData.xlocs[i], 64, {
					flip: i % 2 === 0 ? false : true,
					size: {
						x: this.buttonsData.w,
						y: this.buttonsData.h
					},
					pressedUp: function () {
						console.log("nav button");
					}
				});
				this.buttons.push(button);
			}
			
			this.player = ig.game.spawnEntity("EntityBlob", 50, 50, {
				worldScale: this.worldScale
			});
		},
		update: function () {
			this.updateWorld();
			this.parent();
		},
		draw: function () {
			this.parent();
		},
		updateWorld: function () {
			ig.world.Step(ig.system.tick, 5);
			ig.world.ClearForces();
		}
	});

	ig.main('#canvas', SplashScreen, 60, 960, 640, 1, ig.ImpactSplashLoader);
});

can anyone help me figure out what it is that i'm doing wrong? At first I thought I was getting a different error because i wasn't passing the right thing to the constructor for the b2.CircleShape(), but when I corrected that i get the error above. And now i'm stumped. any help would be greatly appreciated.

Another thing that i haven't been able to find any info on or anything is how to spawn box2d entities. i have been assuming that it is the same as normal but i could be wrong. can anyone confirm the correct way to spawn a box2d entity? Thanks everyone!

1 decade ago by Joncom

You are certain that b2.FixtureDef does exist?
What happens if you type that in console and hit enter?
Page 1 of 1
« first « previous next › last »