Skip to content

Instantly share code, notes, and snippets.

@andyli
Created January 12, 2011 05:40
Show Gist options
  • Save andyli/775746 to your computer and use it in GitHub Desktop.
Save andyli/775746 to your computer and use it in GitHub Desktop.
onthewings.stuffs.stuff3
/**
* Some of the renderings can be found at
* http://www.flickr.com/photos/andy-li/sets/72157625719497466/
*
*
* Copyright (c) 2011, Andy Li http://www.onthewings.net/
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* The name of the author may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package onthewings.stuffs.stuff3;
import cpp.Lib;
import cpp.Sys;
import haxe.FastList;
import hxColorToolkit.spaces.RGB;
import nme.geom.Point;
import of.Context;
import org.casalib.util.NumberUtil;
import phx.Body;
import phx.Circle;
import phx.col.AABB;
import phx.col.SortedList;
import phx.Const;
import phx.Material;
import phx.Properties;
import phx.Shape;
import phx.Vector;
import phx.World;
import hxColorToolkit.spaces.HSL;
using of.Context.Functions;
using Lambda;
using DateTools;
using StringTools;
using org.casalib.util.GeomUtil;
using org.casalib.util.NumberUtil;
using nme.geom.Point;
using hxColorToolkit.ColorToolkit;
class Main extends of.app.BaseApp
{
var width:Int;
var height:Int;
var screenCap:Image;
var world:World;
var balls:FastList<Ball>;
override public function setup():Void {
balls = new FastList<Ball>();
setFrameRate(60);
enableAlphaBlending();
enableSmoothing();
background(0, 0, 0);
noFill();
width = getWidth();
height = getHeight();
screenCap = new Image();
screenCap.allocate(width, height, Constants.OF_IMAGE_COLOR);
world = new World(new AABB( -500, -500, width + 1000, height + 1000), new SortedList());
//world.gravity = new Vector(0, 100);
world.useIslands = true;
//world.sleepEpsilon = 1;
/*
world.addStaticShape(Shape.makeBox(width, 50, 0, height)); //floor
world.addStaticShape(Shape.makeBox(width, 50, 0, -50)); //ceiling
world.addStaticShape(Shape.makeBox(50, height, -50, 0)); //left wall
world.addStaticShape(Shape.makeBox(50, height, width, 0)); //right wall
*/
var p = screenCap.getPixels();
for (i in 0...p.length) {
p[i] = cast 0;
}
screenCap.update();
var halfLength = 100;
for (i in -halfLength...halfLength) {
for (j in -halfLength...halfLength) {
var ball = new Ball(width * 0.5 + i, height * 0.5 +j, 0.5);
world.addBody(ball.body);
ball.color = new RGB(255, 255, 255);
ball.alpha = 10;
balls.add(ball);
}
}
}
override public function draw():Void {
var framenum = getFrameNum();
var ball = new Ball(width * 0.5, height * 0.5, framenum * 0.1);
var pt = new Point(0, framenum);
pt.rotatePoint(new Point(), framenum % 360);
ball.body.setSpeed(pt.x, pt.y);
world.addBody(ball.body);
ball.color = new RGB(255, 0, 0);
ball.alpha = 0;
balls.add(ball);
world.step(1 / 3000, 100);
for (b in balls) {
if (b.alpha <= 0) continue;
var rgb = b.color;
setColor(cast rgb.red, cast rgb.green, cast rgb.blue, b.alpha);
circle(b.body.x, b.body.y, b.radius);
}
screenCap.grabScreen(0, 0, width, height);
screenCap.saveImage(Date.now().format("%Y%m%d_") + Std.string(getFrameNum()).lpad("0",5) + ".png");
}
public static function main():Void {
AppRunner.setupOpenGL(new AppGlutWindow(), 1280, 720, Constants.OF_WINDOW);
AppRunner.runApp(new Main());
}
}
class Ball {
public var body(default,null):Body;
public var radius(default,null):Float;
public var color:RGB;
public var alpha:Int;
public function new(x:Float, y:Float, r:Float, ?mat:Material, ?prop:Properties):Void {
body = new Body(x, y, prop);
body.addShape(new Circle(radius = r, new Vector(0, 0), mat));
bodyDict.set(body.id, this);
}
static public var bodyDict = new IntHash<Ball>();
static public function getFromBody(b:Body):Ball {
return bodyDict.get(b.id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment