So I updated the driving demo code so the floor object is not repeatedly being constructed. It now creates one floor object and simply changes the x-position of the floor as the vehicle moves along the x-axis. Note that when I took out the constraintCollisionCycles property, the vehicle acted in a more spring like fashion, of course with a performance hindrance when this is done.
The constraintCollisionCycles is defined as:
Determines the number of times in a single APEngine.step() cycle that the constraints and particles have their positions corrected. This can greatly increase stability and prevent breakthroughs, especially with large complex arrangements of constraints and particles. The larger this number, the more stable the simulation, at an expense of performance.
This setting differs from the constraintCycles property in that it resolves both constraints and collisions during a APEngine.step(). The default value is 1.
Here is another demo, if the wheel ever falls below the line of the grass, this is because the code only detects the x position of one of the wheels.. not both, part III will fix this issue:
Here is the document class used:
package {
import org.cove.ape.*;
import flash.display.Stage;
import flash.display.Sprite;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
public class Main extends Sprite {
private var truck:Truck;
private var floor:Floor;
private var rightBuild:uint;
public function Main() {
rightBuild = 1;
stage.frameRate = 55;
addEventListener(Event.ENTER_FRAME, run);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
APEngine.init(1/4);
APEngine.container = this;
APEngine.addMasslessForce(new Vector(0,10));
truck = new Truck();
getFloor(350,690);
APEngine.addGroup(truck);
}
private function run(evt:Event):void {
APEngine.step();
APEngine.paint();
APEngine.container.x = -truck.px + 350;
if (truck.px > (700 * rightBuild))
{
floor.px = truck.px;
if (Math.floor(Math.random() * 6) == 1) floor.BuildJump(truck.px + 300);
floor.init();
rightBuild++;
}
}
private function getFloor(xfloor:Number, yfloor:Number):void
{
floor = new Floor(xfloor, yfloor);
APEngine.addGroup(floor);
truck.addCollidable(floor);
}
private function keyPressed(event:KeyboardEvent):void {
var keySpeed:Number = 0.8;
if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.RIGHT)
{
truck.speed = keySpeed;
}
if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.LEFT)
{
truck.speed = -keySpeed;
}
}
private function keyReleased(event:KeyboardEvent):void
{
truck.speed = 0;
}
}
}
The floor class:
package
{
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import org.cove.ape.*;
public class Floor extends Group
{
private var floor:RectangleParticle;
private var floorimage:Loader;
public function Floor(xFloor:Number=700, yFloor:Number=690)
{
floor = new RectangleParticle(xFloor,yFloor,1400,100,0,true, 1, .2,0);
floor.init();
floorimage = new Loader();
floorimage.load(new URLRequest("http://manewc.com/projects/flash/DriveDemo2/Grass.jpg"));
floor.setDisplay(floorimage, -350, -50);
addParticle(floor);
}
public function BuildJump(jx:Number)
{
// jump
var jumpL:RectangleParticle = new RectangleParticle(jx + 400,625,100,1,-.3,true);
addParticle(jumpL);
}
public function get px():Number
{
return floor.px;
}
public function set px(value:Number):void
{
floor.px = value;
}
public function get center():Vector
{
return floor.center;
}
}
}
The vehicle class:
package {
import org.cove.ape.*;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
public class Truck extends Group
{
private var wheelParticleA:WheelParticle;
private var wheelParticleB:WheelParticle;
private var wheelAimage:Loader;
private var wheelBimage:Loader;
public function Truck(colorOne:uint=0x333333, colorTwo:uint=0x333333) {
// Load in the image
wheelAimage = new Loader();
wheelAimage.load(new URLRequest("http://manewc.com/projects/flash/DriveDemo2/wheel.png"));
wheelParticleA = new WheelParticle(50,300,50,false,1);
/*
============================
Assigns a DisplayObject to be used when painting this particle.
Parameters
d:DisplayObject
offsetX:Number (default = 0)
offsetY:Number (default = 0)
rotation:Number (default = 0)
============================
*/
wheelParticleA.setDisplay(wheelAimage, -50, -50);
addParticle(wheelParticleA);
// Load in the image
wheelBimage = new Loader();
wheelBimage.load(new URLRequest("http://manewc.com/projects/flash/DriveDemo2/wheel.png"));
wheelParticleB = new WheelParticle(250,300,50,false,1);
/*
============================
Assigns a DisplayObject to be used when painting this particle.
Parameters
d:DisplayObject
offsetX:Number (default = 0)
offsetY:Number (default = 0)
rotation:Number (default = 0)
============================
*/
wheelParticleB.setDisplay(wheelBimage, -50, -50);
addParticle(wheelParticleB);
// connect the front and rear wheels
var axelConnector:SpringConstraint = new SpringConstraint(wheelParticleA, wheelParticleB, .7, false, 8);
axelConnector.setStyle(0, colorOne, 1, colorTwo);
addConstraint(axelConnector);
// A] add a particle directly above wheel A
var suspA:CircleParticle = new CircleParticle(50,200,7,false,10);
suspA.mass = 0.001;
suspA.setStyle(1, colorOne, 1, colorOne);
addParticle(suspA);
// connect particle [A] to wheel A
var shockAConnector:SpringConstraint = new SpringConstraint(wheelParticleA, suspA, .6, false, 8);
shockAConnector.setStyle(0, colorOne, 1, colorTwo);
addConstraint(shockAConnector);
// B] add a particle directly above wheel B
var suspB:CircleParticle = new CircleParticle(250,200,7,false,10);
suspB.mass = 0.001;
suspB.setStyle(1, colorOne, 1, colorOne);
addParticle(suspB);
// connect particle [B] to wheel B
var shockBConnector:SpringConstraint = new SpringConstraint(wheelParticleB, suspB, .6, false, 8);
shockBConnector.setStyle(0, colorOne, 1, colorTwo);
addConstraint(shockBConnector);
// connect shockAConnector to Wheel B
var wheelBConnector:SpringConstraint = new SpringConstraint(wheelParticleB, suspA, 0.5, false, 8);
wheelBConnector.setStyle(0, colorOne, 1, colorTwo);
addConstraint(wheelBConnector);
// connect shockBConnector to Wheel A
var wheelAConnector:SpringConstraint = new SpringConstraint(wheelParticleA, suspB, .7, false, 8);
wheelAConnector.setStyle(0, colorOne, 1, colorTwo);
addConstraint(wheelAConnector);
// connect the top 2 shocks and add the body of the truck
var truckConnector:SpringConstraint = new SpringConstraint(suspA, suspB, .7, false, 8);
truckConnector.setStyle(0, 0x336699, 1, 0x336699);
addConstraint(truckConnector);
}
public function set speed(s:Number):void {
wheelParticleA.angularVelocity = s;
wheelParticleB.angularVelocity = s;
}
public function get px():Number {
return wheelParticleB.px;
}
}
}
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.