[EDIT: solved my own problem, so here's a new question...]
Ok, my ball is jumping to the mouse, and it falls still (using angularDamping). But I'm wondering: at this point I set the angularDamping on my ball. What I would like is that the damping is different based on the surface the ball is rolling on (for instance: on "sand" the ball should immediately come to a stop, on grass it should keep on rolling).
So I would like to dynamically change the angularDamping of my ball based on the surface it hits. Any suggestions on how I can make this work?
Here's my code so far:
Code:
package{
// imports of flash
import flash.display.*;
import flash.events.*;
//imports of Box2D
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.*;
import Box2D.Common.Math.*;
public class BasicWorld extends Sprite{
//create vars
var bodyDef:b2BodyDef;
var boxDef:b2BoxDef;
var boxDef2:b2BoxDef;
var circleDef:b2CircleDef;
var m_physScale:Number = 1;
var angularDamping:Number = 0.1;
// target followers
var targetBody:b2Body;
var mouseVector:b2Vec2;
// world variable
public var m_world:b2World;
// variables for render engine
public var m_timeStep:Number = 1/30;
public var m_iterations:int = 10;
//main constructor function
public function BasicWorld(){
//2bAABB bounding box //---------------------------------------------------------------------------
var worldAABB:b2AABB = new b2AABB();
worldAABB.minVertex.Set(0.0, 0.0);
worldAABB.maxVertex.Set(1024.0, 768.0);
// world parameters gravity&sleep
var gravity:b2Vec2 = new b2Vec2(0, 600.0)
var doSleep:Boolean = true;
m_world = new b2World(worldAABB, gravity, doSleep);
// GROUND PLANE //---------------------------------------------------------------------------------
bodyDef = new b2BodyDef();
// create the shape
boxDef = new b2BoxDef();
boxDef.extents.Set(800,10);
boxDef.friction = 0.7;
//position body
bodyDef.position.Set(300,680);
//bodyDef.rotation = 30/(180/Math.PI);
bodyDef.AddShape(boxDef);
// visualize world
bodyDef.userData = new groundImage();
bodyDef.userData.width = boxDef.extents.x * 2;
bodyDef.userData.height = boxDef.extents.y * 2;
addChild(bodyDef.userData);
// create plane
m_world.CreateBody(bodyDef);
// RANDOM BODIES //--------------------------------------------------------------------------------
for (var i:uint = 0; i < 20; i++){
// create vars for each body
var body:b2BodyDef = new b2BodyDef();
var bd:b2BoxDef = new b2BoxDef();
// random numbers for starting position & dimensions
var tempX1:Number = (Math.random() * 20 + 10);
var tempY1:Number = (Math.random() * 5 + 20);
var tempX2:Number = (Math.random() * 500 + 20);
var tempY2:Number = (Math.random() * 200 + 400);
// bodies should fall down
bd.density = 1;
// set parameters for bodies
bd.friction = Math.random() * 0.5 + 0.1;
bd.restitution = Math.random() * 0.5 + 0.1;
bd.extents.Set(tempX1,tempY1);
body.position.Set(tempX2,tempY2);
body.rotation = Math.random() * Math.PI;
body.AddShape(bd);
// visualise bodies
body.userData = new boxImage();
body.userData.width = tempX1 * 2;
body.userData.height = tempY1 * 2;
addChild(body.userData);
// create bodies
m_world.CreateBody(body);
}
// BALL //-------------------------------------------------------------------------------------------
// create vars
circleDef = new b2CircleDef();
circleDef.radius = 16;
circleDef.friction = 0.5;
circleDef.density = 1;
circleDef.restitution = 0.4;
// create body for circle
var cirkBody:b2BodyDef = new b2BodyDef();
//cirkBody.preventRotation = true;
cirkBody.position.Set(300,0);
cirkBody.AddShape(circleDef);
cirkBody.angularDamping = angularDamping;
cirkBody.userData = new circleImage();
cirkBody.userData.width = circleDef.radius * 2;
cirkBody.userData.height = circleDef.radius * 2;
addChild(cirkBody.userData);
//m_world.CreateBody(cirkBody);
targetBody = m_world.CreateBody(cirkBody);
// Circle reacts to mouse
stage.addEventListener(MouseEvent.MOUSE_DOWN, selectBall);
//cirkBody.userData.addEventListener(MouseEvent.MOUSE_DOWN, selectBall);
// Start rendering
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
// Capture mouse
stage.addEventListener(MouseEvent.MOUSE_MOVE, captureMouse, false, 0, true);
}
// CAPTUREMOUSE //-------------------------------------------------------------------------------------------
public function captureMouse(e:MouseEvent):void {
mouseVector = new b2Vec2(e.stageX - targetBody.m_position.x, e.stageY - targetBody.m_position.y);
//trace(mouseVector.x);
}
// SELECT BALL //-------------------------------------------------------------------------------------------
public function selectBall(e:Event):void{
//trace(targetBody.m_position.x+" - "+targetBody.m_position.y);
targetBody.WakeUp();
targetBody.ApplyImpulse(new b2Vec2(mouseVector.x * 1500, mouseVector.y * 1500), new b2Vec2(targetBody.m_position.x, targetBody.m_position.y) );
}
// RENDER ENGINE //---------------------------------------------------------------------------------------
public function Update(e:Event):void{
// update world
m_world.Step(m_timeStep, m_iterations);
// Update the positions
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
if (bb.m_userData is Sprite){
bb.m_userData.x = bb.m_position.x;
bb.m_userData.y = bb.m_position.y;
bb.m_userData.rotation = bb.m_rotation * (180/Math.PI);
}
}
}
}
}