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
7 Comments to Papervision3D with Box2DFlash - Part 3 Adding Mouse Interaction
No comments to example. *__*
This blog is practically a course of pvd3d free. xD
I am learning to each blog post.
Making all examples.
Congratulations John! Thank you very much!
December 20, 2008
Can Collada/DAE objects be added to a scene like this?
December 20, 2008
@BeechyBoy - kind of…
Since this is a 2d physics engine, you would need to create a 2d physics shape to represent the edges of your 3d shape. This tool could help: http://www.sideroller.com/wck/ . Just be warned, it would be a pain to test and get all the sizes exactly right
December 20, 2008
Thanks John,
I’m excited about the possibilities this kind of thing has, but as you say it ‘could be a pain to test’ - and I’m dealing with Collada’s and 3ds models that are dynamically loaded into a scene (meaning I won’t know what they are before hand). Great up the UH-mazing tutorials.
December 22, 2008
Thanks John!
Can you tell how to show the handmouse cursor? i tried setting a listener and using the useHandCursor=true but it is not working
April 6, 2009
hi john,
i’ve been messing around with this example.i want to know a good way how to implement Event.RESIZE to it? i have tried it but it seems not very successful.
May 7, 2009
Hi John,
and thanks for all
have you seen that if camera or plane rotates, the mouseX and mouseY values (taken from the stage) don’t collide with the viewport coordinates?
This causes a misfunction in the execution of dragging. How can we repair this problem?
Leave a comment
Search
Recent Posts
- MorphController - Mighty Morphing Papervision3D
- End dump
- Test if a plane is within the view of the camera (aka testing if culled)
- Materials Reference
- Perlin Blob
- Dynamic Text on a Plane
- Maya Texture Baking
- Creating a Custom VectorVision Font
- Workshop video and example dump
- 3D Math Book Recommendation
- Heading to New York. brb ;)
- Launching Flex4.org
- Flex 4 Layouts and Groups
- Flex 4 States
- How to click on stuff in Papervision3D - Viewport, ViewportLayers, InteractiveScene3DEvent, Mouse3D, and MovieMaterial Buttons
Recent Comments
- alexxcz on MorphController - Mighty Morphing Papervision3D
- andre venancio on 3D Math Book Recommendation
- DS on Dynamic Text on a Plane
- John Lindquist on Maya Texture Baking
- Itai on Maya Texture Baking
- Javier on End dump
- samBrown on End dump
- tf on archive
- Pan on requests
- Martin Lindelöf on requests


December 20, 2008