Archive for January 23rd, 2009
SpringCamera3D and Driving a Car
*update* – I updated the source download with Tim’s fix for FrustumClipping and BitmapMaterials. This should eliminate the memory leak pointed out in the comments. The fix is committed in google code if you use svn. I’ll create a .swc and .zip again sometime later this week (right now you can use the .swc included in the source download). *end update*
Well, looks like the forum launch went smoothly and now hopefully everyone knows about the upcoming training in Chicago, so it’s time for me to get back to making examples
Benoit Beauséjour created a nifty little new camera (which I just added to the trunk) that’s really useful for following things around. I decided to resurrect Carlos‘ old car demo and combine it with the new camera. All the credit goes to Carlos for the car stuff and to Benoit for the camera work. I just smashed everything together.
With these sorts of projects, you’ll spend a ton of time tweaking a the values to get everything to move “just right”. So when you start playing around with the SpringCamera3D, you’ll have to keep playing with all the settings until you get something that’s right for you.
Oh, and lastly, I updated the .swc and .zip on google code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | package { import flash.display.Bitmap; import flash.display.StageQuality; import flash.events.Event; import flash.events.KeyboardEvent; import flash.ui.Keyboard; import flash.utils.ByteArray; import org.papervision3d.cameras.SpringCamera3D; import org.papervision3d.core.clipping.FrustumClipping; import org.papervision3d.core.math.Number3D; import org.papervision3d.materials.BitmapMaterial; import org.papervision3d.materials.utils.MaterialsList; import org.papervision3d.objects.DisplayObject3D; import org.papervision3d.objects.parsers.DAE; import org.papervision3d.objects.primitives.Plane; import org.papervision3d.view.BasicView; import org.papervision3d.view.layer.ViewportLayer; import org.papervision3d.view.layer.util.ViewportLayerSortMode; [SWF(width="640", height="480", backgroundColor="#ffffff", frameRate="60")] public class DrivingACar extends BasicView { [Embed(source="assets/Focus.dae", mimeType = "application/octet-stream")] private var carAsset:Class; [Embed(source="/assets/wheel.jpg")] private var wheelBitmapAsset:Class; [Embed(source="/assets/body.jpg")] private var bodyBitmapAsset:Class; [Embed(source="/assets/grassTexture.jpg")] private var grassTextureBitmapAsset:Class; private var car:DAE; private var springCamera:SpringCamera3D; private var focusMaterials:MaterialsList; private var topSpeed:Number = 0; private var topSteer:Number = 0; private var speed:Number = 0; private var steer:Number = 0; private var keyRight:Boolean = false; private var keyLeft:Boolean = false; private var keyForward:Boolean = false; private var keyReverse:Boolean = false; public function DrivingACar() { super(); addEventListener(Event.ENTER_FRAME, enterFrameHandler); stage.addEventListener( KeyboardEvent.KEY_DOWN, stage_keyDownHandler ); stage.addEventListener( KeyboardEvent.KEY_UP, stage_keyUpHandler ); setupScene(); stage.quality = StageQuality.MEDIUM; } private function setupScene():void { renderer.clipping = new FrustumClipping(FrustumClipping.BOTTOM | FrustumClipping.NEAR); springCamera = new SpringCamera3D(); springCamera.mass = 10; springCamera.damping = 10; springCamera.stiffness = 1; springCamera.lookOffset = new Number3D(0, 0, 10); springCamera.positionOffset = new Number3D(0, 5, -15); //Setup materials for the object var wheelBitmap:Bitmap = new wheelBitmapAsset() as Bitmap; var bodyBitmap:Bitmap = new bodyBitmapAsset() as Bitmap; var grassBitmap:Bitmap = new grassTextureBitmapAsset() as Bitmap; var bodyMaterial:BitmapMaterial = new BitmapMaterial(bodyBitmap.bitmapData); bodyMaterial.smooth = true; var wheelMaterial:BitmapMaterial = new BitmapMaterial(wheelBitmap.bitmapData); wheelMaterial.smooth = true; var grassMaterial:BitmapMaterial = new BitmapMaterial(grassBitmap.bitmapData); grassMaterial.precise = true; grassMaterial.tiled = true; grassMaterial.maxU = 2; grassMaterial.maxV = 2; grassMaterial.smooth = true; focusMaterials = new MaterialsList(); focusMaterials.addMaterial(bodyMaterial, "materialBody"); focusMaterials.addMaterial(wheelMaterial, "materialWheel"); // Load Collada var byteArray:ByteArray = new carAsset() as ByteArray; car = new DAE(true); car.load(byteArray, focusMaterials); car.scale = 10; springCamera.target = car; scene.addChild(car); var ground:Plane = new Plane( grassMaterial, 1000, 1000, 2, 2); ground.y = -20; scene.addChild( ground ); // Position the ground ground.rotationX = 90; viewport.containerSprite.sortMode = ViewportLayerSortMode.INDEX_SORT; var carViewportLayer:ViewportLayer = new ViewportLayer(viewport, car); carViewportLayer.addDisplayObject3D(car, true); carViewportLayer.layerIndex = 1; viewport.containerSprite.addLayer(carViewportLayer); } private function enterFrameHandler( event :Event ):void { // Check if car has been loaded if( car ) { // Calculate current steer and speed driveCar(); // Update car model updateCar(); } // Render the scene singleRender(); } private function updateCar():void { // Steer front wheels var steerFR :DisplayObject3D = car.getChildByName( "Steer_FR", true ); var steerFL :DisplayObject3D = car.getChildByName( "Steer_FL", true ); steerFR.rotationY = -steer; steerFL.rotationY = -steer; // Rotate wheels var wheelFR :DisplayObject3D = steerFR.getChildByName( "Wheel_FR", true ); var wheelFL :DisplayObject3D = steerFL.getChildByName( "Wheel_FL", true ); var wheelRR :DisplayObject3D = car.getChildByName( "Wheel_RR", true ); var wheelRL :DisplayObject3D = car.getChildByName( "Wheel_RL", true ); var roll :Number = speed * 2; wheelFR.roll( -roll ); wheelRR.roll( -roll ); wheelFL.roll( roll ); wheelRL.roll( roll ); // Steer car car.yaw( speed * steer / 500 ); // Move car car.moveForward( speed ); } private function driveCar():void { // Speed if( keyForward ) { topSpeed = 7; } else if( keyReverse ) { topSpeed = -2; } else { topSpeed = 0; } speed -= ( speed - topSpeed ) / 10; // Steer if( keyRight ) { if( topSteer < 45 ) { topSteer += 1.5; } } else if( keyLeft ) { if( topSteer > -45 ) { topSteer -= 1.5; } } else { topSteer -= topSteer / 24; } steer -= ( steer - topSteer ) / 2; } private function stage_keyDownHandler( event :KeyboardEvent ):void { switch( event.keyCode ) { case "W".charCodeAt(): case Keyboard.UP: keyForward = true; keyReverse = false; break; case "S".charCodeAt(): case Keyboard.DOWN: keyReverse = true; keyForward = false; break; case "A".charCodeAt(): case Keyboard.LEFT: keyLeft = true; keyRight = false; break; case "D".charCodeAt(): case Keyboard.RIGHT: keyRight = true; keyLeft = false; break; } } private function stage_keyUpHandler( event :KeyboardEvent ):void { switch( event.keyCode ) { case "W".charCodeAt(): case Keyboard.UP: keyForward = false; break; case "S".charCodeAt(): case Keyboard.DOWN: keyReverse = false; break; case "A".charCodeAt(): case Keyboard.LEFT: keyLeft = false; break; case "D".charCodeAt(): case Keyboard.RIGHT: keyRight = false; break; } } override protected function onRenderTick(event:Event=null):void { renderer.renderScene(scene, springCamera, viewport); } } } |
Search
Recommended Books
Speaking at FITC Toronto
Recent Posts
- Moving to johnlindquist.com
- AsyncCommand with Robotlegs, Signals, Flight, MinimalComps
- Search Widget – Robotlegs, Signals, Flight, Minimal Comps, Yahoo Astra
- FDT Super Awesome March Deal
- haXe Tutorial
- AS3 Signals Tutorial
- Preferred Video Tutorial Resolution?
- TweenMax – Tweening a timeline (Advanced Tweening)
- Robotlegs + Flight + Union Platform
- Back in the saddle
- Eclipse Theme Designer Preview
- RobotLegs Hello World Video Tutorial
- 10 Things Every Senior Flash Developer Should Know
- Efflex – 3D Effects for Flex
- MorphController – Mighty Morphing Papervision3D
Recent Comments
- list of lpn courses on SpringCamera3D and Driving a Car
- rn to bsn in montgomery al on archive
- PowerPoint Recovery on Eclipse Theme Designer Preview
- cheat mw3 on Test if a plane is within the view of the camera (aka testing if culled)
- Goa Hotels on Looking around the inside of a Sphere
- cheat gratuit on Looking around the inside of a Sphere
- Application Development on Robotlegs + Flight + Union Platform
- nexium on Moving Faces
- buy nexium on Holy Sphere
- buy aldara online on Tweening a “moveForward()” behavior
Categories
Archives
Preferred Video Tutorial Resolution
- 1024x768 (53%, 85 Votes)
- 1280x1024 (15%, 24 Votes)
- 1920x1080 (15%, 24 Votes)
- 800x600 (13%, 20 Votes)
- 480x320 (4%, 6 Votes)
- 640x480 (0%, 2 Votes)
Total Voters: 160


