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: ,

7 Comments to Papervision3D with Box2DFlash - Part 3 Adding Mouse Interaction

Rafael Lima
December 20, 2008

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!

BeechyBoy
December 20, 2008

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

John Lindquist
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 :)

BeechyBoy
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.

Charles Wicked
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

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

DoctorStones
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