Jump to content

How to make a seesaw ?


Gamble202
 Share

Recommended Posts

Hello.

 

I would like to make a seesaw with the P2 physics, but have some weird behavior.

(excuse my english, i just start to learn)

 

so i made this:

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'gameDiv', { preload: preload, create: create, update: update, render: render });function preload() {	game.load.image('plate', 'img/lock_plat.png');	game.load.image('bowling', 'img/bowling.png');}function create() {	game.physics.startSystem(Phaser.Physics.P2JS);	game.physics.p2.gravity.y = 250;		plate = game.add.sprite(400,428, 'plate');	bowl = game.add.sprite(500, 350, 'bowling');		game.physics.enable(		[plate,bowl], 		Phaser.Physics.P2JS);		bowl.body.debug = true;	bowl.collideWorldBounds = true;	bowl.body.setCircle(20,0,0,0);	bowl.body.mass = 100;			plate.body.debug = true;	plate.body.mass = 10;		ballMateria = game.physics.p2.createMaterial('bowlMateria', bowl.body);	basculeMateria = game.physics.p2.createMaterial('basculeMateria', plate.body);		genericCM = game.physics.p2.createContactMaterial(ballMateria, basculeMateria);	genericCM.friction = 1;	genericCM.stiffness = 1e10;	genericCM.restitution = 0;	genericCM.relaxation = 0;	}function update() {		if(plate.body.x != 400){		plate.body.x = 400;	}	if ( plate.body.y != 428){		plate.body.y = 428;		}	if (plate.body.rotation > 0.2){		plate.body.rotation = 0.2;	}	console.log(plate.body.x+' / '+plate.body.y+' - '+ plate.body.rotation);}function render() {}

The plate is a rectangular sprite and the bowl a circle.

 

And I have some weird behavior.

the console log tell me the body.x and body.y are 400 and 428, but the plate sprite move on screen.

 

In the update function i fixed the x and y of my plate.body to simulate some pivot point. And a stop on the rotation.

It is correct to do it that way ?

 

Thank you in advance.

 

 

 

Link to comment
Share on other sites

I want to do this:

 

see_saw_by_202_trait_plat-d8mechn.jpg

 

In fact, my problem is to block the rotation.

 

My update code restraints my body.x and body.y correctly.

When you make a sprite available for the P2physics, it's body.x/y became the center of the sprite.

If I delete my restraint on the rotation > 0.2, my sprite correctly rotate around his center point when hit by the bowling ball.

function update() {		if(plate.body.x != 400){		plate.body.x = 400;	}	if ( plate.body.y != 428){		plate.body.y = 428;		}}

How can I stop the sprite rotation when body.rotation > 0.2 or body.rotation < -0.2 ?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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