Skip to content

Commit 0fad46e

Browse files
committed
Merged updateMotion with new PhysicsManager
1 parent c1eafe8 commit 0fad46e

7 files changed

Lines changed: 636 additions & 462 deletions

File tree

Phaser/components/sprite/Physics.ts

Lines changed: 28 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ module Phaser.Components {
1717
this._game = parent.game;
1818
this._sprite = parent;
1919

20+
this.gravityFactor = new Vec2(1, 1);
21+
this.drag = new Vec2(0, 0);
22+
this.bounce = new Vec2(0, 0);
23+
this.friction = new Vec2(0.05, 0.05);
24+
this.velocity = new Vec2(0, 0);
25+
this.acceleration = new Vec2(0, 0);
26+
2027
//this.AABB = new Phaser.Physics.AABB(this._game, this._sprite, this._sprite.x, this._sprite.y, this._sprite.width, this._sprite.height);
2128
this.AABB = 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));
2229

@@ -41,128 +48,17 @@ module Phaser.Components {
4148
public immovable: bool;
4249

4350
/**
44-
* Basic speed of this object.
45-
*
46-
* Velocity is given in pixels per second. Therefore a velocity of
47-
* 100 will move at a rate of 100 pixels every 1000 ms (1sec). It's not balls-on
48-
* accurate due to the way timers work, but it's pretty close. Expect tolerance
49-
* of +- 10 px. Also that speed assumes no drag.
50-
*
51-
* @type {Vec2}
52-
*/
53-
public velocity: Vec2;
54-
55-
/**
56-
* The virtual mass of the object.
57-
* @type {number}
58-
*/
59-
public mass: number;
60-
61-
/**
62-
* The bounciness of the object.
63-
* @type {number}
64-
*/
65-
public elasticity: number;
66-
67-
/**
68-
* How fast the speed of this object is changing.
69-
* @type {number}
70-
*/
71-
public acceleration: Vec2;
72-
73-
/**
74-
* This isn't drag exactly, more like deceleration that is only applied
75-
* when acceleration is not affecting the sprite.
76-
* @type {Vec2}
77-
*/
78-
public drag: Vec2;
79-
80-
/**
81-
* It will cap the speed automatically if you use the acceleration
82-
* to change its velocity.
83-
* @type {Vec2}
84-
*/
85-
public maxVelocity: Vec2;
86-
87-
/**
88-
* How fast this object is rotating.
89-
* @type {number}
90-
*/
91-
public angularVelocity: number;
92-
93-
/**
94-
* How fast angularVelocity of this object is changing.
95-
* @type {number}
96-
*/
97-
public angularAcceleration: number;
98-
99-
/**
100-
* Deacceleration of angularVelocity will be applied when it's rotating.
101-
* @type {number}
102-
*/
103-
public angularDrag: number;
104-
105-
/**
106-
* It will cap the rotate speed automatically if you use the angularAcceleration
107-
* to change its angularVelocity.
108-
* @type {number}
109-
*/
110-
public maxAngular: number;
111-
112-
/**
113-
* Set this to false if you want to skip the automatic motion/movement stuff
114-
* (see updateMotion()).
51+
* Set this to false if you want to skip the automatic movement stuff
11552
* @type {boolean}
11653
*/
11754
public moves: bool = true;
11855

119-
/**
120-
* Bit field of flags (use with UP, DOWN, LEFT, RIGHT, etc) indicating surface contacts.
121-
* @type {number}
122-
*/
123-
public touching: number;
124-
125-
/**
126-
* Bit field of flags (use with UP, DOWN, LEFT, RIGHT, etc) indicating surface contacts from the previous game loop step.
127-
* @type {number}
128-
*/
129-
public wasTouching: number;
130-
131-
/**
132-
* Bit field of flags (use with UP, DOWN, LEFT, RIGHT, etc) indicating collision directions.
133-
* @type {number}
134-
*/
135-
public allowCollisions: number;
136-
137-
/**
138-
* Important variable for collision processing.
139-
* @type {Vec2}
140-
*/
141-
public last: Vec2;
142-
143-
/**
144-
* Handy for checking if this object is touching a particular surface.
145-
* For slightly better performance you can just &amp; the value directly into <code>touching</code>.
146-
* However, this method is good for readability and accessibility.
147-
*
148-
* @param Direction {number} Any of the collision flags (e.g. LEFT, FLOOR, etc).
149-
*
150-
* @return {boolean} Whether the object is touching an object in (any of) the specified direction(s) this frame.
151-
*/
152-
//public isTouching(direction: number): bool {
153-
// return (this.touching & direction) > Collision.NONE;
154-
//}
155-
156-
/**
157-
* Handy function for checking if this object just landed on a particular surface.
158-
*
159-
* @param Direction {number} Any of the collision flags (e.g. LEFT, FLOOR, etc).
160-
*
161-
* @returns {boolean} Whether the object just landed on any specicied surfaces.
162-
*/
163-
//public justTouched(direction: number): bool {
164-
// return ((this.touching & direction) > Collision.NONE) && ((this.wasTouching & direction) <= Collision.NONE);
165-
//}
56+
public gravityFactor: Vec2;
57+
public drag: Vec2;
58+
public bounce: Vec2;
59+
public friction: Vec2;
60+
public velocity: Vec2;
61+
public acceleration: Vec2;
16662

16763

16864
/**
@@ -174,51 +70,24 @@ module Phaser.Components {
17470
{
17571
this._sprite.x = this.AABB.position.x - this.AABB.halfWidth;
17672
this._sprite.y = this.AABB.position.y - this.AABB.halfHeight;
73+
//this._sprite.x = this.AABB.position.x;
74+
//this._sprite.y = this.AABB.position.y;
17775
}
178-
179-
/*
180-
var delta: number;
181-
var velocityDelta: number;
182-
183-
velocityDelta = (this._game.motion.computeVelocity(this.angularVelocity, this.angularAcceleration, this.angularDrag, this.maxAngular) - this.angularVelocity) / 2;
184-
this.angularVelocity += velocityDelta;
185-
this._angle += this.angularVelocity * this._game.time.elapsed;
186-
this.angularVelocity += velocityDelta;
187-
188-
velocityDelta = (this._game.motion.computeVelocity(this.velocity.x, this.acceleration.x, this.drag.x, this.maxVelocity.x) - this.velocity.x) / 2;
189-
this.velocity.x += velocityDelta;
190-
delta = this.velocity.x * this._game.time.elapsed;
191-
this.velocity.x += velocityDelta;
192-
this.frameBounds.x += delta;
193-
194-
velocityDelta = (this._game.motion.computeVelocity(this.velocity.y, this.acceleration.y, this.drag.y, this.maxVelocity.y) - this.velocity.y) / 2;
195-
this.velocity.y += velocityDelta;
196-
delta = this.velocity.y * this._game.time.elapsed;
197-
this.velocity.y += velocityDelta;
198-
this.frameBounds.y += delta;
199-
*/
200-
20176
}
20277

20378
/**
204-
* Whether the object collides or not. For more control over what directions
205-
* the object will collide from, use collision constants (like LEFT, FLOOR, etc)
206-
* to set the value of allowCollisions directly.
207-
*/
208-
//public get solid(): bool {
209-
// return (this.allowCollisions & Collision.ANY) > Collision.NONE;
210-
//}
211-
212-
public set solid(value: bool) {
213-
214-
//if (value)
215-
//{
216-
// this.allowCollisions = Collision.ANY;
217-
//}
218-
//else
219-
//{
220-
// this.allowCollisions = Collision.NONE;
221-
//}
79+
* Render debug infos. (including name, bounds info, position and some other properties)
80+
* @param x {number} X position of the debug info to be rendered.
81+
* @param y {number} Y position of the debug info to be rendered.
82+
* @param [color] {number} color of the debug info to be rendered. (format is css color string)
83+
*/
84+
public renderDebugInfo(x: number, y: number, color?: string = 'rgb(255,255,255)') {
85+
86+
this._sprite.texture.context.fillStyle = color;
87+
this._sprite.texture.context.fillText('Sprite: (' + this._sprite.frameBounds.width + ' x ' + this._sprite.frameBounds.height + ')', x, y);
88+
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);
89+
this._sprite.texture.context.fillText('vx: ' + this.velocity.x.toFixed(1) + ' vy: ' + this.velocity.y.toFixed(1), x, y + 28);
90+
this._sprite.texture.context.fillText('ax: ' + this.acceleration.x.toFixed(1) + ' ay: ' + this.acceleration.y.toFixed(1), x, y + 42);
22291

22392
}
22493

0 commit comments

Comments
 (0)