var psystem = new ParticleSystem(); psystem.setDamping(1.0); psystem.setGravity(0.0, 0.4); // cooefficient of restitution psystem.setKfr(0.1); // surface friction for particles psystem.setFriction(0.5); // surfaces var sA = new Surface(new Point(0, 100), new Point(1, 300)); sA.setIsOrientH(false); psystem.addSurface(sA); var s0 = new Surface(new Point(1, 300), new Point(100, 300)); psystem.addSurface(s0); var s1 = new Surface(new Point(100, 300), new Point(300, 300)); psystem.addSurface(s1); var s2 = new Surface(new Point(300, 300), new Point(600, 350)); psystem.addSurface(s2); var s3 = new Surface(new Point(600,350), new Point(799, 300)); psystem.addSurface(s3); var sB = new Surface(new Point(799, 300), new Point(799, 100)); sB.setIsOrientH(false); psystem.addSurface(sB); // circle surface var circA = new CircleSurface(300, 370, 100); psystem.addSurface(circA); var leftX = 10; var rightX = 80 var widthX = rightX - leftX; var midX = leftX + (widthX / 2); var topY = 180; // wheels var wheelA = psystem.addWheel(leftX, topY, 25); var wheelB = psystem.addWheel(rightX, topY, 25); wheelA.coeffSlip = 0.0; wheelB.coeffSlip = 0.0; // body var rectA = psystem.addRectangle(new Vector(midX, topY), widthX, 10); // wheel struts var conn1 = psystem.addConstraint(wheelA.wp, rectA.p3); conn1.setRestLength(5); var conn2 = psystem.addConstraint(wheelB.wp, rectA.p2); conn2.setRestLength(5); var conn1a = psystem.addConstraint(wheelA.wp, rectA.p0); conn1a.setRestLength(5); var conn2a = psystem.addConstraint(wheelB.wp, rectA.p1); conn2a.setRestLength(5); // triangle top of car var p1 = psystem.addParticle(midX, topY - 50); var conn3 = psystem.addConstraint(wheelA.wp, p1); var conn4 = psystem.addConstraint(wheelB.wp, p1); // angular constraint for triangle top var ang = psystem.addAngularConstraint(wheelA.wp, p1, wheelB.wp); var angDefault = ang.targetTheta; // trailing body var rectB = psystem.addRectangle(new Vector(midX, topY - 50), 10, 10); var p2 = psystem.addParticle(midX, topY); var conn3 = psystem.addConstraint(rectB.p1, p2); conn3.setRestLength(20); var conn4 = psystem.addConstraint(p1, p2); conn4.setRestLength(20); psystem.paintSurfaces(); this.onEnterFrame = function() { var keySpeed = 2.0; if(Key.isDown(Key.LEFT)) { wheelA.rp.vs = -keySpeed; wheelB.rp.vs = -keySpeed; } else if(Key.isDown(Key.RIGHT)) { wheelA.rp.vs = keySpeed; wheelB.rp.vs = keySpeed; } else { wheelA.rp.vs = 0; wheelB.rp.vs = 0; } if (Key.isDown(Key.UP)) { if (ang.targetTheta < 2.9) ang.targetTheta += .1; } else { if (ang.targetTheta > angDefault) ang.targetTheta -= .1; } psystem.timeStep(); //psystem.paintParticles(); psystem.paintWheels(); psystem.paintConstraints(); }