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
-
Web hosting forum
-
Buy Domain Names
-
business loan rates
-
Facebook Developers
-
Allenbrayan
-
CatBao
-
Esquimales Sexo Y
-
DoctorStones
-
hud
-
Charles Wicked
-
casas de madera
-
BeechyBoy
-
John Lindquist
-
BeechyBoy
-
Rafael Lima
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


