ホームに戻る
サンプル2:ひとりPONG
図10_00:w と z でラケットを操作
コードは pong_applet.java と racket.java と tama.java の3つあります。
/*
pong_applet.java
*/
import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.util.Random;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.TextureLoader;
/* <applet code="pong_applet" width=300 height=300></applet> */
public class pong_applet extends Applet implements Runnable{
private SimpleUniverse universe = null;
private Canvas3D canvas3d = null;
private boolean mLoop = false;
private Thread mThread = null;
private boolean mInited = false;
private long mCycle = 2000;
private long mTick = 0;
private long mTimeOld;
private float mFInterval = 1.0f / 2000.0f;
private float bg_red = 0.0f;
private float bg_green = 0.0f;
private float bg_blue = 1.0f;
private float jiki_y = 0.0f;
private boolean up_flag = false;
private boolean down_flag = false;
private long keyTime = 0;
private long tamaTime = 0;
private float tama_x = 0.0f;
private float tama_y = 0.0f;
private float tama_dx = 0.002f;
private float tama_dy = 0.001f;
public void init() {
int i;
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
canvas3d = new Canvas3D(config);
this.setLayout(new BorderLayout());
this.add(canvas3d, BorderLayout.CENTER);
canvas3d.stopRenderer();
canvas3d.setDoubleBufferEnable(true);
Transform3D pers = new Transform3D();
Transform3D lookat = new Transform3D();
universe = new SimpleUniverse(canvas3d);
lookat.set(Transform3D.ZERO);
pers.set(Transform3D.ZERO);
Point3d eye = new Point3d(0.0, 0.0, 2.0);
Point3d center = new Point3d(0.0, 0.0, 0.0);
Vector3d upvector = new Vector3d(0.0, 1.0, 0.0);
lookat.lookAt(eye, center, upvector);
pers.perspective(Math.toRadians(90.0), 1.0, 0.1, 10.0);
canvas3d.getView().setWindowEyepointPolicy(View.RELATIVE_TO_COEXISTENCE);
canvas3d.getView().setProjectionPolicy(View.PERSPECTIVE_PROJECTION);
canvas3d.getView().setCompatibilityModeEnable(true);
canvas3d.getView().setVpcToEc(lookat);
canvas3d.setMonoscopicViewPolicy(View.LEFT_EYE_VIEW);
canvas3d.getView().setLeftProjection(pers);
canvas3d.getView().setScreenScalePolicy(View.SCALE_SCREEN_SIZE);
mTimeOld = System.currentTimeMillis();
mInited = true;
key_class key = new key_class();
canvas3d.addKeyListener(key);
tamaTime = System.currentTimeMillis();
}
public void render(){
int i;
GraphicsContext3D gc;
gc = canvas3d.getGraphicsContext3D();
gc.clear();
Background background = new Background(new Color3f(bg_red, bg_green, bg_blue));
gc.setBackground(background);
Transform3D t3d1 = new Transform3D();
// 自機
if(up_flag){
if(jiki_y < 1.8f){
jiki_y += 0.005f * (System.currentTimeMillis() - keyTime);
keyTime = System.currentTimeMillis();
}
}
else if(down_flag){
if(jiki_y > -1.8f){
jiki_y -= 0.005f * (System.currentTimeMillis() - keyTime);
keyTime = System.currentTimeMillis();
}
}
t3d1.setTranslation(new Vector3f(-0.8f, jiki_y, 0.0f));
gc.setModelTransform(t3d1);
racket rac1 = new racket();
Shape3D shape_rac1 = new Shape3D(rac1);
gc.draw(shape_rac1);
// 自機2
t3d1.setTranslation(new Vector3f(0.8f, jiki_y, 0.0f));
gc.setModelTransform(t3d1);
racket rac2 = new racket();
Shape3D shape_rac2 = new Shape3D(rac2);
gc.draw(shape_rac2);
// タマ
tama_x += tama_dx * (System.currentTimeMillis() - tamaTime);
tama_y += tama_dy * (System.currentTimeMillis() - tamaTime);
tamaTime = System.currentTimeMillis();
if(tama_x > -0.80f && tama_x < -0.75f){
if(tama_y > (jiki_y - 0.4f) && tama_y < (jiki_y + 0.4f)){
tama_dx = 0.002f;
tama_dy = (tama_y - jiki_y) * 0.01f;
bg_red = 0.0f;
bg_green = 0.0f;
bg_blue = 1.0f;
}
}
if(tama_x < 0.80f && tama_x > 0.75f){
if(tama_y > (jiki_y - 0.4f) && tama_y < (jiki_y + 0.4f)){
tama_dx = -0.002f;
tama_dy = (tama_y - jiki_y) * 0.01f;
bg_red = 0.0f;
bg_green = 0.0f;
bg_blue = 1.0f;
}
}
if(tama_x >= 1.95f || tama_x <= -1.95f){
tama_dx *= -1;
bg_red = 1.0f;
bg_green = 0.0f;
bg_blue = 0.0f;
}
if(tama_y >= 1.95f || tama_y <= -1.95f){
tama_dy *= -1;
}
t3d1.setTranslation(new Vector3f(tama_x, tama_y, 0.0f));
gc.setModelTransform(t3d1);
tama tama1 = new tama();
Shape3D shape_tama = new Shape3D(tama1);
gc.draw(shape_tama);
canvas3d.swap();
}
public void start(){
mLoop = true;
if(mThread == null){
mThread = new Thread(this);
mThread.start();
}
else{
if(!mThread.isAlive()){
mThread = new Thread(this);
mThread.start();
}
}
}
public void run(){
long current,elapsed;
while(mLoop){
current = System.currentTimeMillis();
elapsed = current - mTimeOld;
if(elapsed > 15){
mTimeOld = current;
mTick += elapsed;
mTick %= mCycle;
if(mInited){
render();
}
}
mThread.yield();
}
}
public void stop(){
mLoop = false;
}
public void destroy(){
universe.cleanup();
}
class key_class implements KeyListener{
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == 87){ // pressed w
up_flag = true;
keyTime = System.currentTimeMillis();
}
else if(e.getKeyCode() == 90){ // pressed z
down_flag = true;
keyTime = System.currentTimeMillis();
}
}
public void keyReleased(KeyEvent e){
if(e.getKeyCode() == 87){ // pressed w
up_flag = false;
}
if(e.getKeyCode() == 90){ // pressed z
down_flag = false;
}
}
public void keyTyped(KeyEvent e){}
}
}
/*
racket.java
*/
import javax.media.j3d.*;
import javax.vecmath.*;
public class racket extends TriangleArray{
public racket(){
super(6, TriangleArray.COORDINATES | TriangleArray.NORMALS);
Point3d[] verteces = new Point3d[4];
Point3d[] verteces2 = new Point3d[6];
verteces[0] = new Point3d(-0.040000,0.200000,0.000000);
verteces[1] = new Point3d(-0.040000,-0.200000,0.000000);
verteces[2] = new Point3d(0.040000,-0.200000,0.000000);
verteces[3] = new Point3d(0.040000,0.200000,0.000000);
int[] indeces = {
0,1,2,2,3,0,0,0
};
for(int i = 0; i < 6; i++){
verteces2[i] = verteces[indeces[i]];
}
this.setCoordinates(0, verteces2);
Vector3f[] normals = calcFaceNormals(verteces2);
this.setNormals(0, normals);
}
private Vector3f[] calcFaceNormals(Point3d[] vertices){
Vector3f[] normals = new Vector3f[vertices.length];
for(int i=0; i<vertices.length; i+=3){
Vector3f fnormal = calcFaceNormal(i, vertices);
normals[i] = fnormal;
normals[i + 1] = fnormal;
normals[i + 2] = fnormal;
}
return normals;
}
private Vector3f calcFaceNormal(int index, Point3d[] vertices){
double ax = vertices[index + 2].x - vertices[index + 1].x;
double ay = vertices[index + 2].y - vertices[index + 1].y;
double az = vertices[index + 2].z - vertices[index + 1].z;
double bx = vertices[index + 0].x - vertices[index + 1].x;
double by = vertices[index + 0].y - vertices[index + 1].y;
double bz = vertices[index + 0].z - vertices[index + 1].z;
double nx = ay * bz - az * by;
double ny = az * bx - ax * bz;
double nz = ax * by - ay * bx;
Vector3f normal = new Vector3f((float)nx, (float)ny, (float)nz);
normal.normalize();
return normal;
}
}
/*
tama.java
*/
import javax.media.j3d.*;
import javax.vecmath.*;
public class tama extends TriangleArray{
public tama(){
super(6, TriangleArray.COORDINATES | TriangleArray.NORMALS);
Point3d[] verteces = new Point3d[4];
Point3d[] verteces2 = new Point3d[6];
verteces[0] = new Point3d(-0.020000,0.020000,0.000000);
verteces[1] = new Point3d(-0.020000,-0.020000,0.000000);
verteces[2] = new Point3d(0.020000,-0.020000,0.000000);
verteces[3] = new Point3d(0.020000,0.020000,0.000000);
int[] indeces = {
0,1,2,2,3,0,0,0
};
for(int i = 0; i < 6; i++){
verteces2[i] = verteces[indeces[i]];
}
this.setCoordinates(0, verteces2);
Vector3f[] normals = calcFaceNormals(verteces2);
this.setNormals(0, normals);
}
private Vector3f[] calcFaceNormals(Point3d[] vertices){
Vector3f[] normals = new Vector3f[vertices.length];
for(int i=0; i<vertices.length; i += 3){
Vector3f fnormal = calcFaceNormal(i, vertices);
normals[i] = fnormal;
normals[i + 1] = fnormal;
normals[i + 2] = fnormal;
}
return normals;
}
private Vector3f calcFaceNormal(int index, Point3d[] vertices){
double ax = vertices[index + 2].x - vertices[index + 1].x;
double ay = vertices[index + 2].y - vertices[index + 1].y;
double az = vertices[index + 2].z - vertices[index + 1].z;
double bx = vertices[index + 0].x - vertices[index + 1].x;
double by = vertices[index + 0].y - vertices[index + 1].y;
double bz = vertices[index + 0].z - vertices[index + 1].z;
double nx = ay * bz - az * by;
double ny = az * bx - ax * bz;
double nz = ax * by - ay * bx;
Vector3f normal = new Vector3f((float)nx, (float)ny, (float)nz);
normal.normalize();
return normal;
}
}