Jump to content

Phaser+Box2D: Collision of Sprite with ground


Schludi
 Share

Recommended Posts

Hello!

 

Can so. explain me why my beer sprite is not colliding with the ground?

 

https://phaser-schludi.c9.io/beer-througher/beerThrougher.html

 

The code is this:

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update });var sprite;var cursors;var headline;var platforms;var ground;// Start and End of Mouse Dragvar startX, startY, endX, endY;function preload() {game.load.image('bier', 'images/Beer_klein.png');game.load.image('ground', 'images/grasunten.png');}function create() {game.stage.backgroundColor = '#00BEF2';// Enable Box2d physicsgame.physics.startSystem(Phaser.Physics.BOX2D);game.physics.box2d.friction = 0.5;game.physics.box2d.setBoundsToWorld();game.physics.box2d.restitution = 0.7;game.physics.box2d.gravity.y = 500;// Add a spritesprite = game.add.sprite(200, 400, 'bier');//sprite.body.immovable=false;//sprite.setZeroVelocity();//sprite.body.gravity=0;// The platforms group contains the ground and the 2 ledges we can jump onplatforms = game.add.group();//game.physics.box2d.enable(platforms);//platforms.body.static = true;// We will enable physics for any object that is created in this groupplatforms.enableBody = true;// Here we create the ground.var ground = platforms.create(0, game.world.height - 29, 'ground');// Scale it to fit the width of the game (the original sprite is 400x32 in size)//ground.scale.setTo(2, 2);// Enable for physics. This creates a default rectangular body.game.physics.box2d.enable(sprite);game.physics.box2d.enable(ground);// This stops it from falling away when you jump on itground.body.static=true;ground.body.immovable = true;// Modify a few body propertiessprite.body.fixedRotation = true;headline=game.add.text(5, 5, 'Drag Object.', { fill: '#ffffff', font: '14pt Arial' });cursors = game.input.keyboard.createCursorKeys();game.input.onDown.add(mouseDragStart, this);game.input.addMoveCallback(mouseDragMove, this);game.input.onUp.add(mouseDragEnd, this);}function mouseDragStart() {game.physics.box2d.mouseDragStart(game.input.mousePointer);startX=Math.round(sprite.x);startY=Math.round(sprite.y);headline.text='Start Point: (' + startX + ','+ startY +')';}function mouseDragMove() {game.physics.box2d.mouseDragMove(game.input.mousePointer);}function mouseDragEnd() {game.physics.box2d.mouseDragEnd();endX=Math.round(sprite.x);endY=Math.round(sprite.y);var distance= Math.sqrt((endX-startX)*(endX-startX)+(endY-startY)*(endY-startY));var angel = Math.atan2(endY-startY, endX-startX) * 180 / Math.PI;headline.text='Start Point: (' + startX + ','+ startY +') / End Point: (' + endX + ','+ endY +') / Distance: '+distance+' / Angel: '+angel;}function update() {//sprite.body.setZeroVelocity();// Prüfe ob die Sterne auf die Plattform treffen//game.physics.box2d.col}
Link to comment
Share on other sites

Hello!

 

I solved it by myself after positioning a sprite at the bottom (without group()) and just enable the body physik.

 

https://phaser-schludi.c9.io/beer-througher/beerThrougher.1.html

 

Next question:

I am using the Mouse Drag and Drop with follow camera. In the example above it is possible to through the beer to the right side, but when the camera is not docked at the right side, the problem is that the drag position is wrong.

 

How can i calculate the offset when the beer is on the right side and i want to through it again?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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