Examples

11 - Image Alpha

Here is an exemple that shows handling of attached image transparency.

Applet

This browser does not have a Java Plug-in.
Get the latest Java Plug-in here.

Code

//Import all Phys2D libraries
import pphys2d.bodies.*;
import pphys2d.joints.*;
import pphys2d.shapes.*;
import pphys2d.phys2d.raw.collide.*;
import pphys2d.phys2d.raw.strategies.*;
import pphys2d.phys2d.raw.forcesource.*;
import pphys2d.phys2d.util.*;
import pphys2d.phys2d.raw.shapes.*;
import pphys2d.*;

//Create a PPhys2D world
PPWorld world = new PPWorld();

//Create an Array to hold the balls
PPCircle[] balls = new PPCircle[8];

//Create a PPhys2D Box
PPBox myBox;

//Alpha variation
float alphaVar = -1;

void setup () {

  //Set size and framerate
  frameRate(30);
  size(500,500);

  //Set a standard world gravity
  world.setGravity(0, 200);

  //Create balls and add them to the world
  for(int i=0; i <balls.length; i++) {    
    balls[i] = new PPCircle(random(20,50));
    balls[i].setFillColor(new Color((int)random(100,140), (int)random(100,140), (int)random(100,140)));
    balls[i].setStrokeColor(new Color(190,190,190));
    balls[i].setStrokeWidth(1);   
    balls[i].setPosition(random(50,450), random(50,150));
    balls[i].setRestitution(0.5);
    world.add(balls[i]);
  }
  
  //Add a static box to the world
  myBox = new PPBox(150, 20);
  myBox.setPosition(250, 300);
  myBox.setStrokeWidth(1);
  myBox.setRotation(2*PI);
  myBox.setStaticBody(true);
  myBox.setRestitution(1);
  myBox.attachImage(loadImage("brick.png"));
  myBox.setImageAlpha(150);
  world.add(myBox);

}

void draw () {
  //Clear screen
  background(255); 
  //Draw world
  world.draw(this);  
  //Check circle position
  for(int i=0; i <balls.length; i++) {    
    if(balls[i].getY()  > 505) {
      balls[i].setPosition(random(50,450), -50);
      balls[i].setVelocity(0,0);      
    }
  }
  
  //Change Alpha variation 
  if (myBox.getImageAlpha()  >= 155 || myBox.getImageAlpha()  <= 2) alphaVar = - alphaVar; 
  myBox.setImageAlpha(myBox.getImageAlpha() + alphaVar);
  println(myBox.getImageAlpha());
}