Papervision3D with Box2DFlash – Part 3 Adding Mouse Interaction

Saturday, December 20th, 2008 | examples

Please view the previous posts on Box2DFlash.

In part 3, we’re adding the ability to drag objects around with the mouse:


source

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

Tags: ,

  • This is the right way,thanks for presenting this informative blog.

  • I feel glad to be a  part of this wonderful discussion..

  • ohh very nice discution !! 

  • Damn this looks so good. Thanks a million.

  • Great, thanks alot. I was looking for this all over the place :)

  • CatBao

    Thanks John!

  • any updates coming ?

  • DoctorStones

    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?

  • hud

    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. :(

  • Charles Wicked

    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

  • ave you seen that if camera or plane rotates, the mouseX and mouseY values (taken from the stage) don't collide with the viewport coordinates

  • BeechyBoy

    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.

  • John Lindquist

    @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 :)

  • BeechyBoy

    Can Collada/DAE objects be added to a scene like this?

  • Rafael Lima

    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!

blog comments powered by Disqus

Search

Recommended Books

Speaking at FITC Toronto

 

December 2008
M T W T F S S
« Nov   Jan »
1234567
891011121314
15161718192021
22232425262728
293031  

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

Loading ... Loading ...