require.config({
baseUrl: 'http://wellcaffeinated.net/PhysicsJS/assets/scripts/vendor/',
packages: [
{
name: 'physicsjs',
location: 'physicsjs-current',
main: 'physicsjs-full.min'
}
]
});
var colors = [
['0x268bd2', '0x0d394f']
,['0xc93b3b', '0x561414']
,['0xe25e36', '0x79231b']
,['0x6c71c4', '0x393f6a']
,['0x58c73c', '0x30641c']
,['0xcac34c', '0x736a2c']
];
function initWorld(world, Physics) {
// bounds of the window
var viewWidth = window.innerWidth
,viewHeight = window.innerHeight
,viewportBounds = Physics.aabb(0, 0, window.innerWidth-50, window.innerHeight-50)
,edgeBounce
,renderer
,styles = {
'circle': {
fillStyle: colors[0][0],
lineWidth: 1,
strokeStyle: colors[0][1],
angleIndicator: colors[0][1]
}
,'rectangle': {
fillStyle: colors[1][0],
lineWidth: 1,
strokeStyle: colors[1][1],
angleIndicator: colors[1][1]
}
}
;
// create a renderer
renderer = Physics.renderer('pixi', { el: 'viewport', styles: styles });
// add the renderer
world.add(renderer);
// render on each step
world.on('step', function () {
world.render();
});
// constrain objects to these bounds
edgeBounce = Physics.behavior('edge-collision-detection', {
aabb: viewportBounds
,restitution: 0.2
,cof: 0.8
});
// resize events
window.addEventListener('resize', function () {
// as of 0.7.0 the renderer will auto resize... so we just take the values from the renderer
viewportBounds = Physics.aabb(0, 0, renderer.width, renderer.height);
// update the boundaries
edgeBounce.setAABB(viewportBounds);
}, true);
// add behaviors to the world
world.add([
Physics.behavior('constant-acceleration')
,Physics.behavior('body-impulse-response')
,Physics.behavior('body-collision-detection')
,Physics.behavior('sweep-prune')
,edgeBounce
]);
}
function startWorld( world, Physics ){
// subscribe to ticker to advance the simulation
Physics.util.ticker.on(function( time ) {
world.step( time );
});
}
//
// Add some interaction
//
function addInteraction( world, Physics ){
// add the mouse interaction
world.add(Physics.behavior('interactive', { el: world.renderer().container }));
// add some fun extra interaction
var attractor = Physics.behavior('attractor', {
order: 0,
strength: 0.002
});
world.on({
'interact:poke': function( pos ){
world.wakeUpAll();
attractor.position( pos );
world.add( attractor );
}
,'interact:move': function( pos ){
attractor.position( pos );
}
,'interact:release': function(){
world.wakeUpAll();
world.remove( attractor );
}
});
}
// helper function (bind "this" to Physics)
function makeBody( obj ){
return this.body( obj.name, obj );
}
//
// Add bodies to the world
//
function addBodies( world, Physics ){
var v = Physics.geometry.regularPolygonVertices;
var bodies = [
{ name: 'circle', x: 100, y: 100, vx: 0.1, radius: 60,mass:10 }
];
var wallbody =[{
name: 'rectangle', x: (innerWidth / 2) - 60,fixed:true, y: innerHeight - 30, vx: 0, width: 60, height: 300, mass: 1000, restitution: 0.0, cof: 1000
}];//want this rectangle to be fixed like a wall
world.add(bodies.map(makeBody.bind(Physics)));
world.add(wallbody.map(makeBody.bind(Physics)));
}
//
// Load the libraries with requirejs and create the simulation
//
require([
'physicsjs',
'pixi'
], function( Physics, PIXI ){
window.PIXI = PIXI;
var worldConfig = {
// timestep
timestep: 6,
// maximum number of iterations per step
maxIPF: 4,
// default integrator
integrator: 'verlet',
// is sleeping disabled?
sleepDisabled: false,
// speed at which bodies wake up
sleepSpeedLimit: 0.1,
// variance in position below which bodies fall asleep
sleepVarianceLimit: 2,
// time (ms) before sleepy bodies fall asleep
sleepTimeLimit: 500
};
Physics( worldConfig, [
initWorld,
addInteraction,
addBodies,
startWorld
]);
});
我试图制作一个乒乓球类型的游戏,其中我想让身体中的矩形固定在该位置充当墙壁,但是当某些物体撞击它时,由于物理原因会反弹,所以有人可以帮我关闭该对象(矩形(物理,使其保持在该点
创建
主体时,必须调整要"static"
的 treatment
属性。否则,您可以设置myBody.treatment = "static"
.
看:http://wellcaffeinated.net/PhysicsJS/docs/#Physics-body