# Line Collision

## Recommended Posts

I'm trying to implement a game where it's possible for a sprite to collide with a line object. For example:

``````game.physics.startSystem(Phaser.Physics.ARCADE);

sprite = game.add.sprite(0, 0, "mySprite");
sprite.body.gravity.y = 420;

foothold = new Phaser.Line(0, 600, 200, 400); // a diagonal line

/**
* update = function() {
*     collide sprite and foothold... HOW!?
* };
*/``````

I want the sprite to collide with the diagonal line when the sprite falls onto it. Is this a possibility in arcade physics? There's an example of diagonal platforms here: http://www.phaser.io/examples/v2/ninja-physics/ninja-aabb-vs-tile but I do not want to use ninja physics.

##### Share on other sites

You can do something like

``````var line = new Phaser.Line;
var rect = new Phaser.Rectangle;
// …

// update:

rect.copyFrom(sprite.body);

Phaser.Line.intersectsRectangle(line, rect);``````

##### Share on other sites

It doesn't work. Phaser.Line.intersectsRectangle(line, rect) is always false.

``````function startGame() {
this.create = function() {
line = new Phaser.Line(200, 500, 600, 500);
rect = new Phaser.Rectangle(0, 0, 50, 50);
};
this.update = function() {
console.log(Phaser.Line.intersectsRectangle(line, rect)); // NEVER TRUE!?
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
rect.x -= 10;
} else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
rect.x += 10;
} else if (game.input.keyboard.isDown(Phaser.Keyboard.UP)) {
rect.y -= 10;
} else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) {
rect.y += 10;
}
};
this.render = function() {
game.debug.geom(line);
game.debug.geom(rect);
};
}``````

##### Share on other sites

@samme That's a good idea! You don't have to copy to a rectangle first, though, cuz that method relies on x, y, right, and bottom, which sprites and sprites' bodies have as well.

@johncintron Everything in your code looks right to me... If you create the rectangle and the line so they intersect from the start does it work? Like, if you don't depend on the keyboard to move them into intersection?

##### Share on other sites

@drhayes That doesn't work either. If i create both the line and rectangle such that they overlap in the `create` function, ` Phaser.Line.intersectsRectangle(line, rect)` is still false. I'm using Phaser 2.6.2 by the way.

For example

``````function startGame() {
this.create = function() {
line = new Phaser.Line(200, 500, 600, 500);
rect = new Phaser.Rectangle(375, 475, 50, 50);
};
this.update = function() {
console.log(Phaser.Line.intersectsRectangle(line, rect));
};
this.render = function() {
game.debug.geom(line);
game.debug.geom(rect);
};
}``````

##### Share on other sites

I figured out the issue.

``Phaser.Line.intersectsRectangle``

works only when the `line` argument is diagonal. Definitely a bug in the phaser source code.

@samme Observe what happens when you do this instead:

``line = new Phaser.Line(200, 500, 600, 500);``

• ### Recently Browsing   0 members

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