Bouncing soccerball Pv3D


Was surfing on the net and some flash blogs the other day and came across a pv3d competition on madvertices.com. I decided to try and participate.

To do:
Ball needs to react when paddle slides to the left and right
At first I was considering to hard code the way back, but I guess it would be more fun if the ball and the board were linked to a physics engine.
I gave the WOW engine a try, very easy to implement and I got it working but I couldn’t figure out if it was possible to link a cube (rectangle) to the physic objects.

Movement paddle via webcam using augmented reality
Ever since I’ve seen augmented reality at work I wanted to do something with the FLAR toolkit.

Source code:

package
{
import flash.display.Bitmap;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import org.papervision3d.materials.BitmapFileMaterial;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.objects.primitives.Sphere;
import PaperBase;

public class Main extends PaperBase
{

[Embed(source = '/assets/soccerbgrnd.jpg')]
private var EmbeddedImage:Class;
private var image:Bitmap;

private var colormaterial:ColorMaterial;
private var topcolormaterial:ColorMaterial;
private var bitmap:BitmapFileMaterial;

private var ball:Sphere;
private var paddle:Cube;

private var velocity:Object = {x:0, y:0};
private var position:Object = {x:0, y:0};
private var gravity:Number = 1;

private var friction:Number = 0.005;

private var rightArrow:Boolean;
private var leftArrow:Boolean;
private var paddleSpeed:Number = 25;

public function Main ()
{
image = new EmbeddedImage();
addChild(image);
stage.addEventListener(Event.ENTER_FRAME, initHandler);
}

private function initHandler(event:Event):void {
stage.removeEventListener(Event.ENTER_FRAME, initHandler);
init(stage.stageWidth, stage.stageHeight);

//set start position ball
position.x = stage.stageWidth / 2;
position.y = stage.stageHeight;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
}

//3d rendering
override protected function init3d():void {
//textures
bitmap = new BitmapFileMaterial("assets/texture01.jpg");
colormaterial = new ColorMaterial(0xffffff);
topcolormaterial = new ColorMaterial(0xefefef);

var materialsList:MaterialsList = new MaterialsList();
materialsList.addMaterial( colormaterial, "front" );
materialsList.addMaterial( colormaterial, "back" );
materialsList.addMaterial( colormaterial, "left" );
materialsList.addMaterial( colormaterial, "right" );
materialsList.addMaterial( topcolormaterial, "top" );
materialsList.addMaterial( colormaterial, "bottom" );

//create paddle
paddle = new Cube(materialsList, 500, 100, 50, 1, 1, 1); //material, width, depth, height, segs
paddle.y = -stage.stageHeight;
default_scene.addChild(paddle);

//create ball
ball = new Sphere(bitmap,150,20,20);
default_scene.addChild(ball);
}

//motion
override protected function processFrame():void {

//hittest
if (paddle.hitTestObject(ball)) {

//bounce the ball
velocity.y = -35;

}

//update location sphere
velocity.y += gravity; //calculate velocity
position.y -= velocity.y; //calculate new position

//update sphere position
ball.y = position.y;

//update rotation
ball.rotationX += position.y * friction;

//update location paddle
if(leftArrow){
paddle.x = Math.max(-(stage.stageWidth), paddle.x -= paddleSpeed);
}

if(rightArrow){
paddle.x = Math.min(stage.stageWidth, paddle.x += paddleSpeed);
}
}

//controls
private function keyPressed(event:KeyboardEvent):void {
trace(paddle.x)
if (event.keyCode == Keyboard.LEFT){
leftArrow = true;
}
if (event.keyCode == Keyboard.RIGHT){
rightArrow = true;
}
}

private function keyReleased(event:KeyboardEvent):void{
if (event.keyCode == Keyboard.LEFT){
leftArrow = false;
}
if (event.keyCode == Keyboard.RIGHT){
rightArrow = false;
}
}
}
}
Please Share:
  • Digg
  • del.icio.us
  • Google Bookmarks
  • email
  • LinkedIn
  • Live
  • NuJIJ
  • Reddit
  • TwitThis
  • YahooMyWeb

If you enjoyed this post, make sure you subscribe to my RSS feed!


TAGS: