Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 0.7.2 #328

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6,114 changes: 2,942 additions & 3,172 deletions build/p2.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions build/p2.min.js

Large diffs are not rendered by default.

180 changes: 145 additions & 35 deletions build/p2.renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16134,6 +16134,7 @@ function Renderer(scenes, options){

this.stateChangeEvent = { type : "stateChange", state:null };

this.mousePosition = p2.vec2.create();

// Default collision masks for new shapes
this.newShapeCollisionMask = 1;
Expand Down Expand Up @@ -16593,6 +16594,11 @@ Renderer.prototype.startRenderingLoop = function(){
demo.resetCallTime = false;
}
lastCallTime = now;

// Cap if we have a really large deltatime.
// The requestAnimationFrame deltatime is usually below 0.0333s (30Hz) and on desktops it should be below 0.0166s.
timeSinceLastCall = Math.min(timeSinceLastCall, 0.5);

demo.world.step(demo.timeStep, timeSinceLastCall, demo.settings.maxSubSteps);
}
demo.render();
Expand Down Expand Up @@ -16646,7 +16652,8 @@ Renderer.prototype.handleMouseDown = function(physicsPosition){
this.world.addBody(this.nullBody);
this.mouseConstraint = new p2.RevoluteConstraint(this.nullBody, b, {
localPivotA: physicsPosition,
localPivotB: localPoint
localPivotB: localPoint,
maxForce: 1000 * b.mass
});
this.world.addConstraint(this.mouseConstraint);
} else {
Expand Down Expand Up @@ -16686,6 +16693,8 @@ Renderer.prototype.handleMouseDown = function(physicsPosition){
* Should be called by subclasses whenever there's a mousedown event
*/
Renderer.prototype.handleMouseMove = function(physicsPosition){
p2.vec2.copy(this.mousePosition, physicsPosition);

var sampling = 0.4;
switch(this.state){
case Renderer.DEFAULT:
Expand All @@ -16699,7 +16708,7 @@ Renderer.prototype.handleMouseMove = function(physicsPosition){

case Renderer.DRAWINGPOLYGON:
// drawing a polygon - add new point
var sqdist = p2.vec2.dist(physicsPosition,this.drawPoints[this.drawPoints.length-1]);
var sqdist = p2.vec2.distance(physicsPosition,this.drawPoints[this.drawPoints.length-1]);
if(sqdist > sampling*sampling){
var copy = [0,0];
p2.vec2.copy(copy,physicsPosition);
Expand Down Expand Up @@ -16753,7 +16762,7 @@ Renderer.prototype.handleMouseUp = function(physicsPosition){
// Create polygon
b = new p2.Body({ mass : 1 });
if(b.fromPolygon(this.drawPoints,{
removeCollinearPoints : 0.01,
removeCollinearPoints: 0.1
})){
this.world.addBody(b);
}
Expand All @@ -16765,7 +16774,7 @@ Renderer.prototype.handleMouseUp = function(physicsPosition){
case Renderer.DRAWINGCIRCLE:
// End this drawing state
this.setState(Renderer.DRAWCIRCLE);
var R = p2.vec2.dist(this.drawCircleCenter,this.drawCirclePoint);
var R = p2.vec2.distance(this.drawCircleCenter,this.drawCirclePoint);
if(R > 0){
// Create circle
b = new p2.Body({ mass : 1, position : this.drawCircleCenter });
Expand Down Expand Up @@ -16994,6 +17003,8 @@ function WebGLRenderer(scenes, options){
this.springSprites = [];
this.debugPolygons = false;

this.islandColors = {}; // id -> int

Renderer.call(this,scenes,options);

for(var key in settings){
Expand Down Expand Up @@ -17023,7 +17034,7 @@ function WebGLRenderer(scenes, options){
var g = that.drawShapeGraphics;
g.clear();
var center = that.drawCircleCenter;
var R = p2.vec2.dist(center, that.drawCirclePoint);
var R = p2.vec2.distance(center, that.drawCirclePoint);
that.drawCircle(g,center[0], center[1], 0, R,false, that.lineWidth);
});

Expand Down Expand Up @@ -17092,6 +17103,10 @@ WebGLRenderer.prototype.init = function(){
this.aabbGraphics = new PIXI.Graphics();
stage.addChild(this.aabbGraphics);

// Graphics object for pick
this.pickGraphics = new PIXI.Graphics();
stage.addChild(this.pickGraphics);

stage.scale.x = 200; // Flip Y direction.
stage.scale.y = -200;

Expand Down Expand Up @@ -17125,7 +17140,7 @@ WebGLRenderer.prototype.init = function(){
p2.vec2.set(stagePos, pos.x, pos.y);
that.stagePositionToPhysics(physicsPosB, stagePos);

initPinchLength = p2.vec2.dist(physicsPosA, physicsPosB);
initPinchLength = p2.vec2.distance(physicsPosA, physicsPosB);

var initScaleX = stage.scale.x;
var initScaleY = stage.scale.y;
Expand Down Expand Up @@ -17172,7 +17187,7 @@ WebGLRenderer.prototype.init = function(){
p2.vec2.set(stagePos, pos.x, pos.y);
that.stagePositionToPhysics(physicsPosB, stagePos);

var pinchLength = p2.vec2.dist(physicsPosA, physicsPosB);
var pinchLength = p2.vec2.distance(physicsPosA, physicsPosB);

// Get center
p2.vec2.add(physicsPosA, physicsPosA, physicsPosB);
Expand Down Expand Up @@ -17414,40 +17429,84 @@ WebGLRenderer.prototype.drawCapsule = function(g, x, y, angle, len, radius, colo
color = typeof(color)==="undefined" ? 0x000000 : color;
g.lineStyle(lineWidth, color, 1);

var vec2 = p2.vec2;

// Draw circles at ends
var c = Math.cos(angle);
var s = Math.sin(angle);
var hl = len / 2;
g.beginFill(fillColor, isSleeping ? this.sleepOpacity : 1.0);
g.drawCircle(-len/2*c + x, -len/2*s + y, radius);
g.drawCircle( len/2*c + x, len/2*s + y, radius);
var localPos = vec2.fromValues(x, y);
var p0 = vec2.fromValues(-hl, 0);
var p1 = vec2.fromValues(hl, 0);
vec2.rotate(p0, p0, angle);
vec2.rotate(p1, p1, angle);
vec2.add(p0, p0, localPos);
vec2.add(p1, p1, localPos);
g.drawCircle(p0[0], p0[1], radius);
g.drawCircle(p1[0], p1[1], radius);
g.endFill();

// Draw rectangle
var pp2 = vec2.create();
var p3 = vec2.create();
vec2.set(p0, -hl, radius);
vec2.set(p1, hl, radius);
vec2.set(pp2, hl, -radius);
vec2.set(p3, -hl, -radius);

vec2.rotate(p0, p0, angle);
vec2.rotate(p1, p1, angle);
vec2.rotate(pp2, pp2, angle);
vec2.rotate(p3, p3, angle);

vec2.add(p0, p0, localPos);
vec2.add(p1, p1, localPos);
vec2.add(pp2, pp2, localPos);
vec2.add(p3, p3, localPos);

g.lineStyle(lineWidth, color, 0);
g.beginFill(fillColor, isSleeping ? this.sleepOpacity : 1.0);
g.moveTo(-len/2*c + radius*s + x, -len/2*s + radius*c + y);
g.lineTo( len/2*c + radius*s + x, len/2*s + radius*c + y);
g.lineTo( len/2*c - radius*s + x, len/2*s - radius*c + y);
g.lineTo(-len/2*c - radius*s + x, -len/2*s - radius*c + y);
g.moveTo(p0[0], p0[1]);
g.lineTo(p1[0], p1[1]);
g.lineTo(pp2[0], pp2[1]);
g.lineTo(p3[0], p3[1]);
// g.lineTo( hl*c - radius*s + x, hl*s - radius*c + y);
// g.lineTo(-hl*c - radius*s + x, -hl*s - radius*c + y);
g.endFill();

// Draw lines in between
g.lineStyle(lineWidth, color, 1);
g.moveTo(-len/2*c + radius*s + x, -len/2*s + radius*c + y);
g.lineTo( len/2*c + radius*s + x, len/2*s + radius*c + y);
g.moveTo(-len/2*c - radius*s + x, -len/2*s - radius*c + y);
g.lineTo( len/2*c - radius*s + x, len/2*s - radius*c + y);
for(var i=0; i<2; i++){
g.lineStyle(lineWidth, color, 1);
var sign = (i===0?1:-1);
vec2.set(p0, -hl, sign*radius);
vec2.set(p1, hl, sign*radius);
vec2.rotate(p0, p0, angle);
vec2.rotate(p1, p1, angle);
vec2.add(p0, p0, localPos);
vec2.add(p1, p1, localPos);
g.moveTo(p0[0], p0[1]);
g.lineTo(p1[0], p1[1]);
}

};

// Todo angle
WebGLRenderer.prototype.drawRectangle = function(g,x,y,angle,w,h,color,fillColor,lineWidth,isSleeping){
lineWidth = typeof(lineWidth)==="number" ? lineWidth : 1;
color = typeof(color)==="number" ? color : 0xffffff;
fillColor = typeof(fillColor)==="number" ? fillColor : 0xffffff;
g.lineStyle(lineWidth);
g.beginFill(fillColor, isSleeping ? this.sleepOpacity : 1.0);
g.drawRect(x-w/2,y-h/2,w,h);
var path = [
[w / 2, h / 2],
[-w / 2, h / 2],
[-w / 2, -h / 2],
[w / 2, -h / 2],
];

// Rotate and add position
for (var i = 0; i < path.length; i++) {
var v = path[i];
p2.vec2.rotate(v, v, angle);
p2.vec2.add(v, v, [x, y]);
}

this.drawPath(g,path,color,fillColor,lineWidth,isSleeping);
};

WebGLRenderer.prototype.drawConvex = function(g,verts,triangles,color,fillColor,lineWidth,debug,offset,isSleeping){
Expand All @@ -17472,6 +17531,25 @@ WebGLRenderer.prototype.drawConvex = function(g,verts,triangles,color,fillColor,
g.lineTo(verts[0][0],verts[0][1]);
}
} else {

// triangles
// var centroid = p2.vec2.create();
// for(var i=0; i<triangles.length; i++){
// var v0 = verts[triangles[i][0]],
// v1 = verts[triangles[i][1]],
// v2 = verts[triangles[i][2]];
// g.lineStyle(lineWidth, 0x000000, 1);
// g.moveTo(v0[0],v0[1]);
// g.lineTo(v1[0],v1[1]);
// g.lineTo(v2[0],v2[1]);
// g.lineTo(v0[0],v0[1]);

// // triangle centroid
// p2.vec2.centroid(centroid,v0,v1,v2);
// g.drawCircle(centroid[0],centroid[1],lineWidth*2);
// }

// convexes
var colors = [0xff0000,0x00ff00,0x0000ff];
for(var i=0; i!==verts.length+1; i++){
var v0 = verts[i%verts.length],
Expand All @@ -17486,7 +17564,7 @@ WebGLRenderer.prototype.drawConvex = function(g,verts,triangles,color,fillColor,
g.drawCircle(x0,y0,lineWidth*2);
}

g.lineStyle(lineWidth, 0x000000, 1);
g.lineStyle(lineWidth, 0xff0000, 1);
g.drawCircle(offset[0],offset[1],lineWidth*2);
}
};
Expand Down Expand Up @@ -17562,14 +17640,15 @@ WebGLRenderer.prototype.render = function(){
this.updateSpriteTransform(this.sprites[i],this.bodies[i]);
}

// Update graphics if the body changed sleepState
// Update graphics if the body changed sleepState or island
for(var i=0; i!==this.bodies.length; i++){
var isSleeping = (this.bodies[i].sleepState===p2.Body.SLEEPING);
var sprite = this.sprites[i];
var body = this.bodies[i];
if(sprite.drawnSleeping !== isSleeping){
var isSleeping = (body.sleepState===p2.Body.SLEEPING);
var sprite = this.sprites[i];
var islandColor = this.getIslandColor(body);
if(sprite.drawnSleeping !== isSleeping || sprite.drawnColor !== islandColor){
sprite.clear();
this.drawRenderable(body, sprite, sprite.drawnColor, sprite.drawnLineColor);
this.drawRenderable(body, sprite, islandColor, sprite.drawnLineColor);
}
}

Expand Down Expand Up @@ -17666,6 +17745,24 @@ WebGLRenderer.prototype.render = function(){
this.aabbGraphics.cleared = true;
}

// Draw pick line
if(this.mouseConstraint){
var g = this.pickGraphics;
g.clear();
this.stage.removeChild(g);
this.stage.addChild(g);
g.lineStyle(this.lineWidth,0x000000,1);
var c = this.mouseConstraint;
var worldPivotB = p2.vec2.create();
c.bodyB.toWorldFrame(worldPivotB, c.pivotB);
g.moveTo(c.pivotA[0], c.pivotA[1]);
g.lineTo(worldPivotB[0], worldPivotB[1]);
g.cleared = false;
} else if(!this.pickGraphics.cleared){
this.pickGraphics.clear();
this.pickGraphics.cleared = true;
}

if(this.followBody){
app.centerCamera(this.followBody.interpolatedPosition[0], this.followBody.interpolatedPosition[1]);
}
Expand Down Expand Up @@ -17703,7 +17800,6 @@ WebGLRenderer.prototype.drawRenderable = function(obj, graphics, color, lineColo
graphics.drawnSleeping = false;
graphics.drawnColor = color;
graphics.drawnLineColor = lineColor;

if(obj instanceof p2.Body && obj.shapes.length){

var isSleeping = (obj.sleepState === p2.Body.SLEEPING);
Expand Down Expand Up @@ -17750,7 +17846,7 @@ WebGLRenderer.prototype.drawRenderable = function(obj, graphics, color, lineColo
p2.vec2.rotate(vrot, v, angle);
verts.push([(vrot[0]+offset[0]), (vrot[1]+offset[1])]);
}
this.drawConvex(graphics, verts, child.triangles, lineColor, color, lw, this.debugPolygons,[offset[0],-offset[1]], isSleeping);
this.drawConvex(graphics, verts, child.triangles, lineColor, color, lw, this.debugPolygons, offset, isSleeping);

} else if(child instanceof p2.Heightfield){
var path = [[0,-100]];
Expand All @@ -17771,26 +17867,40 @@ WebGLRenderer.prototype.drawRenderable = function(obj, graphics, color, lineColo
}
};

WebGLRenderer.prototype.getIslandColor = function(body){
var islandColors = this.islandColors;
if(body.islandId === -1){
color = 0xdddddd; // Gray for static objects
} else if(islandColors[body.islandId]){
color = islandColors[body.islandId];
} else {
color = islandColors[body.islandId] = parseInt(randomPastelHex(),16);
}
return color;
};

WebGLRenderer.prototype.addRenderable = function(obj){
var lw = this.lineWidth;

// Random color
var color = parseInt(randomPastelHex(),16),
lineColor = 0x000000;
var lineColor = 0x000000;

var zero = [0,0];

var sprite = new PIXI.Graphics();
if(obj instanceof p2.Body && obj.shapes.length){

var color = this.getIslandColor(obj);
this.drawRenderable(obj, sprite, color, lineColor);
this.sprites.push(sprite);
this.stage.addChild(sprite);

} else if(obj instanceof p2.Spring){

this.drawRenderable(obj, sprite, 0x000000, lineColor);
this.springSprites.push(sprite);
this.stage.addChild(sprite);

}
};

Expand Down
14 changes: 7 additions & 7 deletions build/p2.renderer.min.js

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions docs/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,17 @@ YUI.add("yuidoc-meta", function(Y) {
"GSSolver",
"GearConstraint",
"Heightfield",
"Island",
"IslandManager",
"IslandNode",
"Line",
"LinearSpring",
"LockConstraint",
"Material",
"NaiveBroadphase",
"Narrowphase",
"Object pooling utility.",
"OverlapKeeper",
"OverlapKeeperRecord",
"Particle",
"Plane",
"Pool",
"PrismaticConstraint",
"Ray",
"RaycastResult",
Expand All @@ -46,13 +43,15 @@ YUI.add("yuidoc-meta", function(Y) {
"Spring",
"TopDownVehicle",
"TupleDictionary",
"UnionFind",
"Utils",
"WheelConstraint",
"World",
"vec2",
null
],
"modules": [],
"allModules": []
"allModules": [],
"elements": []
} };
});
Loading