Hey guys I'm having some trouble with smooth character movement and object movement on my AS3 coded box2d flash game. I am actually utilizing the
quickbox2d engine as it saves me alot of time coding-wise. Anyway, I have tried without much success to apply character movement through forces. I have much more success in implementing velocities to objects which need to move. My main problem is not really in my character movement, rather it is the effects of the moving level on the box2d objects which are created during runtime. Linked below is my game engine: create box2d circle objects by clicking and holding the mouse, then try to move around and jump with the arrow keys (the circle objects seem to bounce when they are not supposed to, gravity does not apply to them when certain keys are pressed, and since the main character is stationary it seems to cause unrealistic collisions with the character and the circle objects). Do I need to make an array that holds boolean values telling me which circles are touching the ground and which aren't; then apply gravity to those which are not on the ground and no gravity to those which are? Please help me as I have been working on this forever, I will appreciate any and all feedback.
PRESS PLAY FULLSCREEN:
My Game EngineRelevant Code & Explanations:
//Everytime I do the for() loops I am going through all box2d circle objects.
//CurrentLevel is the whole level made of a group of box2d box objects.
//gravity = 9.8
//The onStep Function is Triggered Every Box2d Frame (and in extension so is the keyControls function).
Code:
private function onStep(e:Event):void
{
//APPLY FRICTION TO X-VELOCITY
currentLevel.body.SetLinearVelocity(new b2Vec2(currentLevel.body.GetLinearVelocity().x * 0.9, currentLevel.body.GetLinearVelocity().y) );
for (var i:int = 0; i < circleArray.length; i++) {
circleArray[i].body.SetLinearVelocity(new b2Vec2(circleArray[i].body.GetLinearVelocity().x * 0.9, circleArray[i].body.GetLinearVelocity().y) );
}
//SET X-VELOCITY TO ZERO IF TOO LOW
if (Math.abs( currentLevel.body.GetLinearVelocity().x ) < 0.05) {
currentLevel.body.SetLinearVelocity(new b2Vec2(0, currentLevel.body.GetLinearVelocity().y));
for (var i:int = 0; i < circleArray.length; i++) {
circleArray[i].body.SetLinearVelocity(new b2Vec2(0, circleArray[i].body.GetLinearVelocity().y));
}
}
//LIMIT LEVEL Y-VELOCITY
if (levelVelocity.y > 6.6) {
levelVelocity.y = 6.6;
}
//JUMPING = TRUE IF FEET ARE TOUCHING THE LEVEL
if(jumping) {
levelVelocity.y -= 0.4;
//APPLY GRAVITY THROUGH DECREASING VELOCITY
currentLevel.body.SetLinearVelocity(new b2Vec2(0,levelVelocity.y*1) );
for (var i:int = 0; i < circleArray.length; i++) {
circleArray[i].body.SetLinearVelocity(new b2Vec2(circleArray[i].body.GetLinearVelocity().x, currentLevel.body.GetLinearVelocity().y) );
}
} else {
levelVelocity.y = 0;
//APPLY GRAVITY TO CIRCLES AS A FORCE IF FEET ARE TOUCHING THE GROUND
for (var i:int = 0; i < circleArray.length; i++) {
circleArray[i].body.ApplyForce(new b2Vec2(0, circleArray[i].body.GetMass() * gravity * 1), circleArray[i].body.GetWorldCenter() );
}
}
//More Keyboard and Movement Logic
keyControls();
}
//canGoRight = true if the character's rightside is not touching the level, same for canGoLeft.
private function keyControls():void
{
if (Input.kd("A", "LEFT")) {
this.scaleX = -1;
levelVelocity.x = levelVelocity.x > _max - 0.5 ? _max : levelVelocity.x + 0.5;
if (LevelLoader.canGoLeft) {
currentLevel.body.SetLinearVelocity(new b2Vec2(levelVelocity.x, currentLevel.body.GetLinearVelocity().y));
for (var i:int = 0; i < circleArray.length; i++) {
circleArray[i].body.SetLinearVelocity(new b2Vec2(levelVelocity.x, circleArray[i].body.GetLinearVelocity().y));
}
}
}
if (Input.kd("D", "RIGHT")) {
this.scaleX = 1;
levelVelocity.x = levelVelocity.x < 0.5 - _max ? _max * -1 : levelVelocity.x - 0.5;
if(LevelLoader.canGoRight) {
currentLevel.body.SetLinearVelocity(new b2Vec2(levelVelocity.x, currentLevel.body.GetLinearVelocity().y));
for (var i:int = 0; i < circleArray.length; i++) {
circleArray[i].body.SetLinearVelocity(new b2Vec2(levelVelocity.x, circleArray[i].body.GetLinearVelocity().y));
}
}
}
if (!Input.kd("A", "LEFT", "D", "RIGHT")) {
if (levelVelocity.x > 0.5) {
levelVelocity.x = levelVelocity.x < 0.5 ? 0 : levelVelocity.x - 0.5;
} else {
levelVelocity.x = levelVelocity.x > -0.5 ? 0 : levelVelocity.x + 0.5;
}
}
//IF UP KEY IS PRESSED APPLY EQUAL UPWARD VELOCITY TO BOTH THE LEVEL AND THE CIRCLES
if (Input.kd("W", "UP")) {
if (!jumping) {
levelVelocity.y = 50;
currentLevel.body.SetLinearVelocity(new b2Vec2(0, levelVelocity.y) );
for (var i:int = 0; i < circleArray.length; i++) {
circleArray[i].body.SetLinearVelocity(new b2Vec2(circleArray[i].body.GetLinearVelocity().x, currentLevel.body.GetLinearVelocity().y) );
}
levelVelocity.y -= 0.4;
jumping = true;
}
}
if (Input.kd("S", "DOWN")) {
//nothing here yet
}
}
-Thank You