/* * Copyright (c) 2009 Anthony Steed and Manuel Fradinho Oliveira * All rights reserved. * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the * following conditions are met: * 1. Redistributions of source code must retain the above * copyright notice, this list of conditions and the * following disclaimer. * 2. 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. * 3. All advertising materials mentioning features or use of * this software must display the following acknowledgement: * This product includes software from the book "Networked * Graphics" written by Anthony Steed and Manuel Fradinho * Oliveira published by Morgan Kaufmann * 4. Neither the name of the University nor of the Department * may be used to endorse or promote products derived from * this software without specific prior written permission. * 5. Neither the name of Cyntelix may be used to enforese or * promote products derived from this software without prior * written permission. * * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY 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 UNIVERSITY 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. * * Contact Info: Anthony Steed * a.steed@cs.ucl.ac.uk * * Manuel Fradinho Oliveira * mfradinho@cyntelix.com * */ import javax.media.j3d.*; import javax.vecmath.*; class BoidShape extends TriangleArray { private static final float[] verts = { 0.8f, 0.3f, 0.4f, // 2 0.0f, 0.0f, -1.0f, // 1 0.0f, 0.0f, 0.0f, // 0 0.0f, 0.0f, -1.0f, // 1 -0.8f, 0.3f, 0.4f, // 3 0.0f, 0.0f, 0.0f, // 0 0.0f, -0.1f, 0.0f, // 4 -0.8f, 0.3f, 0.4f, // 3 0.0f, 0.0f, -1.0f, // 1 0.8f, 0.3f, 0.4f, // 2 0.0f, -0.1f, 0.0f, // 4 0.0f, 0.0f, -1.0f, // 1 0.0f, 0.0f, 0.0f, // 0 0.0f, -0.1f, 0.0f, // 4 0.8f, 0.3f, 0.4f, // 2 0.0f, 0.0f, 0.0f, // 0 -0.8f, 0.3f, 0.4f, // 3 0.0f, -0.1f, 0.0f, // 4 }; public BoidShape (Color3f colour) { super(18, GeometryArray.COORDINATES | GeometryArray.COLOR_3); setColor(0,colour); setCoordinates(0, verts); } } public class Boid extends TransformGroup { Shape3D _geom; Vector3f _position; Vector3f _velocity; static Vector3f _world_min_pt = new Vector3f(-10.0f, -10.0f, -10.0f); static Vector3f _world_max_pt = new Vector3f(10.0f, 10.0f, 10.0f); static float BOUND_VELOCITY = 0.2f; public Boid() { this(0.1f, 0.8f, 0.3f); } public Boid(float red, float green, float blue) { this(new Color3f(red, green, blue)); } public Boid(Color3f colour) { _position = new Vector3f(); _velocity = new Vector3f(); setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); setCapability(TransformGroup.ALLOW_TRANSFORM_READ); _geom = new Shape3D(); _geom.setGeometry(new BoidShape(colour)); addChild(_geom); } public void worldConstraints() { if (_position.x > _world_max_pt.x) _velocity.x += -BOUND_VELOCITY; if (_position.x < _world_min_pt.x) _velocity.x += BOUND_VELOCITY; if (_position.y > _world_max_pt.y) _velocity.y += -BOUND_VELOCITY; if (_position.y < _world_min_pt.y) _velocity.y += BOUND_VELOCITY; if (_position.z > _world_max_pt.z) _velocity.z += -BOUND_VELOCITY; if (_position.z < _world_min_pt.z) _velocity.z += BOUND_VELOCITY; } public void randomPos() { _position.x = _world_min_pt.x + ((_world_max_pt.x - _world_min_pt.x)*(float)Math.random()); _position.y = _world_min_pt.y + ((_world_max_pt.y - _world_min_pt.y)*(float)Math.random()); _position.z = _world_min_pt.z + ((_world_max_pt.z - _world_min_pt.z)*(float)Math.random()); _velocity.x = 0; _velocity.y = 0; _velocity.z = 0; } public String toString() { StringBuffer str = new StringBuffer(); str.append(_position.x); str.append('@'); str.append(_position.y); str.append('@'); str.append(_position.z); str.append('@'); str.append(_velocity.x); str.append('@'); str.append(_velocity.y); str.append('@'); str.append(_velocity.z); return str.toString(); } public void unpackString(String str) { int index = str.indexOf('@'); _position.x = Float.valueOf(str.substring(0,index)); str = str.substring(index+1); index = str.indexOf('@'); _position.y = Float.valueOf(str.substring(0,index)); str = str.substring(index+1); index = str.indexOf('@'); _position.z = Float.valueOf(str.substring(0,index)); str = str.substring(index+1); index = str.indexOf('@'); _velocity.x = Float.valueOf(str.substring(0,index)); str = str.substring(index+1); index = str.indexOf('@'); _velocity.y = Float.valueOf(str.substring(0,index)); str = str.substring(index+1); _velocity.z = Float.valueOf(str); } }