Skip to content

Commit

Permalink
Rewrote raycasting API, added Shape.prototype.raycast and removed sha…
Browse files Browse the repository at this point in the history
…pe-intersection methods to shapes. Removed World.raycastAny/All/Closest and replaced with World.raycast
  • Loading branch information
schteppe committed Jul 6, 2015
1 parent 9423e3a commit d1feff1
Show file tree
Hide file tree
Showing 21 changed files with 811 additions and 889 deletions.
48 changes: 36 additions & 12 deletions examples/canvas/raycasting.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@
var start = [0,0];
var end = [0,0];
var result = new p2.RaycastResult();
var hitPoint = p2.vec2.create();
var rayClosest = new p2.Ray({
mode: p2.Ray.CLOSEST
});
var rayAll = new p2.Ray({
mode: p2.Ray.ALL,
callback: function(result){
drawRayResult(result, rayAll);
}
});
var rayAny = new p2.Ray({
mode: p2.Ray.ANY
});
var raycastOptions = {};
init();
animate();
Expand Down Expand Up @@ -197,21 +210,23 @@
ctx.restore();
}

function drawRayResult(result){
function drawRayResult(result, ray){

result.getHitPoint(hitPoint, ray);

// Draw hit point
if(result.hasHit){
if(result.hasHit()){
ctx.beginPath();
ctx.arc(result.hitPointWorld[0],result.hitPointWorld[1],0.1,0,2*Math.PI);
ctx.arc(hitPoint[0],hitPoint[1],0.1,0,2*Math.PI);
ctx.stroke();
}

// Draw hit normal
ctx.beginPath();
ctx.moveTo(result.hitPointWorld[0], result.hitPointWorld[1]);
ctx.moveTo(hitPoint[0], hitPoint[1]);
ctx.lineTo(
result.hitPointWorld[0] + result.hitNormalWorld[0],
result.hitPointWorld[1] + result.hitNormalWorld[1]
hitPoint[0] + result.normal[0],
hitPoint[1] + result.normal[1]
);
ctx.stroke();
};
Expand All @@ -232,30 +247,39 @@
end[1] = Math.sin(world.time);

// Closest
p2.vec2.copy(rayClosest.from, start);
p2.vec2.copy(rayClosest.to, end);
rayClosest.update();
ctx.strokeStyle = 'blue';
drawRay(start, end);
result.reset();
world.raycastClosest(start, end, raycastOptions, result);
drawRayResult(result);
world.raycast(result, rayClosest);
drawRayResult(result, rayClosest);

start[1] += 0.5;
end[1] += 0.5;

// All
p2.vec2.copy(rayAll.from, start);
p2.vec2.copy(rayAll.to, end);
rayAll.update();
ctx.strokeStyle = 'green';
drawRay(start, end);
result.reset();
world.raycastAll(start, end, {}, drawRayResult);
world.raycast(result, rayAll); // drawRayResult

start[1] += 0.5;
end[1] += 0.5;

// Any
p2.vec2.copy(rayAny.from, start);
p2.vec2.copy(rayAny.to, end);
rayAny.update();
ctx.strokeStyle = 'red';
drawRay(start, end);
result.reset();
world.raycastAny(start, end, raycastOptions, result);
drawRayResult(result);
world.raycast(result, rayAny);
drawRayResult(result, rayAny);

ctx.strokeStyle = 'black';
}
Expand All @@ -273,7 +297,7 @@
drawbox();
drawPlane();
drawCircle();
// drawCapsule();
drawCapsule();
drawConvex();
drawRays();

Expand Down
76 changes: 42 additions & 34 deletions examples/canvas/rayreflect.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
var direction = [0,0];
var reflect = false;
var result = new p2.RaycastResult();
var raycastOptions = {};
var hitPoint = p2.vec2.create();
var ray = new p2.Ray({
mode: p2.Ray.CLOSEST
});

document.getElementById("reflect").addEventListener('change', function(evt){
reflect = document.getElementById("reflect").checked;
Expand Down Expand Up @@ -114,20 +117,20 @@
world.addBody(convexBody);

// Heightfield
// var data = [];
// var numDataPoints = 200;
// for(var i=0; i<numDataPoints; i++){
// data.push(0.1*Math.sin(i / numDataPoints * Math.PI * 8));
// }
// var heightfieldShape = new p2.Heightfield(data,{
// elementWidth: 5 / numDataPoints
// });
// var heightfield = new p2.Body({
// position:[2,-2],
// angle: Math.PI / 2
// });
// heightfield.addShape(heightfieldShape);
// world.addBody(heightfield);
var data = [];
var numDataPoints = 200;
for(var i=0; i<numDataPoints; i++){
data.push(0.1*Math.sin(i / numDataPoints * Math.PI * 8));
}
var heightfieldShape = new p2.Heightfield(data,{
elementWidth: 5 / numDataPoints
});
var heightfield = new p2.Body({
position:[2,-2],
angle: Math.PI / 2
});
heightfield.addShape(heightfieldShape);
world.addBody(heightfield);
}

function drawbox(){
Expand Down Expand Up @@ -269,8 +272,8 @@
ctx.beginPath();
ctx.moveTo(result.hitPointWorld[0], result.hitPointWorld[1]);
ctx.lineTo(
result.hitPointWorld[0] + result.hitNormalWorld[0],
result.hitPointWorld[1] + result.hitNormalWorld[1]
result.hitPointWorld[0] + result.normal[0],
result.hitPointWorld[1] + result.normal[1]
);
ctx.stroke();
};
Expand Down Expand Up @@ -322,44 +325,49 @@
function drawRays(){
var N = 10;
for (var i = 0; i < N; i++) {
start[0] = -3;
start[1] = 0;

ray.from[0] = -3;
ray.from[1] = 0;
var angle = .5 * Math.sin(world.time * 1 - 1)-0.005 * (i/N)*10 + 0.1;
direction[0] = Math.cos(angle);
direction[1] = Math.sin(angle);
ray.direction[0] = Math.cos(angle);
ray.direction[1] = Math.sin(angle);

ray.to[0] = ray.from[0] + ray.direction[0] * 100;
ray.to[1] = ray.from[1] + ray.direction[1] * 100;

end[0] = start[0] + direction[0] * 100;
end[1] = start[1] + direction[1] * 100;
ray.update();

// Closest
ctx.strokeStyle = 'blue';

var hits = 0;
while(world.raycastClosest(start, end, raycastOptions, result) && hits++ < 10){
drawRay(start, result.hitPointWorld);
while(world.raycast(result, ray) && hits++ < 10){
result.getHitPoint(hitPoint, ray);
drawRay(ray.from, hitPoint);
//drawRayResult(result);

// move start to the hit point
p2.vec2.copy(start, result.hitPointWorld);
p2.vec2.copy(ray.from, hitPoint);

ray.update();

result.getDirection(direction);
if(reflect){
// reflect the direction
p2.vec2.reflect(direction, direction, result.hitNormalWorld);
p2.vec2.reflect(ray.direction, ray.direction, result.normal);
} else {
refract(direction, direction, result.hitNormalWorld, airIndex, shapeIndex);
refract(ray.direction, ray.direction, result.normal, airIndex, shapeIndex);
}

// move out a bit
start[0] += direction[0] * 0.001;
start[1] += direction[1] * 0.001;
ray.from[0] += ray.direction[0] * 0.001;
ray.from[1] += ray.direction[1] * 0.001;

end[0] = start[0] + direction[0] * 100;
end[1] = start[1] + direction[1] * 100;
ray.to[0] = ray.from[0] + ray.direction[0] * 100;
ray.to[1] = ray.from[1] + ray.direction[1] * 100;

result.reset();
}
drawRay(start, end);
drawRay(ray.from, ray.to);
}

ctx.strokeStyle = 'black';
Expand Down
Loading

0 comments on commit d1feff1

Please sign in to comment.