Jump to content

orbital shield, group or individual sprites?


Markarz
 Share

Recommended Posts

Hi guys, 
 
I'm sure people have come across this before, but I'm wondering whether it would make sense to use groups for orbital sprites. I'm trying to set up a typical orbital shield using rotation, and thought it would be cool to add many sprites to the orbit. I've got it working well enough with a single sprite, but adding additional balls seems to result in them colliding immediately, gaining velocity, and spinning out of orbit. 
 
Is it even worth using a group for something like this, or would I be better off using individual sprites like in this example?

 

Here's what I'm doing so far, and I haven't tried to add any code for collisions yet but the idea is to block enemy shots with the orbs. I'm not sure why the orbs collide with themselves in the group since they don't collide with anything else in the game yet, but I think using p2 to get collision handlers would be the next step. 
 

export class PsiBall {  constructor(game, player) {    this.game = game;    this.player = player;    this.psiballs = game.add.group();    this.addBalls(10);    this.psiballs.parent = player;    this.psiballs.enableBody = true;    this.psiballs.physicsBodyType = Phaser.Physics.P2JS;    this.psiballs.pivot.x = 5;  }  rotate(speed) {    this.psiballs.rotation += speed;  }  createBall(x, y) {    let ball = this.psiballs.getFirstDead();    ball.reset(x,y);  }  addBalls(count) {    for(let i = 0; i < count; i++) {      let psiball = this.psiballs.create(0, 0, 'item-57', 0, false);      this.game.physics.p2.enable(psiball);      psiball.scale.setTo(0.5);      psiball.smoothed = false;      psiball.anchor.setTo(0.5);      psiball.enableBody = true;    }  }}
Link to comment
Share on other sites

Hi, I am not P2 expert, but if I was you, I would make orbits kinematic (see this example: http://phaser.io/examples/v2/p2-physics/kinematic-body) and not colliding to each other (see collision groups example: http://phaser.io/examples/v2/p2-physics/collision-groups).

 

 If object is kinematic, then it can move, it reacts to collisions, but forces or collisions do not move it - it is great to create moving platforms or elevators.

 

 Regarding groups - I would place all orbits into one group and rotated this group all at once.

Link to comment
Share on other sites

Hey Tom, 

 

Thanks for the reply! 

 

I think setting the body to kinematic would prevent collision handlers from firing, so I wouldn't be able to use body.collides to destroy incoming bullets (and the ball that blocked it). I did however get the rotation working similar to how I pasted above and reducing the size of the hit box to avoid colliding with themselves. It would be better to use the p2 physics debugger to see the actual hit boxes and get the size right, but I never got it working properly. 

 

I have everything working visually now, but the collisions are still behaving oddly. I think setting the parent on a group may mess up collisions in p2. 

 

Update: Confirmed that setting it to the parent is responsible for the collision problems. Anyone know if there a proper way to handle this, or what may be responsible for the unexpected behavior? I suppose I can set the position of the group to the position of the player in my update method, but having the orbit as a child of the player sprite was convenient. 

Link to comment
Share on other sites

Anyone have any other ideas for resolving the collision issues here? I'd guess that setting the parent to the player object is messing with the collision mask somehow, by putting the orbs on a different layer than the rest of my objects (which do not have parents).

 

My best temporary work-around has been to just add a number of "blocks" to the player whenever they get more orbs, and subtract from there when the player is hit. The downside is that there's no real collision detection on the orbs themselves and it just provides an arbitrary visual cue that you have blocks available before you start taking damage. 

 

I think I could also rewrite all of the code to avoid using groups and parents, but I'd have to calculate the position of every orb in my update function, which just doesn't seem like a good solution to me. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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