-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdraw.js
142 lines (136 loc) · 3.84 KB
/
draw.js
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
function canvasdraw(ctx){
var worldBody=null;
worldBody=world.GetBodyList();
var fixtureList , shape, shapeType ;
while(worldBody.a !== 0){
{
fixtureList = worldBody.GetFixtureList();
next_fixturePointer = fixtureList.a;
while (fixtureList.a !== 0){
shape =fixtureList.GetShape();
shapeType = shape.GetType();
ctx.save();
ctx.setTransform(1,0,0,1,0,0);
ctx.translate(canvasOffset.x,canvasOffset.y);
ctx.scale(1,-1);
if(shapeType == 0){
drawCircleShape(fixtureList,ctx);
ctx.restore();
}
else if (shapeType == 1){
drawEdgeShape();
ctx.restore();
}
else if(shapeType ==2){
drawPolygonShape(fixtureList,ctx);
ctx.restore();
}
else
ctx.restore();
fixtureList=fixtureList.GetNext();
}
}
worldBody=worldBody.GetNext();
}
}
function drawChainPolygonShape(worldBody,ctx){
//console.log('entered');
var position ={x : null,
y : null};
var angle = null;
position.x = worldBody.GetPosition().get_x();
position.y = worldBody.GetPosition().get_y();
//console.log(position.x +","+position.y );
angle= worldBody.GetAngle();
ctx.save();
ctx.setTransform(1,0,0,1,0,0);
ctx.translate(canvasOffset.x,canvasOffset.y);
ctx.scale(1,-1);
ctx.translate(position.x*30,position.y*30);
ctx.rotate(angle);
ctx.beginPath();
for(var i in worldBody.userData.verticesList){
if(i == 0){
//console.log('beginning path');
ctx.moveTo(worldBody.userData.verticesList[i].x*30,worldBody.userData.verticesList[i].y*30);
}
else
ctx.lineTo(worldBody.userData.verticesList[i].x*30,worldBody.userData.verticesList[i].y*30);
}
ctx.lineWidth = 3;
ctx.strokeStyle= 'blue';
ctx.stroke();
}
function initialDraw(verticesList,ctx){
ctx.save();
ctx.setTransform(1,0,0,1,0,0);
ctx.translate(canvas.width/2,canvas.height/2);
ctx.scale(1,-1);
for(var i in verticesList){
if( i ==0){
ctx.beginPath();
ctx.moveTo(verticesList[i].x*30,verticesList[i].y*30);
}
else{
ctx.lineTo(verticesList[i].x*30,verticesList[i].y*30);
}
}
ctx.lineWidth=3.0;
ctx.strokeStyle='green';
ctx.stroke();
ctx.restore();
}
function drawCircleShape(fixture,ctx){
var pos=fixture.GetBody().GetPosition();
var x_coord = pos.get_x()*PTM;
var y_coord = pos.get_y()*PTM;
var radius =PTM*fixture.GetShape().get_m_radius();
//ctx.save();
//ctx.translate(x_coord,y_coord);
ctx.beginPath();
ctx.arc(x_coord,y_coord,radius,0,2*Math.PI,false);
ctx.strokeStyle = 'green';
ctx.fillStyle = fixture.GetBody().userData.color;
ctx.fill();
//ctx.drawImage(img,40,x_coord-radius,y_coord-radius,2*radius,2*radius);
}
function drawEdgeShape(fixture,ctx){
var pos=fixture.GetBody().GetPosition();
var angle = fixture.GetBody().GetAngle();
var x_coord = pos.get_x()*PTM;
var y_coord = pos.get_y()*PTM;
ctx.translate(x_coord,y_coord);
ctx.rotate(angle);
var shape = Box2D.castObject(fixture.GetShape(),b2EdgeShape);
}
function drawPolygonShape(fixture,ctx){
var pos = fixture.GetBody().GetPosition();
var angle = fixture.GetBody().GetAngle();
//ctx.save();
var x_coord = pos.get_x()*PTM;
var y_coord = pos.get_y()*PTM;
//console.log('x : ' +x_coord +' ,y : ' +y_coord);
ctx.translate(x_coord,y_coord);
//ctx.beginPath();
ctx.rotate(angle);
var shape = Box2D.castObject(fixture.GetShape(),b2PolygonShape);
//console.log('came here with vertex count = '+ shape.GetVertexCount());
//console.log(shape);
for(var vertexCount =0;vertexCount<shape.GetVertexCount();vertexCount++)
{
var vertex = shape.GetVertex(vertexCount);
x_coord = vertex.get_x()* PTM;
y_coord = vertex.get_y()* PTM;
if(vertexCount == 0){
ctx.beginPath();
ctx.moveTo(x_coord,y_coord);
}
else{
ctx.lineTo(x_coord,y_coord);
}
}
ctx.strokeStyle = 'darkSalmon';
ctx.fillStyle = fixture.GetBody().userData.color;
ctx.fill();
ctx.restore();
}