From 29e04d2cca00cfbdbc1cec57c160bdd0d996ea5d Mon Sep 17 00:00:00 2001 From: Rynhardt Kruger Date: Mon, 27 Jun 2011 19:31:20 +0200 Subject: [PATCH] Totally rewroat the code for collision of solid objects. Also added an example application to test the environment. --- examples/WorldExample.java | 205 ++++++++++++++++++++++++ src/org/alterverse/sound/SoundManager.java | 4 + src/org/alterverse/world/Area.java | 23 ++- src/org/alterverse/world/DirectionalObject.java | 111 +++++++++++++ src/org/alterverse/world/GameObject.java | 36 +++-- src/org/alterverse/world/MovableObject.java | 90 +++++------ src/org/alterverse/world/Point.java | 66 ++++++++ 7 files changed, 464 insertions(+), 71 deletions(-) create mode 100644 examples/WorldExample.java create mode 100644 src/org/alterverse/world/DirectionalObject.java create mode 100644 src/org/alterverse/world/Point.java diff --git a/examples/WorldExample.java b/examples/WorldExample.java new file mode 100644 index 0000000..1786167 --- /dev/null +++ b/examples/WorldExample.java @@ -0,0 +1,205 @@ +import bcl.bcl; +import org.alterverse.world.*; +import org.alterverse.shapes.*; +import org.alterverse.sound.*; +import org.alterverse.game.*; +import org.alterverse.control.*; +import org.alterverse.speech.*; + +public class WorldExample extends GameContext { +boolean editing = false; +GameEngine engine; +Area area; +class Floor extends GameObject { +public Floor(Shape shape) { +super(area,shape); +} +} + +class Stair extends Floor { +public Stair(Shape shape) { +super(shape); +} +} + +class Wall extends GameObject { +public Wall(Shape shape) { +super(area,shape); +} +} + +class Human extends DirectionalObject { +Sound land; +Sound wall; +Sound watch; +Sound stair; +public Human(Shape shape) { +super(area,shape); +land = new Sound("sounds/step.wav"); +wall = new Sound("sounds/wall.wav"); +addSound(wall); +addSound(land); +watch = new Sound("sounds/watch.wav"); +addSound(watch); +stair = new Sound("sounds/stair.wav"); +addSound(stair); +setStepupSize(0.5); +} + +public void onFall(double distance) { +} + +public void updateListener() { +super.updateData(); +SoundManager.setListenerPosition(getX(),getY(),getZ()); +} + +public void onBump(GameObject othr) { +if (othr instanceof Wall) +wall.play(); +else if (othr instanceof Stair) +stair.play(); +else if (othr instanceof Floor) +land.play(); +if (othr instanceof Human) +watch.play(); +super.onBump(othr); +} +} + +Player player; +public WorldExample() { +engine = new GameEngine(this); + +area = new Area(); +area.add(new Floor(new Box(-30,-2,-20,60,2,22))); +area.add(new Floor(new Box(-30,-7,-19,60,2,40))); +area.add(new Stair(new Box(-2,-1.5,2,4,1,1))); +area.add(new Stair(new Box(-2,-2,3,4,1,1))); +area.add(new Stair(new Box(-2,-2.5,4,4,1,1))); +area.add(new Stair(new Box(-2,-3,5,4,1,1))); +area.add(new Stair(new Box(-2,-3.5,6,4,1,1))); +area.add(new Stair(new Box(-2,-4,7,4,1,1))); +area.add(new Stair(new Box(-2,-4.5,8,4,1,1))); +area.add(new Stair(new Box(-2,-5,9,4,1,1))); +area.add(new Stair(new Box(-2,-5.5,10,4,1,1))); +area.add(new Wall(new Box(-30,0,-3,30,10,1))); +area.add(new Wall(new Box(1,0,-3,10,10,1))); +player =new Player(new Box(-0.2,2,0.2,0.4,1.9,0.4)); +area.add(player); +area.add(new Nonplayer(new Box(-5,1,1,0.4,1.9,0.4))); +area.add(new Nonplayer(new Box(5,1,1,0.4,1.9,0.4))); +area.setGravity(true); +tts.speak("Starting up", 2); + +engine.run(); +} + +public void process() { +area.process(); +} + +public class Nonplayer extends Human { +int timer=0; +public Nonplayer(Shape shape) { +super(shape); +} + +public void process() { +timer++; +if (timer%20==0) { +double dx = (Math.random()*2)-1; +double dz = (Math.random()*2)-1; +setX(getX()+dx); +setZ(getZ()+dz); +} +} +} + +public class Player extends Human { +public Player(Shape shape) { +super(shape); +setSpeed(0.5); +} + +public void updateData() { +super.updateData(); +SoundManager.setListenerPosition(getX(), getY(), getZ()); +Point p = new Point(getDirection(),1); +SoundManager.setListenerOrientation(p.x,p.y,p.z,0,1,0); +} +} + +public void keyDown(int key) { +if (editing) { +switch(key) { +case 's': +if (player.getSolid()) +player.setSolid(false); +else +player.setSolid(true); +tts.speak(player.getSolid()? "Solid":"Non-solid", 2); +break; +case Keys.UP: +if (bcl.isKeyDown(Keys.LSHIFT)) +player.setY(player.getY()+0.5); +player.updateListener(); +break; +case Keys.DOWN: +if (bcl.isKeyDown(Keys.LSHIFT)) +player.setY(player.getY()-0.5); +player.updateListener(); +break; +} +} +switch(key) { +case 'e': +if (bcl.isKeyDown(Keys.LSHIFT) && bcl.isKeyDown(Keys.LALT)) { +if (editing) +editing = false; +else +editing = true; +tts.speak(editing? "Edit mode": "normal mode", 2); +area.setGravity(! editing); +} +break; +case Keys.LEFT: +if (bcl.isKeyDown(Keys.LSHIFT)) +player.turnLeft(); +else if (bcl.isKeyDown(Keys.LALT)) +player.turnLeft90(); +else +player.moveLeft(); +break; +case Keys.RIGHT: +if (bcl.isKeyDown(Keys.LSHIFT)) +player.turnRight(); +else if (bcl.isKeyDown(Keys.LALT)) +player.turnRight90(); +else +player.moveRight(); +break; +case Keys.UP: +if (! bcl.isKeyDown(Keys.LSHIFT)) +player.moveForward(); +break; +case Keys.DOWN: +if (! bcl.isKeyDown(Keys.LSHIFT)) +player.moveBackward(); +break; +case 'd': +tts.speak(""+player.getDirection(),2); +break; +case 'w': +tts.speak("position: " + player.getX()+", "+player.getY()+", " + player.getZ(),2); +break; +case 'q': +engine.quit(); +break; +} +} + +public static void main (String [] args) { +new WorldExample(); +} +} diff --git a/src/org/alterverse/sound/SoundManager.java b/src/org/alterverse/sound/SoundManager.java index 26ad98b..fec56a2 100644 --- a/src/org/alterverse/sound/SoundManager.java +++ b/src/org/alterverse/sound/SoundManager.java @@ -61,4 +61,8 @@ delsounds.remove(delsounds.get(i)); i--; } } + +public static void setListenerOrientation(double x1, double y1, double z1, double x2, double y2, double z2) { +bcl.setListenerOrientation((float)x1,(float)y1,(float)z1,(float)x2,(float)y2,(float)z2); +} } diff --git a/src/org/alterverse/world/Area.java b/src/org/alterverse/world/Area.java index d787405..b4ee0e9 100644 --- a/src/org/alterverse/world/Area.java +++ b/src/org/alterverse/world/Area.java @@ -38,22 +38,10 @@ public void process() { for (int i = 0; i < objects.size();i++) { // gravity stuff if (gravity&& objects.get(i) instanceof MovableObject && !((MovableObject)objects.get(i)).hasLanded()) { -objects.get(i).setY(objects.get(i).getY()-(10.0/32.0)); ((MovableObject)objects.get(i)).fall(); } // other stuff objects.get(i).process(); -if (objects.get(i) instanceof MovableObject) { -for (int j = 0; j < objects.size(); j++) { -if (j == i) -continue; -if (objects.get(i).isTouching(objects.get(j))) { -objects.get(i).onBump(objects.get(j)); -objects.get(j).onBump(objects.get(i)); -} -} -((MovableObject)objects.get(i)).restoreMovement(); -} } for (GameObject obj : dellist) { objects.remove(obj); @@ -96,4 +84,15 @@ this.gravity = gravity; public boolean getGravity() { return gravity; } + +public ArrayList getTouchingObjects(GameObject obj) { +ArrayList ret = new ArrayList(); +for (GameObject i: objects) { +if (i==obj) +continue; +if (i.isTouching(obj)) +ret.add(i); +} +return ret; +} } diff --git a/src/org/alterverse/world/DirectionalObject.java b/src/org/alterverse/world/DirectionalObject.java new file mode 100644 index 0000000..a952cc7 --- /dev/null +++ b/src/org/alterverse/world/DirectionalObject.java @@ -0,0 +1,111 @@ +package org.alterverse.world; + +import org.alterverse.shapes.*; + +public class DirectionalObject extends MovableObject { +double direction=0; +double speed=1; +double turnSize=5; +double stepupSize=0; +Point prev; +public DirectionalObject(Area area, Shape shape) { +super(area,shape); +prev=new Point(); +} + +public DirectionalObject(Area area, double x, double y, double z, Shape shape) { +super(area,x,y,z,shape); +prev=new Point(); +} + +public void setDirection(double degrees) { +direction=degrees; +} + +public double getDirection() { +return direction; +} + +public void setSpeed(double speed) { +this.speed = speed; +} + +public void moveForward() { +stepup(); +Point p = new Point(direction,speed); +setX(getX()+p.x); +setY(getY()+p.y); +setZ(getZ()+p.z); +} + +public void moveLeft() { +stepup(); +Point p = new Point(direction-90,speed); +setX(getX()+p.x); +setY(getY()+p.y); +setZ(getZ()+p.z); +} + +public void moveRight() { +stepup(); +Point p = new Point(direction+90,speed); +setX(getX()+p.x); +setY(getY()+p.y); +setZ(getZ()+p.z); +} + +public void moveBackward() { +stepup(); +Point p = new Point(direction+180,speed); +setX(getX()+p.x); +setY(getY()+p.y); +setZ(getZ()+p.z); +} + +public void stepup() { +if (stepupSize==0) +return; +if (x==prev.x&&z==prev.z&&y==prev.y) +return; +setY(getY()+stepupSize); +prev.x=x; +prev.y=y; +prev.z=z; +} + +public void turnRight() { +direction=(direction+turnSize)%360; +updateData(); +} + +public void turnLeft() { +direction=(direction-turnSize)%360; +updateData(); +} + +public double getTurnSize() { +return turnSize; +} + +public void setTurnSize(double turnSize) { +this.turnSize = turnSize; +} + +public void turnRight90() { +direction=(direction+90)%360; +updateData(); +} + +public void turnLeft90() { +direction=(direction-90)%360; +updateData(); +} + +public double getStepupSize() { +return stepupSize; +} + +public void setStepupSize(double size) { +stepupSize=size; +} +} diff --git a/src/org/alterverse/world/GameObject.java b/src/org/alterverse/world/GameObject.java index f48be7e..d1f7b8e 100644 --- a/src/org/alterverse/world/GameObject.java +++ b/src/org/alterverse/world/GameObject.java @@ -29,6 +29,9 @@ Shape shape; double x; double y; double z; +double prevX; +double prevY; +double prevZ; ArrayList sounds; boolean solid; Area myArea; @@ -44,6 +47,9 @@ this.shape=shape; solid=true; sounds=new ArrayList(); myArea=area; +prevX=x; +prevY=y; +prevZ=z; } public GameObject(Area area,double x,double y,double z,Shape shape) { @@ -54,6 +60,9 @@ this.shape=shape; solid=true; sounds=new ArrayList(); myArea = area; +prevX=x; +prevY=y; +prevZ=z; } public boolean isTouching(GameObject other) { @@ -61,6 +70,11 @@ return this.getShape().isTouching(other.getShape()); } public void onBump(GameObject obj) { + +} + +public boolean canEnter(GameObject othr) { +return !(this.isSolid()&&othr.isSolid()); } public boolean isSolid() { @@ -71,6 +85,10 @@ public void setSolid(boolean solid) { this.solid=solid; } +public boolean getSolid() { +return solid; +} + public void addSound(Sound sound) { sounds.add(sound); } @@ -84,9 +102,9 @@ return sounds; } public void setPosition(double x, double y, double z) { -setX(x); -setY(y); -setZ(z); +this.x = x; +this.y = y; +this.z = z; } public double getX() { @@ -105,22 +123,22 @@ public void updateData() { for (Sound sound : sounds) { sound.setPosition(x,y,z); } +} + +public void updateShape() { shape.setPosition(x,y,z); } public void setX(double x) { -this.x = x; -updateData(); +setPosition(x,y,z); } public void setY(double y) { -this.y = y; -updateData(); +setPosition(x,y,z); } public void setZ(double z) { -this.z = z; -updateData(); +setPosition(x,y,z); } public Shape getShape() { diff --git a/src/org/alterverse/world/MovableObject.java b/src/org/alterverse/world/MovableObject.java index d61e584..3ce5ab7 100644 --- a/src/org/alterverse/world/MovableObject.java +++ b/src/org/alterverse/world/MovableObject.java @@ -41,62 +41,48 @@ oldZ=z; } public void fall() { +if (oldX==this.x&&oldY max) +mx=max; +else +mx=lx; +if (ly max) +my=max; +else +my=ly; +if (lz max) +mz=max; +else +mz=lz; +Point p=new Point(x,y,z); +if (p.x!=0) +p.x=p.x/lx*mx; +if (p.y!=0) +p.y=p.y/ly*my; +if (p.z!=0) +p.z=p.z/lz*mz; +return p; +} +} -- 2.11.4.GIT