/** * Flade - Flash Dynamics Engine * Release 0.4 alpha * Wheel class * Copyright 2004, 2005 Alec Cove * * This file is part of Flade. The Flash Dynamics Engine. * * Flade is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Flade is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Flade; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Flash is a registered trademark of Macromedia */ Wheel = function (x, y, r) { this.wr = r; // wheel radius this.wp = new Particle(x, y); // wheel particle this.rp = new RimParticle(r, 2); // rim particle (radius, max torque) this.contactRadius = r; // contact radius - for checking if in bounds of surface // by default its the radius, but for smaller wheels // its a good idea to set this larger than the radius this.coeffSlip = 0.0; // 1 = totally slippery, 0 = full friction this.closestPoint = new Vector(0,0); var drawClipName = "wheeldrawclip_" + this.depth; this.dmc = _root.createEmptyMovieClip(drawClipName, this.depth); Wheel.prototype.depth++; } Wheel.prototype.depth = 6000; Wheel.prototype.verlet = function(sysObj) { this.rp.verlet(sysObj); this.wp.verlet(sysObj); } Wheel.prototype.checkCollision = function(surface, sysObj) { surface.resolveWheelCollision(this); } Wheel.prototype.paint = function() { // draw wheel circle var px = this.wp.curr.x; var py = this.wp.curr.y; var rx = this.rp.curr.x; var ry = this.rp.curr.y; this.dmc.clear(); this.dmc.lineStyle(1, 0x222288, 100); Graphics.prototype.paintCircle(this.dmc, px, py, this.wr); // draw rim cross this.dmc.lineStyle(0, 0x999999, 100); Graphics.prototype.paintLine(this.dmc, rx + px, ry + py, px, py); Graphics.prototype.paintLine(this.dmc, -rx + px, -ry + py, px, py); Graphics.prototype.paintLine(this.dmc, -ry + px, rx + py, px, py); Graphics.prototype.paintLine(this.dmc, ry + px, -rx + py, px, py); } /* * simulates torque/wheel-gound interaction * n is the surface normal */ Wheel.prototype.resolve = function(n) { var wp = this.wp; var rp = this.rp; // this is the tangent vector at the rim particle var rx = -rp.curr.y; var ry = rp.curr.x; // normalize so we can scale by the rotational speed var len = Math.sqrt(rx * rx + ry * ry); rx /= len; ry /= len; // sx,sy is the velocity of the wheel's surface relative to the wheel var sx = rx * rp.speed; var sy = ry * rp.speed; // tx,ty is the velocity of the wheel relative to the world var tx = wp.curr.x - wp.prev.x; var ty = wp.curr.y - wp.prev.y; // vx,vy is the velocity of the wheel's surface relative to the ground var vx = tx + sx; var vy = ty + sy; // dp is the the wheel's surfacevel projected onto the ground's tangent var dp = -n.y * vx + n.x * vy; // set the wheel's spinspeed to track the ground rp.prev.x = rp.curr.x - dp * rx; rp.prev.y = rp.curr.y - dp * ry; // some of the wheel's torque is removed and converted into linear displacement var w0 = 1 - this.coeffSlip; wp.curr.x += w0 * rp.speed * -n.y; wp.curr.y += w0 * rp.speed * n.x; rp.speed *= this.coeffSlip; }