-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex2.html
More file actions
166 lines (102 loc) · 5.63 KB
/
Copy pathindex2.html
File metadata and controls
166 lines (102 loc) · 5.63 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html>
<html>
<body>
<h1>Using Physics Engine 2D to apply Kinematic Motion</h1>
<h2>Using p5 collision.js to check for collision between the bottom boundary and the canvas obj (coin)</h2>
<canvas id='canvas' height=500 width=700></canvas>
<script type="text/javascript" src="https://rawgit.com/marcustansoon/PhysicsEngine/master/src/PhysicsEngine.js"></script>
<script type="text/javascript" src='https://rawgit.com/marcustansoon/PhysicsEngine.js-2D/master/src/p5.collide2d.js'></script>
<div id = 'motion'></div>
<script>
PhysicsEngineTick.Start();//Start the Physics Engine Time Tick
var Canvas=document.getElementById('canvas');
var Context=Canvas.getContext('2d');//Get canvas
var stringB=[];
var stringA=[];
function Animation(){
Context.clearRect(0, 0, Canvas.width, Canvas.height); //clear canvas
Context.font = "10px Arial";//set font size
for (let x=0;x<=4;x++){//display motion status on canvas
Context.fillText(stringA[x],Coin.x-40,Coin.y+x*10-20);
Context.fillText(stringB[x],Coin.x+110,Coin.y+x*10-20);
}
Context.drawImage(CoinImage,//Image
0, //Source Starting Point x
0, //Source Starting Point y
CoinImage.height,//Image height
CoinImage.width,//Image width
Coin.x,//Position x of the image
Coin.y,//Position y of the image
CoinImage.height,//Image height
CoinImage.width//Image width
);
Bounce();//Induce upward motion (bouncing ) when the Coin's height reaches certain point
if (!Coin.CheckIfKinematicComponentExists('AnyName'))//Check if 'AnyName' component is removed
{
stringB[0]='Component Name : AnyName -REMOVED-';
stringB[1]='Velocity : 0';
stringB[2]='Acceleration : 0';
stringB[3]='Angle : 0';
stringB[4]='Motion Expire Time : 0';
}
requestAnimationFrame(Animation);//Redraw canvas
}
function YourObj (x,y){//An Object with X and Y properties included
this.x=x;
this.y=y;//Both X and Y properties are REQUIRED!!!
RegisterPhysicsObject.call(this);//Register the object so that kinematic motion can be attached to that object
}
//-------------------------------Must Inherit the engine prototype func!!!--------------------
YourObj.prototype = Object.create(RegisterPhysicsObject.prototype);//Inherit the Prototype properties of Physics Engine
YourObj.prototype.constructor = YourObj;//Inherit the Prototype properties of Physics Engine
//-------------------------------Must Inherit the engine prototype func!!!--------------------
var Coin = new YourObj(50,50);//Create an Object with x and y properties
var CoinImage=new Image();//Create Image Object
CoinImage.src="http://img8.downloadapk.net/8/54/e5daa0_150.png";//Image src
var p5_collision = new p5();//using p5 collision detection
CoinImage.onload=function(){
Animation();//Start the animation function
stringB[0]='Component Name : AnyName ';
stringB[1]='Velocity : 80';
stringB[2]='Acceleration : -8';
stringB[3]='Angle : 0';
stringB[4]='Motion Expire Time : 6seconds';
//------------Parameters:--Name --Velocity--Acceleration--Angle in degree--Any Unique Name--Motion Expire Time---
Coin.SetKinematicParameters("Add",80,-8,0,6,"AnyName");
//Attach an Kinematic component to that Coin, with velocity of 80, acceleration of -8, Angle of Zero and 6 seconds expire countdown (Automatically remove this kinematic component after 6 seconds)
stringA[0]='Component Name : Bounce';
stringA[1]='Velocity : 0';
stringA[2]='Acceleration : 35';
stringA[3]='Angle : -90';
stringA[4]='Motion Expire Time : Permanent';
Coin.SetKinematicParameters("Add",0,35,-90,0,"Bounce");//Attach Bounce acting downward(-90 degree) with acceleration of 9.8,
//Note: Attaching kinematic component allows it to alter the x and y properties of the registered object, hence, it is necessary for that object to have both x and y properties before registeration of that object
}
function Bounce(){//Introduce upward motion
if (p5_collision.collideLineRect(0,400,Canvas.width, 400, Coin.x,Coin.y,CoinImage.width,CoinImage.height)/*Coin.y>300*/ && Coin.CheckIfKinematicComponentExists('Bounce'))//Add bounce effect when coin height is above 400
{
Coin.SetKinematicParameters("Change",115,-35,90,0,"Bounce");//Change the previously attached Bounce Component, by adding upward velocity, inducing bounce effect
stringA[0]='Component Name : Bounce';
stringA[1]='Velocity : 115';
stringA[2]='Acceleration : -35';
stringA[3]='Angle : 90';
stringA[4]='Motion Expire Time : Permanent';
}
}
document.onkeydown = function(e)
{
if (e.key===' ')//remove 'Bounce' component when space bar is pressed
{
console.log('Removed'); Coin.RemoveKinematicParameters("Bounce");//Remove 'Bounce' component
stringA[0]='Component Name : Bounce -REMOVED-';
stringA[1]='Velocity : 0';
stringA[2]='Acceleration : 0';
stringA[3]='Angle : 0';
stringA[4]='Motion Expire Time : 0';
}
}
</script>
<p style="color:green;font-size:15px;">To remove 'Bounce' motion, <strong>press Space Bar</strong></p>
<h3><a href="https://github.com/marcustansoon/PhysicsEngine.js-2D">Interested? Check out our Engine source code and Documentation</a></h3>
</body>
</html>