Jump to content

Intersect between sprite and line Physics.Arcade


Recommended Posts

How can I test the intersection between a Line and a Sprite using Physics.Arcade? I want to do this because I use ray castig, and I want to test wheter a ray intersects other enemies or not.


This is the sprite I want to test collision against:

    this.mainBody = game.add.sprite(x, y, spriteName, 'tank1');       game.physics.enable(this.mainBody, Phaser.Physics.ARCADE);    this.mainBody.body.immovable = false;    this.mainBody.body.collideWorldBounds = true;

I tried using a Phaser.Line , but found no collision detection function in Physics.Arcade that takes a Phaser.Line and a Sprite.

What is the best way to test if a ray cast intersects a sprite? (tried implementing my own function which tests a segment and a rectangle, but the problem is that the rectangle, eg the sprite, can also be rotated).


My idea was to create (somehow) a rectangle of width 1, to represent the line, anduse the Phaser.Arcade overlap function between this rectangle and my sprite.

Link to comment
Share on other sites

U can draw big bullet sprites for your game. It will be look more cute than rays.

In create function:

bullets = game.add.group();bullets.enableBody = true; 

In update:

cursors   =   game . input . keyboard . createCursorKeys ( );//the same for right up and downif   ( cursors . left . isDown ) {    var bullet = bullets.create( x for tank , y for tank , 'bullet');bullet . body . velocity . x   =   - 500 ; }game.physics.arcade.overlap(bullet, enemy, boom, null, this); 


Create boom function:

function boom (bullet, enemy) {   // Removes the bullet from the screen bullet.kill();} 

And u must kill bullet if it out of screen.



If u looking for best decision u can see phaser examples:



Link to comment
Share on other sites

Yeah but if you look into the getRayCastTiles method you will see how it works:

First, it generates a set of coordinates from the line:

var coords = line.coordinatesOnLine(stepRate);

Then, it checks every tile (which, with collision enabled, I think is a Sprite) with the coordinate list:

  if (tile.containsPoint(coord[0], coord[1]))

And does whatever, in this case store the tiles into an array.


Link to comment
Share on other sites


  • Recently Browsing   0 members

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