Jump to content

Best technique for collision between custom objects


JackBid
 Share

Recommended Posts

Sword = function (x, y) {	this.sprite = game.add.sprite(x, y, 'sword');	this.sprite.anchor.setTo(0.5, 0.5);	this.sprite.immovable = true;	this.sprite.body.velocity.y = -150;	this.sprite.outOfBoundskill = true;};Mummy = function (x, y) {	this.sprite = game.add.sprite(x, y, 'mummy');	this.sprite.anchor.setTo(0.5, 0.5);	this.sprite.immovable = true;	this.sprite.body.velocity.y = 30;	this.sprite.animations.add('walk');	this.sprite.animations.play('walk', 7, true);}var swords = [];var mummies = [];function preload(){	game.load.spritesheet('player', 'assets/player.png', 64, 22);	game.load.spritesheet('mummy', 'assets/mummy.png', 64, 22);	game.load.image('sword', 'assets/sword.png');}function create(){	game.stage.backgroundColor = '#E2DBCE';	player = game.add.sprite(10,530, 'player');	player.name = 'player';	player.anchor.setTo(0.5, 0.5);	player.body.collideWorldBounds = true;        player.body.immovable = true;        player.animations.add('walk');    leftKey = game.input.keyboard.addKey(Phaser.Keyboard.LEFT);    rightKey = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);    spaceKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);}function update() {        game.physics.collide(swords, mummies, swordHitMummy. null, this);        if (spaceKey.isDown) {    	     createSword(player.x, player.y-50);        }        for (var i=0; i<10; i++){             createMummy(Math.random(), Math.random());        }	}function createSword (x, y) {        swords.push(new Sword(x, y));}function createMummy (x, y) {	mummies.push(new Mummy(x, y));}function swordHitMummy (sword, mummy) {        console.log("success");}

The design of my game is that the player moves left and right firing swords (which move only up) at mummies (which move only down). The player is at the bottom of the canvas and the swords are created at the bottom and moved up, the mummies spawn in randomly at the top and move down. The code above had been edited and had pieces removed as to focus just on my current issue. I am unsure of how to collide the mummies and swords as they are stored in arrays. I tried a few ideas earlier but could not get it to work properly, so decided to post on here in order to find the best way to achieve this. I tried using groups, but it appeared that I could only store sprites in groups and not objects - although I could be wrong, I am quite new to this! I also tried looping through the arrays and colliding individual objects, but I had problems with objects with no sprite attached still colliding and causing errors. Basically I want to know what would be the best possible way to do this, am I even doing this at all right? Should I be using arrays (I noticed they were used in the tank example)? Please consider that I am quite new to programming and phaser.

 

Thanks.

Link to comment
Share on other sites

If this wasn't in the context of Phaser, I'd just say this sounds like it could be solved using simple rectangle collisions.

Basically have a x,y and width,height value for the collision mask of each sword and mummy.

Every step, check for every sword against every mummy,
if swords x + width is less than mummy's x

if swords x is greater than mummy's x +width

if swords y + width is less than mummy's y
if swords y is greater than mummy's y + height


if any of those are true, they are not colliding

Link to comment
Share on other sites

If this wasn't in the context of Phaser, I'd just say this sounds like it could be solved using simple rectangle collisions.

Basically have a x,y and width,height value for the collision mask of each sword and mummy.

Every step, check for every sword against every mummy,

if swords x + width is less than mummy's x

if swords x is greater than mummy's x +width

if swords y + width is less than mummy's y

if swords y is greater than mummy's y + height

if any of those are true, they are not colliding

Yes, but my main problem is that mummies and swords are arrays which contain objects. Part of the object is a phaser sprite, but the object as a whole is not just a sprite, this is where I am not sure how to fix it.

Link to comment
Share on other sites

you could walk the arrays and collide each object's sprite with the object's sprites in the other array, but this begs the question: why are you creating these objects that have a Sprite property instead of just using Sprites? the easy solution is to just extend Sprites with the extra properties that you need and store the Sprites in Groups. then you can do a simple collide of the groups against each other, which Phaser will handle for you...

 

javascript is a dynamic language. you can add whatever additional properties you need directly onto Sprite objects. if you find this too awful to consider you can extend the Sprite class and "inherit" all the Sprite properties into your object, which can then be treated exactly like any other Sprite.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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