-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhysicsObject.js
More file actions
116 lines (107 loc) · 4.03 KB
/
PhysicsObject.js
File metadata and controls
116 lines (107 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var PhysicsObject = SceneObject.extend(function(args) {
if(args === undefined) args = {};
SceneObject.prototype.constructor.call(this,args);
this.visible = (args.visible !== undefined)? args.visible : true;
this.solid = (args.solid !== undefined)? args.solid : true;
this.x_acc = (args.xAcceleration !== undefined)? args.xAcceleration : 0;
this.y_acc = (args.yAcceleration !== undefined)? args.yAcceleration : 0;
this.x_vel = (args.xVelocity !== undefined)? args.xVelocity : 0;
this.y_vel = (args.yVelocity !== undefined)? args.yVelocity : 0;
this.collision_block = (args.collisionBlock !== undefined)? args.collisionBlock : undefined;
if(!(this.collision_block instanceof CollisionBlock)) this.collision_block = undefined;
if(this.collision_block !== undefined) {
this.width = this.collision_block.width;
this.height = this.collision_block.height;
}
this.last_time_step = 0;
});
PhysicsObject.prototype.constructor = PhysicsObject;
PhysicsObject.prototype.setAcceleration = function(args) {
this.x_acc = (args.xAcceleration !== undefined)? args.xAcceleration : this.x_acc;
this.y_acc = (args.yAcceleration !== undefined)? args.yAcceleration : this.y_acc;
this.x_acc = (args.x !== undefined)? args.x : this.x_acc;
this.y_acc = (args.y !== undefined)? args.y : this.y_acc;
}
PhysicsObject.prototype.setVelocity = function(args) {
this.x_vel = (args.xVelocity !== undefined)? args.xVelocity : this.x_vel;
this.y_vel = (args.yVelocity !== undefined)? args.yVelocity : this.y_vel;
this.x_vel = (args.x !== undefined)? args.x : this.x_vel;
this.y_vel = (args.y !== undefined)? args.y : this.y_vel;
}
PhysicsObject.prototype.setXAcceleration = function(xAcc) { this.x_acc = xAcc; }
PhysicsObject.prototype.setYAcceleration = function(yAcc) { this.y_acc = yAcc; }
PhysicsObject.prototype.setXVelocity = function(xVel) { this.x_vel = xVel; }
PhysicsObject.prototype.setYVelocity = function(yVel) { this.y_vel = yVel; }
PhysicsObject.prototype.update = function(args) {
if(args === undefined) args = {};
var time_step = (args.ms !== undefined)? args.ms : 100;
time_step = time_step / 1000; /* convert to seconds */
this.x_vel += this.x_acc * time_step;
this.y_vel += this.y_acc * time_step;
this.x += this.x_vel * time_step;
this.y += this.y_vel * time_step;
this.last_time_step = time_step;
for(var i=0; i<this.controllers.length; i++) this.controllers[i].update(args);
};
PhysicsObject.prototype.getCollisionBlock = function() { return this.collision_block; }
PhysicsObject.prototype.isOverlapping = function(sceneObject) {
if(this.collision_block === undefined) return false;
return (this.collision_block.isOverlapping({
collisionBlock: sceneObject.getCollisionBlock(),
xOffset: sceneObject.x - this.x,
yOffset: sceneObject.y - this.y
}));
}
PhysicsObject.prototype.onCollision = function(sceneObject) {
if(sceneObject.solid) {
var x_blocked = true;
var y_blocked = true;
this.x -= this.x_vel * this.last_time_step;
this.y -= this.y_vel * this.last_time_step;
var max_x = this.x_vel * this.last_time_step;
var max_y = this.y_vel * this.last_time_step;
/* TODO: implement move-around-obstacle behaviour */
this.x += max_x;
if(!this.isOverlapping(sceneObject)) x_blocked = false;
this.x -= max_x;
this.y += max_y;
if(!this.isOverlapping(sceneObject)) y_blocked = false;
this.y -= max_y;
if(!x_blocked) this.x += max_x;
else {
if(max_x > 0) {
for(var i=max_x; i>0; i--) {
this.x += i;
if(!this.isOverlapping(sceneObject)) break;
this.x -= i;
}
}
else if(max_x < 0) {
for(var i=max_x; i<0; i++) {
this.x += i;
if(!this.isOverlapping(sceneObject)) break;
this.x -= i;
}
}
}
if(!y_blocked) this.y += max_y;
else {
if(max_y > 0) {
for(var i=max_y; i>0; i--) {
this.y += i;
if(!this.isOverlapping(sceneObject)) break;
this.y -= i;
}
}
else if(max_y < 0) {
for(var i=max_y; i<0; i++) {
this.y += i;
if(!this.isOverlapping(sceneObject)) break;
this.y -= i;
}
}
}
if(x_blocked) this.x_vel = 0;
if(y_blocked) this.y_vel = 0;
}
}