Jump to content

How to collide between line and sprite?


rosancoderian
 Share

Recommended Posts

How to collide between line and sprite?
something like
 

var w = 320;var h = 640;var line;var sprite;var game = new Phaser.Game(w, h, Phaser.AUTO, '', { 'preload': preload, 'create': create, 'update': update });function preload() {    game.load.spritesheet('sprite', 'enemy.png', 8, 8);}function create() {        sprite = game.add.sprite(0, 0, 'sprite');    sprite.body.collideWorldBounds=true;    sprite.body.gravity.y = 9.8;        line = game.add.graphics(0, 0);    line.beginFill(0xffffff);    line.lineStyle(1, 0xffffff, 1);    line.moveTo(0, h/2-1);    line.lineTo(w, h/2-1);    line.endFill();}function update() {    game.physics.collide(sprite, line, collHandler, null, this);}function collHandler(obj1, obj2) {    console.log('collision!!!!');}

but it's not working

Link to comment
Share on other sites

pretty sure you'll have to write your own collision logic for colliding a line with a sprite. The easiest (and it's not really that easy) way that I can think of is use geometry to see if a segment intersects another segment. Run this check four times, once for each "side" of the sprite.

 

This also requires you to figure out the line equations for your line, and also for your sides of your sprite. The math gets pretty hairy but it's doable. It's not that efficient though, so i would collide with lines sparingly.

Link to comment
Share on other sites

if you want your sprite to stop falling,

an easy way to go about it would be to have it's gravity at zero and only activate it when it leaves the ground.

And you can still draw the line but it would be only cosmetically there.

 

var ground = 320;function update(){ sprite.gravity.y = 0 if (sprite.y < ground) {  sprite.gravity.y = 10 }}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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