Skip to content

Commit d1feff1

Browse files
committed
Rewrote raycasting API, added Shape.prototype.raycast and removed shape-intersection methods to shapes. Removed World.raycastAny/All/Closest and replaced with World.raycast
1 parent 9423e3a commit d1feff1

File tree

21 files changed

+811
-889
lines changed

21 files changed

+811
-889
lines changed

examples/canvas/raycasting.html

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@
2222
var start = [0,0];
2323
var end = [0,0];
2424
var result = new p2.RaycastResult();
25+
var hitPoint = p2.vec2.create();
26+
var rayClosest = new p2.Ray({
27+
mode: p2.Ray.CLOSEST
28+
});
29+
var rayAll = new p2.Ray({
30+
mode: p2.Ray.ALL,
31+
callback: function(result){
32+
drawRayResult(result, rayAll);
33+
}
34+
});
35+
var rayAny = new p2.Ray({
36+
mode: p2.Ray.ANY
37+
});
2538
var raycastOptions = {};
2639
init();
2740
animate();
@@ -197,21 +210,23 @@
197210
ctx.restore();
198211
}
199212

200-
function drawRayResult(result){
213+
function drawRayResult(result, ray){
214+
215+
result.getHitPoint(hitPoint, ray);
201216

202217
// Draw hit point
203-
if(result.hasHit){
218+
if(result.hasHit()){
204219
ctx.beginPath();
205-
ctx.arc(result.hitPointWorld[0],result.hitPointWorld[1],0.1,0,2*Math.PI);
220+
ctx.arc(hitPoint[0],hitPoint[1],0.1,0,2*Math.PI);
206221
ctx.stroke();
207222
}
208223

209224
// Draw hit normal
210225
ctx.beginPath();
211-
ctx.moveTo(result.hitPointWorld[0], result.hitPointWorld[1]);
226+
ctx.moveTo(hitPoint[0], hitPoint[1]);
212227
ctx.lineTo(
213-
result.hitPointWorld[0] + result.hitNormalWorld[0],
214-
result.hitPointWorld[1] + result.hitNormalWorld[1]
228+
hitPoint[0] + result.normal[0],
229+
hitPoint[1] + result.normal[1]
215230
);
216231
ctx.stroke();
217232
};
@@ -232,30 +247,39 @@
232247
end[1] = Math.sin(world.time);
233248

234249
// Closest
250+
p2.vec2.copy(rayClosest.from, start);
251+
p2.vec2.copy(rayClosest.to, end);
252+
rayClosest.update();
235253
ctx.strokeStyle = 'blue';
236254
drawRay(start, end);
237255
result.reset();
238-
world.raycastClosest(start, end, raycastOptions, result);
239-
drawRayResult(result);
256+
world.raycast(result, rayClosest);
257+
drawRayResult(result, rayClosest);
240258

241259
start[1] += 0.5;
242260
end[1] += 0.5;
243261

244262
// All
263+
p2.vec2.copy(rayAll.from, start);
264+
p2.vec2.copy(rayAll.to, end);
265+
rayAll.update();
245266
ctx.strokeStyle = 'green';
246267
drawRay(start, end);
247268
result.reset();
248-
world.raycastAll(start, end, {}, drawRayResult);
269+
world.raycast(result, rayAll); // drawRayResult
249270

250271
start[1] += 0.5;
251272
end[1] += 0.5;
252273

253274
// Any
275+
p2.vec2.copy(rayAny.from, start);
276+
p2.vec2.copy(rayAny.to, end);
277+
rayAny.update();
254278
ctx.strokeStyle = 'red';
255279
drawRay(start, end);
256280
result.reset();
257-
world.raycastAny(start, end, raycastOptions, result);
258-
drawRayResult(result);
281+
world.raycast(result, rayAny);
282+
drawRayResult(result, rayAny);
259283

260284
ctx.strokeStyle = 'black';
261285
}
@@ -273,7 +297,7 @@
273297
drawbox();
274298
drawPlane();
275299
drawCircle();
276-
// drawCapsule();
300+
drawCapsule();
277301
drawConvex();
278302
drawRays();
279303

examples/canvas/rayreflect.html

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
var direction = [0,0];
2121
var reflect = false;
2222
var result = new p2.RaycastResult();
23-
var raycastOptions = {};
23+
var hitPoint = p2.vec2.create();
24+
var ray = new p2.Ray({
25+
mode: p2.Ray.CLOSEST
26+
});
2427

2528
document.getElementById("reflect").addEventListener('change', function(evt){
2629
reflect = document.getElementById("reflect").checked;
@@ -114,20 +117,20 @@
114117
world.addBody(convexBody);
115118

116119
// Heightfield
117-
// var data = [];
118-
// var numDataPoints = 200;
119-
// for(var i=0; i<numDataPoints; i++){
120-
// data.push(0.1*Math.sin(i / numDataPoints * Math.PI * 8));
121-
// }
122-
// var heightfieldShape = new p2.Heightfield(data,{
123-
// elementWidth: 5 / numDataPoints
124-
// });
125-
// var heightfield = new p2.Body({
126-
// position:[2,-2],
127-
// angle: Math.PI / 2
128-
// });
129-
// heightfield.addShape(heightfieldShape);
130-
// world.addBody(heightfield);
120+
var data = [];
121+
var numDataPoints = 200;
122+
for(var i=0; i<numDataPoints; i++){
123+
data.push(0.1*Math.sin(i / numDataPoints * Math.PI * 8));
124+
}
125+
var heightfieldShape = new p2.Heightfield(data,{
126+
elementWidth: 5 / numDataPoints
127+
});
128+
var heightfield = new p2.Body({
129+
position:[2,-2],
130+
angle: Math.PI / 2
131+
});
132+
heightfield.addShape(heightfieldShape);
133+
world.addBody(heightfield);
131134
}
132135

133136
function drawbox(){
@@ -269,8 +272,8 @@
269272
ctx.beginPath();
270273
ctx.moveTo(result.hitPointWorld[0], result.hitPointWorld[1]);
271274
ctx.lineTo(
272-
result.hitPointWorld[0] + result.hitNormalWorld[0],
273-
result.hitPointWorld[1] + result.hitNormalWorld[1]
275+
result.hitPointWorld[0] + result.normal[0],
276+
result.hitPointWorld[1] + result.normal[1]
274277
);
275278
ctx.stroke();
276279
};
@@ -322,44 +325,49 @@
322325
function drawRays(){
323326
var N = 10;
324327
for (var i = 0; i < N; i++) {
325-
start[0] = -3;
326-
start[1] = 0;
328+
329+
ray.from[0] = -3;
330+
ray.from[1] = 0;
327331
var angle = .5 * Math.sin(world.time * 1 - 1)-0.005 * (i/N)*10 + 0.1;
328-
direction[0] = Math.cos(angle);
329-
direction[1] = Math.sin(angle);
332+
ray.direction[0] = Math.cos(angle);
333+
ray.direction[1] = Math.sin(angle);
334+
335+
ray.to[0] = ray.from[0] + ray.direction[0] * 100;
336+
ray.to[1] = ray.from[1] + ray.direction[1] * 100;
330337

331-
end[0] = start[0] + direction[0] * 100;
332-
end[1] = start[1] + direction[1] * 100;
338+
ray.update();
333339

334340
// Closest
335341
ctx.strokeStyle = 'blue';
336342

337343
var hits = 0;
338-
while(world.raycastClosest(start, end, raycastOptions, result) && hits++ < 10){
339-
drawRay(start, result.hitPointWorld);
344+
while(world.raycast(result, ray) && hits++ < 10){
345+
result.getHitPoint(hitPoint, ray);
346+
drawRay(ray.from, hitPoint);
340347
//drawRayResult(result);
341348

342349
// move start to the hit point
343-
p2.vec2.copy(start, result.hitPointWorld);
350+
p2.vec2.copy(ray.from, hitPoint);
351+
352+
ray.update();
344353

345-
result.getDirection(direction);
346354
if(reflect){
347355
// reflect the direction
348-
p2.vec2.reflect(direction, direction, result.hitNormalWorld);
356+
p2.vec2.reflect(ray.direction, ray.direction, result.normal);
349357
} else {
350-
refract(direction, direction, result.hitNormalWorld, airIndex, shapeIndex);
358+
refract(ray.direction, ray.direction, result.normal, airIndex, shapeIndex);
351359
}
352360

353361
// move out a bit
354-
start[0] += direction[0] * 0.001;
355-
start[1] += direction[1] * 0.001;
362+
ray.from[0] += ray.direction[0] * 0.001;
363+
ray.from[1] += ray.direction[1] * 0.001;
356364

357-
end[0] = start[0] + direction[0] * 100;
358-
end[1] = start[1] + direction[1] * 100;
365+
ray.to[0] = ray.from[0] + ray.direction[0] * 100;
366+
ray.to[1] = ray.from[1] + ray.direction[1] * 100;
359367

360368
result.reset();
361369
}
362-
drawRay(start, end);
370+
drawRay(ray.from, ray.to);
363371
}
364372

365373
ctx.strokeStyle = 'black';

0 commit comments

Comments
 (0)