Papervision3D with Box2DFlash – Part 3 Adding Mouse Interaction
Please view the previous posts on Box2DFlash.
In part 3, we’re adding the ability to drag objects around with the mouse:
private var mousePVec:b2Vec2 = new b2Vec2(); private var mouseJoint:b2MouseJoint; private var mouseXWorldPhys:Number; private var mouseYWorldPhys:Number; private var isMouseDown:Boolean = false;
private function setupMouseEvents():void { stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); } private function onMouseDown(e:MouseEvent):void{isMouseDown = true;}; private function onMouseUp(e:MouseEvent):void{isMouseDown = false;}; private function updateMouseWorld():void{ mouseXWorldPhys = mouseX/WORLD_SCALE; mouseYWorldPhys = mouseY/WORLD_SCALE; } private function mouseDrag():void{ // mouse press if (isMouseDown && !mouseJoint){ //refer to 'getBodyAtMouse()' below var body:b2Body = getBodyAtMouse(); if (body) { var md:b2MouseJointDef = new b2MouseJointDef(); md.body1 = world.m_groundBody; md.body2 = body; md.target = new b2Vec2(mouseXWorldPhys, mouseYWorldPhys); md.maxForce = 300.0 * body.m_mass; md.timeStep = timeStep; mouseJoint = world.CreateJoint(md) as b2MouseJoint; body.WakeUp(); } } // mouse release if (!isMouseDown){ if (mouseJoint){ world.DestroyJoint(mouseJoint); mouseJoint = null; } } // mouse move if (mouseJoint) { var p2:b2Vec2 = new b2Vec2(mouseXWorldPhys, mouseYWorldPhys); mouseJoint.m_target = p2; } } public function getBodyAtMouse(isStaticIncluded:Boolean=false):b2Body{ // Make a small box. mousePVec = new b2Vec2(mouseXWorldPhys, mouseYWorldPhys); var aabb:b2AABB = new b2AABB(); aabb.lowerBound = new b2Vec2(mouseXWorldPhys - 0.001, mouseYWorldPhys - 0.001); aabb.upperBound = new b2Vec2(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001); // Query the world for overlapping shapes. var maxCount:int = 10; var shapes:Array = new Array(); var count:int = world.Query(aabb, shapes, maxCount); var body:b2Body = null; for (var i:int = 0; i < count; ++i) { var shape:b2Shape = shapes[i] as b2Shape; var inside:Boolean = shape.TestPoint(shape.m_body.GetXForm(), mousePVec); if (inside) { body = shape.m_body; break; } } return body; }
override protected function onRenderTick(event:Event=null):void { updateMouseWorld(); mouseDrag(); [...]
*note – I also added a ceiling
-
CatBao
-
Esquimales Sexo Y
-
DoctorStones
-
hud
-
Charles Wicked
-
BeechyBoy
-
John Lindquist
-
BeechyBoy
-
Rafael Lima
Search
Recommended Books
Speaking at FITC Toronto
Recent Posts
- 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
- End dump
- Test if a plane is within the view of the camera (aka testing if culled)
- Materials Reference
Recent Comments
- KD on ActionScript 3 – Model View Controller (MVC)
- BAM5 on haXe Tutorial
- AlexG on Finding 2D Coordinates of a DisplayObject3D
- Josh on ActionScript 3 – Model View Controller (MVC)
- martin everett on requests
- martin everett on requests
- lillacska on Dragging Spheres
- Guy Ritchie on MXML without the Flex framework
- Pedro on ActionScript 3 – Namespaces
- daveevolve on AS3DMod Perlin Noise
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


