Jump to content

Static P2 body, relative to dynamic body?


Recommended Posts

I have a circle ("hull") with a small thin rectangle ("turret") extending from it. The hull and the turret are children of sprite, "tank".


Hull has a dynamic body. Nothing fancy. It loads in the center of the viewport and then falls to the lower bounds. That's exactly what I want it to do.


If I don't apply physics to the turret, no problem. The turret follows right along with hull, as it should. With user input I can aim the turret by adjusting angle with the arrow keys.


However, I want the turret to have a physical body, so if the hull (circle) is resting on the lower bounds, the position of the hull can be influenced by the angle of the turret, particularly if the turret comes in contact with the ground.


The problem is, as soon as I apply a dynamic body to turret, it turns into more of a flaccid rag doll as opposed to a mechanical extension of the hull.


I'd like the turret to behave like a static body, relative to a dynamic body. I'd like it to have a physical surface that it can come to rest on, and I'd need to be able to adjust its angle with user input, but I want it to be otherwise unaffected by physics.


How can I accomplish this?




I'm fairly new to Phaser and game development. This is my, "Hello World" of sorts.

Link to comment
Share on other sites

Alright, here's some code. This is exactly how I want the tank to behave, except I want the turret to have a physics body, so that if the tank is upside down, I can potentially use the turret to upright the tank by changing its angle (with the arrow keys).


Any thoughts?



NOTE: The circle is just a debug object, thrown in to interfere with the tank body.

		function create() {						game.stage.backgroundColor = 0xffffff;						game.physics.startSystem(Phaser.Physics.P2JS);			game.physics.p2.gravity.y = 300;						ball = game.add.sprite(90,150,'');		    game.physics.p2.enable(ball,true);  //activate the physics body debug rendering to make it visible		    ball.body.setCircle(20,0,0);															// Tank			tank = game.add.sprite(100, 100, '');						game.physics.p2.enable(tank, true);			// Hull			hull = game.add.graphics();					    hull.lineStyle(0);		    hull.beginFill(0x000000, 1);		    hull.arc(0,0,25,Math.PI,0);		    hull.endFill();		    		    tank.hull = game.add.sprite(0, 0, hull.generateTexture());		    tank.hull.anchor.setTo(0.5);		    tank.addChild(tank.hull);		    		    hull.destroy();		    		   	// Turret  		    turret = game.add.graphics();								    turret.lineStyle(0);		    turret.beginFill(0x000000, 1);		    turret.drawRect(0,0,24,7);		    turret.endFill();		    		    tank.turret = game.add.sprite(0, 8, turret.generateTexture());		    tank.turret.anchor.setTo(-0.5, 0.5);		    tank.turret.angle = -25;		    tank.addChild(tank.turret);		    		    turret.destroy();		    		    		 		    		    cursors = game.input.keyboard.createCursorKeys();		    					}				function update() {						if (cursors.left.isDown) {		        tank.turret.angle--;		    }		    else if (cursors.right.isDown) {		        tank.turret.angle++;		    }					}
Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...