So i had an idea for a loader, just a simple top-down view of a puck that you can hit around with your mouse.
The problem? when the objects collide there is no bounce or slide... it just kind of pushes the puck out of the way. The gravity and friction is set to 0. Can anyone see what I might be doing wrong? I played around with the variables but it always seems to give me the same result.
I've been messing around with this for a couple days now, but the effect is always the same 🙁 any help or advice would be greatly appreciated.
EDIT: Even when I bump up the gravity to 9999999999999 it does nothing... the puck body just very slowly moves down.
example:
http://www.entrance4.net/examples/preloader.swf
zip:
http://www.entrance4.net/examples/preloaderExample.zip
source:
The problem? when the objects collide there is no bounce or slide... it just kind of pushes the puck out of the way. The gravity and friction is set to 0. Can anyone see what I might be doing wrong? I played around with the variables but it always seems to give me the same result.
I've been messing around with this for a couple days now, but the effect is always the same 🙁 any help or advice would be greatly appreciated.
EDIT: Even when I bump up the gravity to 9999999999999 it does nothing... the puck body just very slowly moves down.
example:
http://www.entrance4.net/examples/preloader.swf
zip:
http://www.entrance4.net/examples/preloaderExample.zip
source:
Code:
package singulair {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.Stage;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.Dictionary;
import flash.geom.Point;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import Box2D.Dynamics.Joints.*;
import gs.TweenMax;
import gs.easing.Strong;
public class Preloader extends MovieClip {
// box2d
public var m_timestep:Number = 1.0/30.0;
public var m_iterations:Number = 10.0;
public var m_world:b2World;
public var m_dbgSprite:Sprite;
public var m_phys_scale:Number = 30.0;
public var collider:b2Body;
public var puck_body:b2Body;
public var m_mouseJoint:b2MouseJoint;
public var dimensions = new Point(950, 550);
public function Preloader():void {
puck.gotoAndPlay(2);
TweenMax.from(puck, .6, {alpha:0, blurFilter:{blurX:10, blurY:10}});
puck.addEventListener("DONE", _puckDynamics);
}
// PUCK DYNAMICS ////////////////////////////////////
private function _puckDynamics(e:Event):void {
_initBox2D();
_currentMouseX = mouseX;
_currentMouseY = mouseY;
this.addEventListener(Event.ENTER_FRAME, _loopPuck);
_puckShape();
_colliderShape();
}
private function _initBox2D():void {
var gravity:b2Vec2 = new b2Vec2(0,0);
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-1000,-1000);
worldAABB.upperBound.Set(1000,1000);
m_world = new b2World(worldAABB,gravity,true);
SetDebugDraw();
}
private function _puckShape():void {
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(puck.x,puck.y);
bodyDef.massData.mass = 0.01;
bodyDef.allowSleep = false;
bodyDef.linearDamping = 0.0;
var circleDef = new b2CircleDef();
circleDef.radius= 40;
circleDef.friction = 0.0;
circleDef.restitution = .9;
puck_body = m_world.CreateBody(bodyDef);
puck_body.CreateShape(circleDef);
}
private function _colliderShape():void {
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(400, 275);
bodyDef.massData.mass = 1.0;
bodyDef.angle = 0;
bodyDef.fixedRotation = true;
bodyDef.allowSleep = false;
var circleDef = new b2CircleDef();
circleDef.radius= 20;
circleDef.density = 10.0;
circleDef.friction = 0.0;
circleDef.restitution = 0.9;
collider = m_world.CreateBody(bodyDef);
collider.CreateShape(circleDef);
var md:b2MouseJointDef = new b2MouseJointDef();
md.body1 = m_world.GetGroundBody();
md.body2 = collider;
md.target.Set(400, 275);
md.maxForce = 100000;
md.timeStep = m_timestep;
m_mouseJoint = m_world.CreateJoint(md) as b2MouseJoint;
stage.addEventListener(MouseEvent.MOUSE_MOVE, mJoint, false, 0, true);
}
private function mJoint(e:MouseEvent):void {
var p2:b2Vec2 = new b2Vec2(mouseX, mouseY);
m_mouseJoint.SetTarget(p2);
}
public function SetDebugDraw():void {
//trace("set debug");
var m_sprite = new Sprite();
addChild(m_sprite);
var dbgDraw:b2DebugDraw = new b2DebugDraw();
var dbgSprite:Sprite = new Sprite();
m_sprite.addChild(dbgSprite);
dbgDraw.m_sprite = m_sprite;
dbgDraw.m_drawScale = 1;
dbgDraw.m_fillAlpha = 0.3;
dbgDraw.m_lineThickness = 1.0;
dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit;
m_world.SetDebugDraw(dbgDraw);
}
public function copyPhysicsToMovieclip(body:b2Body, mc:MovieClip):void {
var pos:b2Vec2 = body.GetPosition();
mc.x = pos.x;
mc.y = pos.y;
}
// ENTERFRAME ////////////////////////////////////////
private function _loopPuck(e:Event):void {
//mouseHit.x = mouseX-5;
//mouseHit.y = mouseY-5;
m_world.Step(m_timestep,m_iterations);
if(puck_body) {
copyPhysicsToMovieclip(puck_body, puck)
}
}
}
}
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.Stage;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.Dictionary;
import flash.geom.Point;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import Box2D.Dynamics.Joints.*;
import gs.TweenMax;
import gs.easing.Strong;
public class Preloader extends MovieClip {
// box2d
public var m_timestep:Number = 1.0/30.0;
public var m_iterations:Number = 10.0;
public var m_world:b2World;
public var m_dbgSprite:Sprite;
public var m_phys_scale:Number = 30.0;
public var collider:b2Body;
public var puck_body:b2Body;
public var m_mouseJoint:b2MouseJoint;
public var dimensions = new Point(950, 550);
public function Preloader():void {
puck.gotoAndPlay(2);
TweenMax.from(puck, .6, {alpha:0, blurFilter:{blurX:10, blurY:10}});
puck.addEventListener("DONE", _puckDynamics);
}
// PUCK DYNAMICS ////////////////////////////////////
private function _puckDynamics(e:Event):void {
_initBox2D();
_currentMouseX = mouseX;
_currentMouseY = mouseY;
this.addEventListener(Event.ENTER_FRAME, _loopPuck);
_puckShape();
_colliderShape();
}
private function _initBox2D():void {
var gravity:b2Vec2 = new b2Vec2(0,0);
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-1000,-1000);
worldAABB.upperBound.Set(1000,1000);
m_world = new b2World(worldAABB,gravity,true);
SetDebugDraw();
}
private function _puckShape():void {
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(puck.x,puck.y);
bodyDef.massData.mass = 0.01;
bodyDef.allowSleep = false;
bodyDef.linearDamping = 0.0;
var circleDef = new b2CircleDef();
circleDef.radius= 40;
circleDef.friction = 0.0;
circleDef.restitution = .9;
puck_body = m_world.CreateBody(bodyDef);
puck_body.CreateShape(circleDef);
}
private function _colliderShape():void {
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(400, 275);
bodyDef.massData.mass = 1.0;
bodyDef.angle = 0;
bodyDef.fixedRotation = true;
bodyDef.allowSleep = false;
var circleDef = new b2CircleDef();
circleDef.radius= 20;
circleDef.density = 10.0;
circleDef.friction = 0.0;
circleDef.restitution = 0.9;
collider = m_world.CreateBody(bodyDef);
collider.CreateShape(circleDef);
var md:b2MouseJointDef = new b2MouseJointDef();
md.body1 = m_world.GetGroundBody();
md.body2 = collider;
md.target.Set(400, 275);
md.maxForce = 100000;
md.timeStep = m_timestep;
m_mouseJoint = m_world.CreateJoint(md) as b2MouseJoint;
stage.addEventListener(MouseEvent.MOUSE_MOVE, mJoint, false, 0, true);
}
private function mJoint(e:MouseEvent):void {
var p2:b2Vec2 = new b2Vec2(mouseX, mouseY);
m_mouseJoint.SetTarget(p2);
}
public function SetDebugDraw():void {
//trace("set debug");
var m_sprite = new Sprite();
addChild(m_sprite);
var dbgDraw:b2DebugDraw = new b2DebugDraw();
var dbgSprite:Sprite = new Sprite();
m_sprite.addChild(dbgSprite);
dbgDraw.m_sprite = m_sprite;
dbgDraw.m_drawScale = 1;
dbgDraw.m_fillAlpha = 0.3;
dbgDraw.m_lineThickness = 1.0;
dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit;
m_world.SetDebugDraw(dbgDraw);
}
public function copyPhysicsToMovieclip(body:b2Body, mc:MovieClip):void {
var pos:b2Vec2 = body.GetPosition();
mc.x = pos.x;
mc.y = pos.y;
}
// ENTERFRAME ////////////////////////////////////////
private function _loopPuck(e:Event):void {
//mouseHit.x = mouseX-5;
//mouseHit.y = mouseY-5;
m_world.Step(m_timestep,m_iterations);
if(puck_body) {
copyPhysicsToMovieclip(puck_body, puck)
}
}
}
}