|
20 | 20 | var direction = [0,0];
|
21 | 21 | var reflect = false;
|
22 | 22 | 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 | + }); |
24 | 27 |
|
25 | 28 | document.getElementById("reflect").addEventListener('change', function(evt){
|
26 | 29 | reflect = document.getElementById("reflect").checked;
|
|
114 | 117 | world.addBody(convexBody);
|
115 | 118 |
|
116 | 119 | // 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); |
131 | 134 | }
|
132 | 135 |
|
133 | 136 | function drawbox(){
|
|
269 | 272 | ctx.beginPath();
|
270 | 273 | ctx.moveTo(result.hitPointWorld[0], result.hitPointWorld[1]);
|
271 | 274 | 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] |
274 | 277 | );
|
275 | 278 | ctx.stroke();
|
276 | 279 | };
|
|
322 | 325 | function drawRays(){
|
323 | 326 | var N = 10;
|
324 | 327 | 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; |
327 | 331 | 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; |
330 | 337 |
|
331 |
| - end[0] = start[0] + direction[0] * 100; |
332 |
| - end[1] = start[1] + direction[1] * 100; |
| 338 | + ray.update(); |
333 | 339 |
|
334 | 340 | // Closest
|
335 | 341 | ctx.strokeStyle = 'blue';
|
336 | 342 |
|
337 | 343 | 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); |
340 | 347 | //drawRayResult(result);
|
341 | 348 |
|
342 | 349 | // move start to the hit point
|
343 |
| - p2.vec2.copy(start, result.hitPointWorld); |
| 350 | + p2.vec2.copy(ray.from, hitPoint); |
| 351 | + |
| 352 | + ray.update(); |
344 | 353 |
|
345 |
| - result.getDirection(direction); |
346 | 354 | if(reflect){
|
347 | 355 | // reflect the direction
|
348 |
| - p2.vec2.reflect(direction, direction, result.hitNormalWorld); |
| 356 | + p2.vec2.reflect(ray.direction, ray.direction, result.normal); |
349 | 357 | } else {
|
350 |
| - refract(direction, direction, result.hitNormalWorld, airIndex, shapeIndex); |
| 358 | + refract(ray.direction, ray.direction, result.normal, airIndex, shapeIndex); |
351 | 359 | }
|
352 | 360 |
|
353 | 361 | // 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; |
356 | 364 |
|
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; |
359 | 367 |
|
360 | 368 | result.reset();
|
361 | 369 | }
|
362 |
| - drawRay(start, end); |
| 370 | + drawRay(ray.from, ray.to); |
363 | 371 | }
|
364 | 372 |
|
365 | 373 | ctx.strokeStyle = 'black';
|
|
0 commit comments