Skip to content

Commit 1184d8b

Browse files
committed
PointProxy added to allow for easy setting of force and velocity. More p2 tests done. World update done.
1 parent 2de9347 commit 1184d8b

8 files changed

Lines changed: 456 additions & 47 deletions

File tree

build/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
<script src="$path/src/utils/Color.js"></script>
180180
181181
<script src="$path/src/physics/World.js"></script>
182+
<script src="$path/src/physics/PointProxy.js"></script>
182183
<script src="$path/src/physics/Body.js"></script>
183184
184185
<script src="$path/src/particles/Particles.js"></script>

examples/wip/p22.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function preload() {
99

1010
var box;
1111
var box2;
12+
var cursors;
1213

1314
function p2px(v) {
1415
return v *= -20;
@@ -24,7 +25,13 @@ function create() {
2425

2526
box2 = game.add.sprite(400, 0, 'box');
2627
box2.physicsEnabled = true;
27-
box2.body.mass = 2;
28+
29+
box2.body.setZeroDamping();
30+
31+
cursors = game.input.keyboard.createCursorKeys();
32+
33+
34+
// box2.body.mass = 2;
2835

2936
/*
3037
// Add a plane
@@ -39,10 +46,33 @@ function create() {
3946

4047
function update() {
4148

49+
box2.body.setZeroVelocity();
50+
51+
if (cursors.left.isDown)
52+
{
53+
box2.body.moveLeft(200);
54+
}
55+
else if (cursors.right.isDown)
56+
{
57+
box2.body.moveRight(200);
58+
}
59+
60+
if (cursors.up.isDown)
61+
{
62+
box2.body.moveUp(200);
63+
}
64+
else if (cursors.down.isDown)
65+
{
66+
box2.body.moveDown(200);
67+
}
68+
4269
}
4370

4471
function render() {
4572

73+
game.debug.renderText('x: ' + box2.body.velocity.x, 32, 32);
74+
game.debug.renderText('y: ' + box2.body.velocity.y, 32, 64);
75+
4676
// game.debug.renderText('x: ' + p2px(boxBody.position[0]), 32, 32);
4777
// game.debug.renderText('y: ' + p2px(boxBody.position[1]), 32, 64);
4878
// game.debug.renderText('r: ' + boxBody.angle, 32, 96);

examples/wip/p23.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
3+
4+
function preload() {
5+
6+
game.load.image('box', 'assets/sprites/block.png');
7+
8+
}
9+
10+
var box;
11+
var move = false;
12+
var start = 0;
13+
var end = 0;
14+
15+
function create() {
16+
17+
box = game.add.sprite(200, 200, 'box');
18+
box.physicsEnabled = true;
19+
20+
box.body.setZeroDamping();
21+
22+
game.input.onDown.addOnce(startTiming, this);
23+
24+
}
25+
26+
function startTiming() {
27+
28+
start = game.time.now;
29+
end = start + 1000;
30+
move = true;
31+
32+
}
33+
34+
function update() {
35+
36+
if (move)
37+
{
38+
box.body.moveLeft(100);
39+
40+
if (game.time.now >= end)
41+
{
42+
move = false;
43+
var duration = game.time.now - start;
44+
console.log('Test over. Distance: ', box.x, 'duration', duration);
45+
}
46+
}
47+
else
48+
{
49+
box.body.setZeroVelocity();
50+
}
51+
52+
}
53+
54+
function render() {
55+
56+
game.debug.renderText('x: ' + box.body.velocity.x, 32, 32);
57+
game.debug.renderText('y: ' + box.body.velocity.y, 32, 64);
58+
59+
}

examples/wip/p24.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
3+
4+
function preload() {
5+
6+
game.load.image('box', 'assets/sprites/block.png');
7+
8+
}
9+
10+
var box;
11+
var box2;
12+
var cursors;
13+
14+
function p2px(v) {
15+
return v *= -20;
16+
}
17+
18+
function px2p(v) {
19+
return v * -0.05;
20+
}
21+
22+
function create() {
23+
24+
box = game.add.sprite(200, 200, 'box');
25+
box.anchor.set(0.5);
26+
box.physicsEnabled = true;
27+
28+
box2 = game.add.sprite(400, 100, 'box');
29+
box2.anchor.set(0.5);
30+
box2.physicsEnabled = true;
31+
32+
box2.body.setZeroDamping();
33+
box2.body.fixedRotation = true;
34+
35+
cursors = game.input.keyboard.createCursorKeys();
36+
37+
game.physics.defaultRestitution = 0.8;
38+
39+
var top = new p2.Body({ mass: 0, position:[0, 0], angle: game.math.degToRad(-180) });
40+
top.addShape(new p2.Plane());
41+
game.physics.addBody(top);
42+
43+
var bottom = new p2.Body({ mass: 0, position:[0, px2p(600)] });
44+
bottom.addShape(new p2.Plane());
45+
game.physics.addBody(bottom);
46+
47+
var left = new p2.Body({ mass: 0, position:[0, 0], angle: game.math.degToRad(90) });
48+
left.addShape(new p2.Plane());
49+
game.physics.addBody(left);
50+
51+
var right = new p2.Body({ mass: 0, position:[px2p(800), 0], angle: game.math.degToRad(-90) });
52+
right.addShape(new p2.Plane());
53+
game.physics.addBody(right);
54+
55+
}
56+
57+
function update() {
58+
59+
box2.body.setZeroVelocity();
60+
61+
if (cursors.left.isDown)
62+
{
63+
box2.body.moveLeft(400);
64+
}
65+
else if (cursors.right.isDown)
66+
{
67+
box2.body.moveRight(400);
68+
}
69+
70+
if (cursors.up.isDown)
71+
{
72+
box2.body.moveUp(400);
73+
}
74+
else if (cursors.down.isDown)
75+
{
76+
box2.body.moveDown(400);
77+
}
78+
79+
}
80+
81+
function render() {
82+
83+
game.debug.renderText('x: ' + box2.body.velocity.x, 32, 32);
84+
game.debug.renderText('y: ' + box2.body.velocity.y, 32, 64);
85+
86+
// game.debug.renderText('x: ' + p2px(boxBody.position[0]), 32, 32);
87+
// game.debug.renderText('y: ' + p2px(boxBody.position[1]), 32, 64);
88+
// game.debug.renderText('r: ' + boxBody.angle, 32, 96);
89+
90+
// drawbox();
91+
92+
}
93+
94+
function drawbox() {
95+
96+
// var ctx = game.context;
97+
98+
/*
99+
ctx.save();
100+
ctx.translate(game.width/2, game.height/2); // Translate to the center
101+
ctx.scale(50, -50); // Zoom in and flip y axis
102+
103+
ctx.lineWidth = 0.05;
104+
ctx.strokeStyle = 'rgb(255,255,255)';
105+
106+
107+
ctx.beginPath();
108+
var x = boxBody.position[0],
109+
y = boxBody.position[1];
110+
ctx.save();
111+
ctx.translate(x, y); // Translate to the center of the box
112+
ctx.rotate(boxBody.angle); // Rotate to the box body frame
113+
ctx.rect(-boxShape.width/2, -boxShape.height/2, boxShape.width, boxShape.height);
114+
ctx.stroke();
115+
ctx.closePath();
116+
// ctx.restore();
117+
*/
118+
119+
// ctx.save();
120+
// ctx.translate(game.width/2, game.height/2); // Translate to the center
121+
// ctx.scale(20, -20); // Zoom in and flip y axis
122+
123+
// ctx.lineWidth = 0.05;
124+
// ctx.strokeStyle = 'rgb(255,255,255)';
125+
// ctx.beginPath();
126+
127+
// var y = planeBody.position[1];
128+
// ctx.rotate(0); // Rotate to the box body frame
129+
// ctx.moveTo(-game.width, y);
130+
// ctx.lineTo( game.width, y);
131+
// ctx.stroke();
132+
133+
// ctx.closePath();
134+
// ctx.restore();
135+
}
136+

src/core/Game.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,6 @@ Phaser.Game.prototype = {
447447
this.tweens = new Phaser.TweenManager(this);
448448
this.input = new Phaser.Input(this);
449449
this.sound = new Phaser.SoundManager(this);
450-
// this.physics = new Phaser.Physics.Arcade(this);
451450
this.physics = new Phaser.Physics.World(this);
452451
this.particles = new Phaser.Particles(this);
453452
this.plugins = new Phaser.PluginManager(this, this);
@@ -616,11 +615,11 @@ Phaser.Game.prototype = {
616615
this.tweens.update();
617616
this.sound.update();
618617
this.input.update();
619-
this.physics.update();
620618
this.state.update();
621-
this.world.update();
619+
this.physics.update();
622620
this.particles.update();
623621
this.plugins.update();
622+
this.world.update();
624623

625624
this.world.postUpdate();
626625
this.plugins.postUpdate();

0 commit comments

Comments
 (0)