Skip to content

Commit 3e53c06

Browse files
committed
About to start re-doing collideShapes
1 parent 498725c commit 3e53c06

16 files changed

Lines changed: 1471 additions & 316 deletions

File tree

Phaser/Phaser.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@
116116
<DependentUpon>AABB.ts</DependentUpon>
117117
</Content>
118118
<TypeScriptCompile Include="physics\IPhysicsShape.ts" />
119+
<TypeScriptCompile Include="physics\Circle.ts" />
120+
<Content Include="physics\Circle.js">
121+
<DependentUpon>Circle.ts</DependentUpon>
122+
</Content>
119123
<Content Include="physics\IPhysicsShape.js">
120124
<DependentUpon>IPhysicsShape.ts</DependentUpon>
121125
</Content>

Phaser/Statics.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,60 @@ module Phaser {
2424
static GEOM_LINE: number = 3;
2525
static GEOM_POLYGON: number = 4;
2626

27+
/**
28+
* Flag used to allow GameObjects to collide on their left side
29+
* @type {number}
30+
*/
31+
static LEFT: number = 0x0001;
32+
33+
/**
34+
* Flag used to allow GameObjects to collide on their right side
35+
* @type {number}
36+
*/
37+
static RIGHT: number = 0x0010;
38+
39+
/**
40+
* Flag used to allow GameObjects to collide on their top side
41+
* @type {number}
42+
*/
43+
static UP: number = 0x0100;
44+
45+
/**
46+
* Flag used to allow GameObjects to collide on their bottom side
47+
* @type {number}
48+
*/
49+
static DOWN: number = 0x1000;
50+
51+
/**
52+
* Flag used with GameObjects to disable collision
53+
* @type {number}
54+
*/
55+
static NONE: number = 0;
56+
57+
/**
58+
* Flag used to allow GameObjects to collide with a ceiling
59+
* @type {number}
60+
*/
61+
static CEILING: number = Types.UP;
62+
63+
/**
64+
* Flag used to allow GameObjects to collide with a floor
65+
* @type {number}
66+
*/
67+
static FLOOR: number = Types.DOWN;
68+
69+
/**
70+
* Flag used to allow GameObjects to collide with a wall (same as LEFT+RIGHT)
71+
* @type {number}
72+
*/
73+
static WALL: number = Types.LEFT | Types.RIGHT;
74+
75+
/**
76+
* Flag used to allow GameObjects to collide on any face
77+
* @type {number}
78+
*/
79+
static ANY: number = Types.LEFT | Types.RIGHT | Types.UP | Types.DOWN;
80+
2781
}
2882

2983
}

Phaser/components/sprite/Physics.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
/// <reference path="../../core/Point.ts" />
33
/// <reference path="../../math/Vec2Utils.ts" />
44
/// <reference path="../../physics/AABB.ts" />
5+
/// <reference path="../../physics/Circle.ts" />
6+
/// <reference path="../../physics/IPhysicsShape.ts" />
57

68
/**
79
* Phaser - Components - Physics
@@ -23,6 +25,7 @@ module Phaser.Components {
2325

2426
this.velocity = new Vec2;
2527
this.acceleration = new Vec2;
28+
this.touching = Phaser.Types.NONE;
2629

2730
this.shape = this.game.world.physics.add(new Phaser.Physics.AABB(this.game, this._sprite, this._sprite.x, this._sprite.y, this._sprite.width, this._sprite.height));
2831

@@ -37,7 +40,7 @@ module Phaser.Components {
3740
* Whether this object will be moved by impacts with other objects or not.
3841
* @type {boolean}
3942
*/
40-
public immovable: bool;
43+
public immovable: bool = false;
4144

4245
/**
4346
* Set this to false if you want to skip the automatic movement stuff
@@ -52,12 +55,22 @@ module Phaser.Components {
5255
public velocity: Vec2;
5356
public acceleration: Vec2;
5457

58+
public touching: number;
59+
60+
public setCircle(diameter: number) {
61+
62+
this.game.world.physics.remove(this.shape);
63+
this.shape = this.game.world.physics.add(new Phaser.Physics.Circle(this.game, this._sprite, this._sprite.x, this._sprite.y, diameter));
64+
this._sprite.physics.shape.physics = this;
65+
66+
}
67+
5568
/**
5669
* Internal function for updating the position and speed of this object.
5770
*/
5871
public update() {
5972

60-
if (this.moves)
73+
if (this.moves && this.shape)
6174
{
6275
this._sprite.x = (this.shape.position.x - this.shape.bounds.halfWidth) - this.shape.offset.x;
6376
this._sprite.y = (this.shape.position.y - this.shape.bounds.halfHeight) - this.shape.offset.y;
@@ -74,7 +87,8 @@ module Phaser.Components {
7487

7588
this._sprite.texture.context.fillStyle = color;
7689
this._sprite.texture.context.fillText('Sprite: (' + this._sprite.frameBounds.width + ' x ' + this._sprite.frameBounds.height + ')', x, y);
77-
this._sprite.texture.context.fillText('x: ' + this._sprite.frameBounds.x.toFixed(1) + ' y: ' + this._sprite.frameBounds.y.toFixed(1) + ' rotation: ' + this._sprite.rotation.toFixed(1), x, y + 14);
90+
//this._sprite.texture.context.fillText('x: ' + this._sprite.frameBounds.x.toFixed(1) + ' y: ' + this._sprite.frameBounds.y.toFixed(1) + ' rotation: ' + this._sprite.rotation.toFixed(1), x, y + 14);
91+
this._sprite.texture.context.fillText('x: ' + this.shape.bounds.x.toFixed(1) + ' y: ' + this.shape.bounds.y.toFixed(1) + ' rotation: ' + this._sprite.rotation.toFixed(1), x, y + 14);
7892
this._sprite.texture.context.fillText('vx: ' + this.velocity.x.toFixed(1) + ' vy: ' + this.velocity.y.toFixed(1), x, y + 28);
7993
this._sprite.texture.context.fillText('ax: ' + this.acceleration.x.toFixed(1) + ' ay: ' + this.acceleration.y.toFixed(1), x, y + 42);
8094

Phaser/core/Circle.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ module Phaser {
2727

2828
private _diameter: number = 0;
2929
private _radius: number = 0;
30-
//private _pos: Vec2;
3130

3231
/**
3332
* The x coordinate of the center of the circle

Phaser/physics/AABB.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ module Phaser.Physics {
5353
public preUpdate() {
5454

5555
this.oldPosition.copyFrom(this.position);
56+
57+
this.bounds.x = this.position.x - this.bounds.halfWidth;
58+
this.bounds.y = this.position.y - this.bounds.halfHeight;
5659

5760
if (this.sprite)
5861
{
@@ -71,8 +74,8 @@ module Phaser.Physics {
7174

7275
public update() {
7376

74-
this.bounds.x = this.position.x;
75-
this.bounds.y = this.position.y;
77+
//this.bounds.x = this.position.x;
78+
//this.bounds.y = this.position.y;
7679

7780
}
7881

@@ -85,6 +88,12 @@ module Phaser.Physics {
8588

8689
public render(context:CanvasRenderingContext2D) {
8790

91+
//context.beginPath();
92+
//context.strokeStyle = 'rgb(255,255,0)';
93+
//context.strokeRect(this.bounds.x, this.bounds.y, this.bounds.width, this.bounds.height);
94+
//context.stroke();
95+
//context.closePath();
96+
8897
context.beginPath();
8998
context.strokeStyle = 'rgb(0,255,0)';
9099
context.strokeRect(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight, this.bounds.width, this.bounds.height);
@@ -95,7 +104,7 @@ module Phaser.Physics {
95104
context.fillStyle = 'rgb(0,255,0)';
96105
context.fillRect(this.position.x, this.position.y, 2, 2);
97106

98-
if (this.oH == 1)
107+
if (this.physics.touching == Phaser.Types.LEFT)
99108
{
100109
context.beginPath();
101110
context.strokeStyle = 'rgb(255,0,0)';
@@ -104,7 +113,7 @@ module Phaser.Physics {
104113
context.stroke();
105114
context.closePath();
106115
}
107-
else if (this.oH == -1)
116+
else if (this.physics.touching == Phaser.Types.RIGHT)
108117
{
109118
context.beginPath();
110119
context.strokeStyle = 'rgb(255,0,0)';
@@ -114,7 +123,7 @@ module Phaser.Physics {
114123
context.closePath();
115124
}
116125

117-
if (this.oV == 1)
126+
if (this.physics.touching == Phaser.Types.UP)
118127
{
119128
context.beginPath();
120129
context.strokeStyle = 'rgb(255,0,0)';
@@ -123,7 +132,7 @@ module Phaser.Physics {
123132
context.stroke();
124133
context.closePath();
125134
}
126-
else if (this.oV == -1)
135+
else if (this.physics.touching == Phaser.Types.DOWN)
127136
{
128137
context.beginPath();
129138
context.strokeStyle = 'rgb(255,0,0)';

Phaser/physics/Circle.ts

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/// <reference path="../Game.ts" />
2+
/// <reference path="../core/Rectangle.ts" />
3+
/// <reference path="../math/Vec2Utils.ts" />
4+
/// <reference path="PhysicsManager.ts" />
5+
/// <reference path="IPhysicsShape.ts" />
6+
7+
/**
8+
* Phaser - Physics - Circle
9+
*/
10+
11+
module Phaser.Physics {
12+
13+
export class Circle implements IPhysicsShape {
14+
15+
constructor(game: Game, sprite: Sprite, x: number, y: number, diameter: number) {
16+
17+
this.game = game;
18+
this.world = game.world.physics;
19+
20+
if (sprite !== null)
21+
{
22+
this.sprite = sprite;
23+
this.scale = Vec2Utils.clone(this.sprite.scale);
24+
}
25+
else
26+
{
27+
this.sprite = null;
28+
this.physics = null;
29+
this.scale = new Vec2(1, 1);
30+
}
31+
32+
this.radius = diameter / 2;
33+
this.bounds = new Rectangle(x + Math.round(diameter / 2), y + Math.round(diameter / 2), diameter, diameter);
34+
this.position = new Vec2(x + this.bounds.halfWidth, y + this.bounds.halfHeight);
35+
this.oldPosition = new Vec2(x + this.bounds.halfWidth, y + this.bounds.halfHeight);
36+
this.offset = new Vec2(0, 0);
37+
38+
}
39+
40+
public game: Game;
41+
public world: PhysicsManager;
42+
public sprite: Sprite;
43+
public physics: Phaser.Components.Physics;
44+
45+
public position: Vec2;
46+
public oldPosition: Vec2;
47+
public offset: Vec2;
48+
public scale: Vec2;
49+
public bounds: Rectangle;
50+
51+
public radius: number;
52+
public oH: number;
53+
public oV: number;
54+
55+
public preUpdate() {
56+
57+
this.oldPosition.copyFrom(this.position);
58+
59+
if (this.sprite)
60+
{
61+
this.position.setTo((this.sprite.x + this.bounds.halfWidth) + this.offset.x, (this.sprite.y + this.bounds.halfHeight) + this.offset.y);
62+
63+
// Update scale / dimensions
64+
if (Vec2Utils.equals(this.scale, this.sprite.scale) == false)
65+
{
66+
this.scale.copyFrom(this.sprite.scale);
67+
// needs to be radius based (+ square)
68+
//this.bounds.width = this.sprite.width;
69+
//this.bounds.height = this.sprite.height;
70+
}
71+
}
72+
73+
}
74+
75+
public update() {
76+
77+
this.bounds.x = this.position.x;
78+
this.bounds.y = this.position.y;
79+
80+
}
81+
82+
public setSize(width: number, height: number) {
83+
84+
this.bounds.width = width;
85+
this.bounds.height = height;
86+
87+
}
88+
89+
public render(context:CanvasRenderingContext2D) {
90+
91+
context.beginPath();
92+
context.strokeStyle = 'rgb(0,255,0)';
93+
context.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2);
94+
context.stroke();
95+
context.closePath();
96+
97+
// center point
98+
context.fillStyle = 'rgb(0,255,0)';
99+
context.fillRect(this.position.x, this.position.y, 2, 2);
100+
101+
if (this.oH == 1)
102+
{
103+
context.beginPath();
104+
context.strokeStyle = 'rgb(255,0,0)';
105+
context.moveTo(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight);
106+
context.lineTo(this.position.x - this.bounds.halfWidth, this.position.y + this.bounds.halfHeight);
107+
context.stroke();
108+
context.closePath();
109+
}
110+
else if (this.oH == -1)
111+
{
112+
context.beginPath();
113+
context.strokeStyle = 'rgb(255,0,0)';
114+
context.moveTo(this.position.x + this.bounds.halfWidth, this.position.y - this.bounds.halfHeight);
115+
context.lineTo(this.position.x + this.bounds.halfWidth, this.position.y + this.bounds.halfHeight);
116+
context.stroke();
117+
context.closePath();
118+
}
119+
120+
if (this.oV == 1)
121+
{
122+
context.beginPath();
123+
context.strokeStyle = 'rgb(255,0,0)';
124+
context.moveTo(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight);
125+
context.lineTo(this.position.x + this.bounds.halfWidth, this.position.y - this.bounds.halfHeight);
126+
context.stroke();
127+
context.closePath();
128+
}
129+
else if (this.oV == -1)
130+
{
131+
context.beginPath();
132+
context.strokeStyle = 'rgb(255,0,0)';
133+
context.moveTo(this.position.x - this.bounds.halfWidth, this.position.y + this.bounds.halfHeight);
134+
context.lineTo(this.position.x + this.bounds.halfWidth, this.position.y + this.bounds.halfHeight);
135+
context.stroke();
136+
context.closePath();
137+
}
138+
139+
}
140+
141+
}
142+
143+
}

0 commit comments

Comments
 (0)