tips4design Posted January 28, 2015 Share Posted January 28, 2015 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 More sharing options...
Tenshi Posted January 28, 2015 Share Posted January 28, 2015 I am noob, but I think u can solve your problem using invisible sprite for ray. Link to comment Share on other sites More sharing options...
tips4design Posted January 28, 2015 Author Share Posted January 28, 2015 I am noob, but I think u can solve your problem using invisible sprite for ray. And how do I create an invisible sprite that can I use for collision? (in my case a px rectangular sprite that I can rotate)And what about performance? Isn't it too much to create a spirte if I only need a line? Link to comment Share on other sites More sharing options...
Tenshi Posted January 28, 2015 Share Posted January 28, 2015 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:http://examples.phaser.io/_site/view_full.html?d=games&f=tanks.js&t=tankshttp://examples.phaser.io/_site/view_full.html?d=games&f=invaders.js&t=invaders Link to comment Share on other sites More sharing options...
tips4design Posted January 28, 2015 Author Share Posted January 28, 2015 I use the rays for ray casting, so I do not need to draw them. I only need them in order to check for collisions, but they will not actually be drawn.A ray is not something that you "shoot", but a line segment of specific length and orientation that does not move. Link to comment Share on other sites More sharing options...
mariogarranz Posted January 28, 2015 Share Posted January 28, 2015 May this example help you?http://examples.phaser.io/_site/view_full.html?d=tilemaps&f=tilemap+ray+cast.js&t=tilemap%20ray%20cast Link to comment Share on other sites More sharing options...
tips4design Posted January 28, 2015 Author Share Posted January 28, 2015 May this example help you?http://examples.phaser.io/_site/view_full.html?d=tilemaps&f=tilemap+ray+cast.js&t=tilemap%20ray%20castThat examples uses a tilemaplayer, and for the intersection tilemaplayer.getRayCastTiles . My game does not use a tilemap layer, but, as I said, is based on the tanks example. (only uses sprites) Link to comment Share on other sites More sharing options...
mariogarranz Posted January 28, 2015 Share Posted January 28, 2015 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. tips4design 1 Link to comment Share on other sites More sharing options...
tips4design Posted January 29, 2015 Author Share Posted January 29, 2015 Thank you! This is a nice solution, though it's a bit "hacky". So, bumping this thread... Link to comment Share on other sites More sharing options...
Recommended Posts