mirror of
https://github.com/photonstorm/phaser
synced 2024-11-23 05:03:37 +00:00
More collision test cases and fixing a few issues as I go :)
This commit is contained in:
parent
f812b92b8a
commit
ecc91fb4e0
7 changed files with 167 additions and 9 deletions
Binary file not shown.
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 21 KiB |
BIN
examples/assets/sprites/fruitnveg64wh37.png
Normal file
BIN
examples/assets/sprites/fruitnveg64wh37.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 27 KiB |
100
examples/collision_sprite_vs_group.php
Normal file
100
examples/collision_sprite_vs_group.php
Normal file
|
@ -0,0 +1,100 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<title>phaser.js - a new beginning</title>
|
||||
<?php
|
||||
require('js.php');
|
||||
?>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
(function () {
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('phaser', 'assets/sprites/phaser-dude.png');
|
||||
game.load.spritesheet('veggies', 'assets/sprites/fruitnveg32wh37.png', 32, 32);
|
||||
|
||||
}
|
||||
|
||||
var sprite;
|
||||
var group;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#2d2d2d';
|
||||
|
||||
// This will check Sprite vs. Group collision
|
||||
|
||||
sprite = game.add.sprite(32, 200, 'phaser');
|
||||
sprite.name = 'phaser-dude';
|
||||
|
||||
group = game.add.group();
|
||||
|
||||
for (var i = 0; i < 50; i++)
|
||||
{
|
||||
var c = group.create(100 + Math.random() * 700, game.world.randomY, 'veggies', game.rnd.integerInRange(0, 36));
|
||||
c.name = 'veg' + i;
|
||||
c.body.immovable = true;
|
||||
}
|
||||
|
||||
for (var i = 0; i < 20; i++)
|
||||
{
|
||||
// Here we'll create some chillis which the player can pick-up. They are still part of the same Group.
|
||||
var c = group.create(100 + Math.random() * 700, game.world.randomY, 'veggies', 17);
|
||||
c.name = 'chilli' + i;
|
||||
c.body.immovable = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
sprite.velocity.x = 0;
|
||||
sprite.velocity.y = 0;
|
||||
|
||||
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
|
||||
{
|
||||
sprite.velocity.x = -200;
|
||||
}
|
||||
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
|
||||
{
|
||||
sprite.velocity.x = 200;
|
||||
}
|
||||
|
||||
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
|
||||
{
|
||||
sprite.velocity.y = -200;
|
||||
}
|
||||
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
|
||||
{
|
||||
sprite.velocity.y = 200;
|
||||
}
|
||||
|
||||
game.physics.collide(sprite, group, collisionHandler, null, this);
|
||||
|
||||
}
|
||||
|
||||
function collisionHandler (obj1, obj2) {
|
||||
|
||||
// If the player collides with the chillis then they get eaten :)
|
||||
// The chilli frame ID is 17
|
||||
|
||||
console.log('Hit', obj2.name);
|
||||
|
||||
if (obj2.frame == 17)
|
||||
{
|
||||
obj2.kill();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
})();
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -30,20 +30,25 @@
|
|||
|
||||
// This will check Sprite vs. Sprite collision using a custom process callback
|
||||
|
||||
sprite1 = game.add.sprite(50, 200, 'atari');
|
||||
sprite1 = game.add.sprite(0, 200, 'atari');
|
||||
sprite1.name = 'atari';
|
||||
sprite1.body.velocity.x = 100;
|
||||
// We'll use a random velocity here so we can test it in our processHandler
|
||||
sprite1.body.velocity.x = 50 + Math.random() * 100;
|
||||
// This tells phaser to not use the built-in body separation, instead you should handle it in your process callback (see below)
|
||||
sprite1.body.customSeparateX = true;
|
||||
|
||||
sprite2 = game.add.sprite(700, 220, 'mushroom');
|
||||
sprite2 = game.add.sprite(750, 220, 'mushroom');
|
||||
sprite2.name = 'mushroom';
|
||||
sprite2.body.velocity.x = -100;
|
||||
// We'll use a random velocity here so we can test it in our processHandler
|
||||
sprite2.body.velocity.x = -(50 + Math.random() * 100);
|
||||
// This tells phaser to not use the built-in body separation, instead you should handle it in your process callback (see below)
|
||||
sprite2.body.customSeparateX = true;
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
// object1, object2, collideCallback, processCallback, callbackContext
|
||||
game.physics.collide(sprite1, sprite2, collisionHandler, null, this);
|
||||
game.physics.collide(sprite1, sprite2, collisionHandler, processHandler, this);
|
||||
|
||||
}
|
||||
|
||||
|
@ -53,7 +58,23 @@
|
|||
// For example you could test for velocity, health, etc.
|
||||
// If you want the collision to be deemed successful this function must return true.
|
||||
// In which case the collisionHandler will be called, otherwise it won't.
|
||||
// Note: the objects will have already collided and separated by this point.
|
||||
|
||||
// Note: the objects will have already been separated by this point unless you have set
|
||||
// their customSeparateX/Y flags to true. If you do that it's up to you to handle separation.
|
||||
|
||||
// Whichever one is going fastest wins, the other dies :)
|
||||
if (obj1.body.velocity.x > Math.abs(obj2.body.velocity.x))
|
||||
{
|
||||
obj2.kill();
|
||||
obj1.body.velocity.x = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
obj1.kill();
|
||||
obj2.body.velocity.x = 0;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -167,6 +167,9 @@ Phaser.Sprite = function (game, x, y, key, frame) {
|
|||
// Set-up the physics body
|
||||
this.body = new Phaser.Physics.Arcade.Body(this);
|
||||
|
||||
this.velocity = this.body.velocity;
|
||||
this.acceleration = this.body.acceleration;
|
||||
|
||||
// World bounds check
|
||||
this.inWorld = Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds);
|
||||
this.inWorldThreshold = 0;
|
||||
|
|
|
@ -424,11 +424,12 @@ Phaser.Physics.Arcade.prototype = {
|
|||
*/
|
||||
separate: function (body1, body2) {
|
||||
|
||||
if (this.separateX(body1, body2) || this.separateY(body1, body2))
|
||||
this._result = (this.separateX(body1, body2) || this.separateY(body1, body2));
|
||||
|
||||
if (this._result)
|
||||
{
|
||||
body1.postUpdate();
|
||||
body2.postUpdate();
|
||||
this._result = true;
|
||||
}
|
||||
|
||||
},
|
||||
|
@ -496,12 +497,21 @@ Phaser.Physics.Arcade.prototype = {
|
|||
// Then adjust their positions and velocities accordingly (if there was any overlap)
|
||||
if (this._overlap != 0)
|
||||
{
|
||||
body1.overlapX = this._overlap;
|
||||
body2.overlapX = this._overlap;
|
||||
|
||||
if (body1.customSeparateX || body2.customSeparateX)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
this._velocity1 = body1.velocity.x;
|
||||
this._velocity2 = body2.velocity.x;
|
||||
|
||||
if (!body1.immovable && !body2.immovable)
|
||||
{
|
||||
this._overlap *= 0.5;
|
||||
|
||||
body1.x = body1.x - this._overlap;
|
||||
body2.x += this._overlap;
|
||||
|
||||
|
@ -510,6 +520,7 @@ Phaser.Physics.Arcade.prototype = {
|
|||
this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
|
||||
this._newVelocity1 -= this._average;
|
||||
this._newVelocity2 -= this._average;
|
||||
|
||||
body1.velocity.x = this._average + this._newVelocity1 * body1.bounce.x;
|
||||
body2.velocity.x = this._average + this._newVelocity2 * body2.bounce.x;
|
||||
}
|
||||
|
@ -595,12 +606,21 @@ Phaser.Physics.Arcade.prototype = {
|
|||
// Then adjust their positions and velocities accordingly (if there was any overlap)
|
||||
if (this._overlap != 0)
|
||||
{
|
||||
body1.overlapY = this._overlap;
|
||||
body2.overlapY = this._overlap;
|
||||
|
||||
if (body1.customSeparateY || body2.customSeparateY)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
this._velocity1 = body1.velocity.y;
|
||||
this._velocity2 = body2.velocity.y;
|
||||
|
||||
if (!body1.immovable && !body2.immovable)
|
||||
{
|
||||
this._overlap *= 0.5;
|
||||
|
||||
body1.y = body1.y - this._overlap;
|
||||
body2.y += this._overlap;
|
||||
|
||||
|
@ -609,6 +629,7 @@ Phaser.Physics.Arcade.prototype = {
|
|||
this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
|
||||
this._newVelocity1 -= this._average;
|
||||
this._newVelocity2 -= this._average;
|
||||
|
||||
body1.velocity.y = this._average + this._newVelocity1 * body1.bounce.y;
|
||||
body2.velocity.y = this._average + this._newVelocity2 * body2.bounce.y;
|
||||
}
|
||||
|
@ -616,6 +637,7 @@ Phaser.Physics.Arcade.prototype = {
|
|||
{
|
||||
body1.y = body1.y - this._overlap;
|
||||
body1.velocity.y = this._velocity2 - this._velocity1 * body1.bounce.y;
|
||||
|
||||
// This is special case code that handles things like horizontal moving platforms you can ride
|
||||
if (body2.active && body2.moves && (body1.deltaY() > body2.deltaY()))
|
||||
{
|
||||
|
@ -626,6 +648,7 @@ Phaser.Physics.Arcade.prototype = {
|
|||
{
|
||||
body2.y += this._overlap;
|
||||
body2.velocity.y = this._velocity1 - this._velocity2 * body2.bounce.y;
|
||||
|
||||
// This is special case code that handles things like horizontal moving platforms you can ride
|
||||
if (body1.sprite.active && body1.moves && (body1.deltaY() < body2.deltaY()))
|
||||
{
|
||||
|
|
|
@ -49,6 +49,17 @@ Phaser.Physics.Arcade.Body = function (sprite) {
|
|||
this.allowRotation = true;
|
||||
this.allowGravity = true;
|
||||
|
||||
// These two flags allow you to disable the custom separation that takes place
|
||||
// Used in combination with your own collision processHandler you can create whatever
|
||||
// type of collision response you need.
|
||||
this.customSeparateX = false;
|
||||
this.customSeparateY = false;
|
||||
|
||||
// When this body collides with another the amount of overlap is stored in here
|
||||
// These values are useful if you want to provide your own custom separation logic.
|
||||
this.overlapX = 0;
|
||||
this.overlapY = 0;
|
||||
|
||||
this.collideWorldBounds = false;
|
||||
|
||||
this.lastX = sprite.x;
|
||||
|
|
Loading…
Reference in a new issue