Jump to content

Body - Shape Problem!


Recommended Posts

I've set the shape to be the size of the sprites but still when I jump the collision doesn't happen on the right spot...this is my code.


game.module(    'game.main').require('engine.core','engine.physics').body(function() {game.addAsset('grass.png', 'grass');game.addAsset('p1_stand.png', 'player');game.Debug.enabled = true;SceneGame = game.Scene.extend({    backgroundColor: 0xd0f4f7,    init: function() {        this.world = new game.World(0, 2000);        var grass = new game.TilingSprite('grass');        grass.position.set(0, game.system.height - grass.height);        grass.speed.x = -300;        this.stage.addChild(grass);        this.addObject(grass);                var grassShape =  new game.Rectangle(grass.width, grass.height);                var grassBody = new game.Body({            position: {x: 0, y: game.system.height - grass.height},            collisionGroup: 1,            collideAgainst: 0          });                    grassBody.addShape(grassShape);                    this.world.addBody(grassBody);                    var player = new game.Sprite('player');        player.position.set(game.system.width/6, game.system.height - grass.height - player.height);                var playerShape =  new game.Rectangle(player.width, player.height);                var playerBody = new game.Body({            position: {x: game.system.width/6, y: game.system.height - grass.height - player.height},            velocityLimit: {x: 100, y: 1000},            collisionGroup: 0,            collideAgainst: 1          });                    playerBody.addShape(playerShape);                    this.world.addBody(playerBody);                        this.stage.addChild(player);        this.addObject(player);                grassBody.collide = function() {            console.log('hi');        }                        player.update = function() {            player.position.x = playerBody.position.x;            player.position.y = playerBody.position.y;        }                this.mousedown = function() {         playerBody.mass = 1;        playerBody.velocity.y = -900;                }    }});game.start(SceneGame, 1024, 768);});
Link to comment
Share on other sites

Hmm i think you are positioning wrong.


You see body position is center of shape, so try to set your sprite anchor to center too.

player.anchor.set(0.5, 0.5);

or if you don't want to change anchor, then:

player.update = function() {    player.position.x = playerBody.position.x - player.width / 2;    player.position.y = playerBody.position.y - player.height / 2;}

Also you can see your physics shapes if you enable debugDraw,

just put ?debugdraw into end of your url.

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.

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.


  • Recently Browsing   0 members

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