Jump to content

Collision Detection


Joss
 Share

Recommended Posts

Hi,

 

I've knocked up a simple game using js and pixi.js, very basic but want to include collision detection, I realise pixi.js doesn't do that, but can anyone recommend a simple but effective way to handle that which work with pixi.js? I want to know when two sprites touch on the screen for my game 'stab the crab'!

 

Thanks

 

Joss

 

Link to comment
Share on other sites

Stap the crab! Sounds ace :)

 

For most of the games I usually keep collision down to its simplest form.. either using rectangle collision or circle collision. Run Pixie Run uses rectangle collision detection as its super fast (which means its great for mobile).

 

GAME.CollisionManager.prototype.playerVsPickup = function(){		var pickups = this.engine.pickupManager.pickups;	var steve = this.engine.steve; // yes, he is called steve 		for (var i = 0; i < pickups.length; i++) 	{		var pickup = pickups[i]		var xdist = pickup.position.x - steve.position.x;		if(xdist > -pickup.width/2 && xdist < pickup.width/2)		{			var ydist = pickup.position.y - steve.position.y;					if(ydist > -pickup.height/2 && ydist < pickup.height/2)			{				// HIT CODE!!   			}		}	}}

 

hope that helps!

Link to comment
Share on other sites

Thanks for that bit of code, although I'm totally new to developing games of any sort, so not really sure what to do with it, presumably I need to include a JS file for a gaming engine?

 

My current game is purely straight JS and pixi.js

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...