guys...... help...urgent.....
guys, i have this big problem about our program. this is for our thesis... this is in opengl, can anybody help me with this? this program is made up of 3 dice that suppose to bounce at the boundaries and with each other... help me guys, please... heres the code... if you are interested.. i can give you the whole program... but heres the program that really needs an urgent help... thanks. but if anyone can add up a little physics to make it more realistic, i will be more thankful. thanks guys.. hoping to hear from you soon..
*****************************************************************************
*****************************************************************************
/*
* Dice.java
*
* Created on 08 February 2007, 22:26
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package longgong;
import java.net.URL;
import gl4java.GLContext;
import gl4java.awt.GLAnimCanvas;
import gl4java.utils.textures.*;
/**
*
* @author welcome
*/
public class Dice {
GLAnimCanvas glCanvas ;
URL codeBase;
int[]texture1 = new int[1]; //Storage for one texture ( NEW )
int[]texture2 = new int[1]; //Storage for one texture ( NEW )
int[]texture3 = new int[1]; //Storage for one texture ( NEW )
int[]texture4 = new int[1]; //Storage for one texture ( NEW )
int[]texture5 = new int[1]; //Storage for one texture ( NEW )
int[]texture6 = new int[1]; //Storage for one texture ( NEW )
float rx;
float ry;
float rz;
float tx;
float ty;
float tz;
float speed;
boolean txMoveLeft;
boolean tyMoveUp;
boolean tzMoveFront;
/** Creates a new instance of Dice */
public Dice(GLAnimCanvas g) {
glCanvas = g;
if(!LoadGLTexture("c:\\java\\images\\one.png",texture1)){
System.out.println("Failed to load Textures,Bailing!");
System.exit(0);
}
if(!LoadGLTexture("c:\\java\\images\\two.png",texture2)){
System.out.println("Failed to load Textures,Bailing!");
System.exit(0);
}
if(!LoadGLTexture("c:\\java\\images\\three.png",texture3)){
System.out.println("Failed to load Textures,Bailing!");
System.exit(0);
}
if(!LoadGLTexture("c:\\java\\images\\four.png",texture4)){
System.out.println("Failed to load Textures,Bailing!");
System.exit(0);
}
if(!LoadGLTexture("c:\\java\\images\\five.png",texture5)){
System.out.println("Failed to load Textures,Bailing!");
System.exit(0);
}
if(!LoadGLTexture("c:\\java\\images\\six.png",texture6)){
System.out.println("Failed to load Textures,Bailing!");
System.exit(0);
}
txMoveLeft = true;
tyMoveUp = true;
}
public boolean inCircleBoundaries(float x, float y, float radius)
{
float r;
r = (float)Math.sqrt((x*x) + (y*y));
if (r <= radius)
return true;
else
return false;
}
public void setCoordinates(float corRx,float corRy,float corRz,float corTx,float corTy,float corTz){
rx = corRx;
ry = corRy;
rz = corRz;
tx = corTx;
ty = corTy;
tz = corTz;
}
public void getRandomDice(){
int num = (int)(Math.random() * 6);
if (num==1){
rx=0.0f;
ry=0.0f;
rz=0.0f;
}
else if (num==2){
rx=90.0f;
ry=0.0f;
rz=0.0f;
}
else if (num==3){
rx=0.0f;
ry=-90.0f;
rz=0.0f;
}
else if (num==4){
rx=0.0f;
ry=90.0f;
rz=0.0f;
}
else if (num==5){
rx=-90.0f;
ry=0.0f;
rz=0.0f;
}
else{
rx=180.0f;
ry=0.0f;
rz=0.0f;
}
}
public float getTx(){
return tx+0.5f;
}
public float getTy(){
return ty+0.5f;
}
public float getTz(){
return tz+0.5f;
}
public void setTz(float z){
tz=z;
}
public boolean getMoveLeft(){
return txMoveLeft;
}
public boolean getMoveUp(){
return tyMoveUp;
}
public void setMoveLeft(boolean m){
txMoveLeft = m;
}
public void setMoveUp(boolean m){
tyMoveUp = m;
}
public void startDiceMovement(){
if (txMoveLeft==true) {
tx+=speed;
}
else {
tx-=speed;
}
if (tyMoveUp==true) {
ty+=speed;
}
else {
ty-=speed;
}
ry+=(speed*4);
rx+=(speed*4);
//rz+=(speed*2);
//tx+=speed;
//ty+=speed;
//tz+=speed;
if (inCircleBoundaries(tx,ty,3.5f)==false) {
if (tx>=3.0f) txMoveLeft = false;
if (tx<=-3.0f) txMoveLeft = true;
if (ty>=3.0f) tyMoveUp = false;
if (ty<=-3.0f) tyMoveUp = true;
}
}
public void setSpeed(float s){
speed = s;
}
public void drawDice(){
glCanvas.gl.glLoadIdentity();// Reset The View
glCanvas.gl.glTranslatef(tx,ty,tz);
glCanvas.gl.glRotatef(rx,1.0f,0.0f,0.0f);
glCanvas.gl.glRotatef(ry,0.0f,1.0f,0.0f);
glCanvas.gl.glRotatef(rz,0.0f,0.0f,1.0f);
glCanvas.gl.glBindTexture(glCanvas.gl.GL_TEXTURE_2D, texture1[0]);
glCanvas.gl.glBegin(glCanvas.gl.GL_QUADS);
// Front Face
glCanvas.gl.glTexCoord2f(0.0f, 0.0f); glCanvas.gl.glVertex3f(-1.0f, -1.0f, 1.0f);
glCanvas.gl.glTexCoord2f(1.0f, 0.0f); glCanvas.gl.glVertex3f( 1.0f, -1.0f, 1.0f);
glCanvas.gl.glTexCoord2f(1.0f, 1.0f); glCanvas.gl.glVertex3f( 1.0f, 1.0f, 1.0f);
glCanvas.gl.glTexCoord2f(0.0f, 1.0f); glCanvas.gl.glVertex3f(-1.0f, 1.0f, 1.0f);
glCanvas.gl.glEnd();
glCanvas.gl.glBindTexture(glCanvas.gl.GL_TEXTURE_2D, texture6[0]);
glCanvas.gl.glBegin(glCanvas.gl.GL_QUADS);
// Back Face
glCanvas.gl.glTexCoord2f(1.0f, 0.0f); glCanvas.gl.glVertex3f(-1.0f, -1.0f, -1.0f);
glCanvas.gl.glTexCoord2f(1.0f, 1.0f); glCanvas.gl.glVertex3f(-1.0f, 1.0f, -1.0f);
glCanvas.gl.glTexCoord2f(0.0f, 1.0f); glCanvas.gl.glVertex3f( 1.0f, 1.0f, -1.0f);
glCanvas.gl.glTexCoord2f(0.0f, 0.0f); glCanvas.gl.glVertex3f( 1.0f, -1.0f, -1.0f);
glCanvas.gl.glEnd();
glCanvas.gl.glBindTexture(glCanvas.gl.GL_TEXTURE_2D, texture2[0]);
glCanvas.gl.glBegin(glCanvas.gl.GL_QUADS);
// Top Face
glCanvas.gl.glTexCoord2f(0.0f, 1.0f); glCanvas.gl.glVertex3f(-1.0f, 1.0f, -1.0f);
glCanvas.gl.glTexCoord2f(0.0f, 0.0f); glCanvas.gl.glVertex3f(-1.0f, 1.0f, 1.0f);
glCanvas.gl.glTexCoord2f(1.0f, 0.0f); glCanvas.gl.glVertex3f( 1.0f, 1.0f, 1.0f);
glCanvas.gl.glTexCoord2f(1.0f, 1.0f); glCanvas.gl.glVertex3f( 1.0f, 1.0f, -1.0f);
glCanvas.gl.glEnd();
glCanvas.gl.glBindTexture(glCanvas.gl.GL_TEXTURE_2D, texture5[0]);
glCanvas.gl.glBegin(glCanvas.gl.GL_QUADS);
// Bottom Face
glCanvas.gl.glTexCoord2f(1.0f, 1.0f); glCanvas.gl.glVertex3f(-1.0f, -1.0f, -1.0f);
glCanvas.gl.glTexCoord2f(0.0f, 1.0f); glCanvas.gl.glVertex3f( 1.0f, -1.0f, -1.0f);
glCanvas.gl.glTexCoord2f(0.0f, 0.0f); glCanvas.gl.glVertex3f( 1.0f, -1.0f, 1.0f);
glCanvas.gl.glTexCoord2f(1.0f, 0.0f); glCanvas.gl.glVertex3f(-1.0f, -1.0f, 1.0f);
glCanvas.gl.glEnd();
glCanvas.gl.glBindTexture(glCanvas.gl.GL_TEXTURE_2D, texture3[0]);
glCanvas.gl.glBegin(glCanvas.gl.GL_QUADS);
// Right face
glCanvas.gl.glTexCoord2f(1.0f, 0.0f); glCanvas.gl.glVertex3f( 1.0f, -1.0f, -1.0f);
glCanvas.gl.glTexCoord2f(1.0f, 1.0f); glCanvas.gl.glVertex3f( 1.0f, 1.0f, -1.0f);
glCanvas.gl.glTexCoord2f(0.0f, 1.0f); glCanvas.gl.glVertex3f( 1.0f, 1.0f, 1.0f);
glCanvas.gl.glTexCoord2f(0.0f, 0.0f); glCanvas.gl.glVertex3f( 1.0f, -1.0f, 1.0f);
glCanvas.gl.glEnd();
glCanvas.gl.glBindTexture(glCanvas.gl.GL_TEXTURE_2D, texture4[0]);
glCanvas.gl.glBegin(glCanvas.gl.GL_QUADS);
// Left Face
glCanvas.gl.glTexCoord2f(0.0f, 0.0f); glCanvas.gl.glVertex3f(-1.0f, -1.0f, -1.0f);
glCanvas.gl.glTexCoord2f(1.0f, 0.0f); glCanvas.gl.glVertex3f(-1.0f, -1.0f, 1.0f);
glCanvas.gl.glTexCoord2f(1.0f, 1.0f); glCanvas.gl.glVertex3f(-1.0f, 1.0f, 1.0f);
glCanvas.gl.glTexCoord2f(0.0f, 1.0f); glCanvas.gl.glVertex3f(-1.0f, 1.0f, -1.0f);
glCanvas.gl.glEnd();
}
public boolean LoadGLTexture(String fileName, int textureNumber[])
{
PngTextureLoader texLoader = new PngTextureLoader(glCanvas.gl, glCanvas.glu);
if(codeBase!=null)
texLoader.readTexture(codeBase, fileName);
else
texLoader.readTexture(fileName);
if(texLoader.isOk())
{
//Create Texture
glCanvas.gl.glGenTextures(1, textureNumber);
glCanvas.gl.glBindTexture(glCanvas.gl.GL_TEXTURE_2D, textureNumber[0]);
glCanvas.gl.glTexParameteri(glCanvas.gl.GL_TEXTURE_2D, glCanvas.gl.GL_TEXTURE_MAG_FILTER, glCanvas.gl.GL_LINEAR);
glCanvas.gl.glTexParameteri(glCanvas.gl.GL_TEXTURE_2D, glCanvas.gl.GL_TEXTURE_MIN_FILTER, glCanvas.gl.GL_LINEAR);
glCanvas.gl.glTexImage2D(glCanvas.gl.GL_TEXTURE_2D,
0,
3,
texLoader.getImageWidth(),
texLoader.getImageHeight(),
0,
glCanvas.gl.GL_RGB,
glCanvas.gl.GL_UNSIGNED_BYTE,
texLoader.getTexture());
return true;
}
return false;
}
}

