Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  question about Tour3D(the source of the Andrew Davison 's book)  (Read 1144 times)
0 Members and 1 Guest are viewing this topic.
Offline jiyanliang

Junior Newbie





« Posted 2007-04-21 11:55:10 »

In Tour3D'Obstacles  class,i want to use BoundingBox instead of BoundingSphere.But it doesn't work.
Here is source:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
// Obstacles.java
// Andrew Davison, April 2005, ad@fivedots.coe.psu.ac.th

/* Obstacles stores a 2D boolean array (obs) representing the
   XZ plane. The row index is the z-axis, the column index
   is the x-axis. obs[0][0] is the back left hand corner of
   the plane, which is the (x,z) point (0-FLOOR_LEN/2, 0-FLOOR_LEN/2).

   Obstacles can only be positioned at integer positions due
   to the use of array subscripts to mark the obstacle positions.
*/


import java.util.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.*;


public class Obstacles
{
  private final static float RADIUS = 0.1f;   // radius of obstacle
 private final static float HEIGHT = 1.0f;   // height of obstacle

  private final static int FLOOR_LEN = 50;  
      // should be the same as the floor size in CheckerFloor, and be even

  // colours for obs material
 private final static Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
  private final static Color3f specular = new Color3f(1.0f, 1.0f, 1.0f);
  private final static Color3f red = new Color3f(0.9f, 0.1f, 0.2f);


  private boolean obs[][];    // whether a (x,z) has an obstacle
 //private BoundingSphere obsBounds[][];   // the bounding sphere for obs
 private BoundingBox obsBounds[][];   // the bounding sphere for obs
 private Group obsGroup;     // group of 3d obstacles shown in world


  public Obstacles()
  {
    obs = new boolean[FLOOR_LEN+1][FLOOR_LEN+1];    // larger than FLOOR_LEN*FLOOR_LEN
   //obsBounds = new BoundingSphere[FLOOR_LEN+1][FLOOR_LEN+1];
   obsBounds = new BoundingBox[FLOOR_LEN+1][FLOOR_LEN+1];
    for (int z=0; z <= FLOOR_LEN; z++)
      for(int x=0; x <= FLOOR_LEN; x++) {
        obs[z][x] = false;
        obsBounds[z][x] = null;
       // obsBounds2[z][x] = null;
     }

    obsGroup = new Group();

  }  // end of Obstacles()


  public void store(String line)
  /* The line will have the format:
        (x1,z1) (x2,z2) ...
  */

  {
    int x=0, z=0;
    String coordStr;
    StringTokenizer points;
    StringTokenizer coords = new StringTokenizer(line);
    while (coords.hasMoreTokens()) {
      coordStr = coords.nextToken();    // should be a single (x,z)
     points = new StringTokenizer(coordStr, "(,)");
      try {
        x = Integer.parseInt(points.nextToken());
        z = Integer.parseInt(points.nextToken());
      }
      catch (NumberFormatException ex){
        System.out.println("Incorrect format for obstacle data in tours file");
        break;
      }
      markObstacle(x,z);
    }
  }  // end of store()
 
  public void store2(String line)
  /* The line will have the format:
        (x1,z1) (x2,z2) ...
  */

  {
    int x=0, z=0;
    String coordStr;
    StringTokenizer points;
    StringTokenizer coords = new StringTokenizer(line);
    while (coords.hasMoreTokens()) {
      coordStr = coords.nextToken();    // should be a single (x,z)
     points = new StringTokenizer(coordStr, "(,)");
      try {
        x = Integer.parseInt(points.nextToken());
        z = Integer.parseInt(points.nextToken());
      }
      catch (NumberFormatException ex){
        System.out.println("Incorrect format for obstacle data in tours file");
        break;
      }
      markObstacle2(x,z);
    }
  }  // end of store()


  private void markObstacle(int x, int z)
  {
    if ((x < -FLOOR_LEN/2) || (x > FLOOR_LEN/2)) {
      System.out.println("Obstacle x point out of bounds: " + x);
      x = 0;
    }
    if ((z < -FLOOR_LEN/2) || (z > FLOOR_LEN/2)) {
      System.out.println("Obstacle z point out of bounds: " + z);
      z = 0;
    }
    obs[z+(FLOOR_LEN/2)][x+(FLOOR_LEN/2)] = true;
    obsBounds[z+(FLOOR_LEN/2)][x+(FLOOR_LEN/2)] =
         new BoundingBox( new Point3d(x-0.5, 0.0, z+0.5), new Point3d(x+0.5, 1.0, z-0.5));
   

   obsGroup.addChild( makeObs(x, z));

  }  // end of markObstacle()
 
  private void markObstacle2(int x, int z)
  {
    if ((x < -FLOOR_LEN/2) || (x > FLOOR_LEN/2)) {
      System.out.println("Obstacle x point out of bounds: " + x);
      x = 0;
    }
    if ((z < -FLOOR_LEN/2) || (z > FLOOR_LEN/2)) {
      System.out.println("Obstacle z point out of bounds: " + z);
      z = 0;
    }
    obs[z+(FLOOR_LEN/2)][x+(FLOOR_LEN/2)] = true;
    /*obsBounds[z+(FLOOR_LEN/2)][x+(FLOOR_LEN/2)] =
         new BoundingSphere( new Point3d(x, 0.0, z), RADIUS);*/

    obsBounds[z+(FLOOR_LEN/2)][x+(FLOOR_LEN/2)] =
      new BoundingBox( new Point3d(x-0.5, 0.0, z+0.5), new Point3d(x+0.5, 2.0, z-0.5));

   obsGroup.addChild( makeObs2(x, z));

  }  // end of markObstacle()


  public void print()
  {
    // header - markers for each 5 units, and a line
   for(int x=(-FLOOR_LEN/2); x <= (FLOOR_LEN/2); x++) {
      if (x == 0)
        System.out.print("0");
      else if (x%5 == 0)
        System.out.print("*");
      else
        System.out.print(" ");
    }
    System.out.println("");
    for(int x=(-FLOOR_LEN/2); x <= (FLOOR_LEN/2); x++)
      System.out.print("-");
    System.out.println("");

    // body of table: O's and a z-axis at the end of each line
   for (int z=0; z <= FLOOR_LEN; z++) {
      for(int x=0; x <= FLOOR_LEN; x++) {
        if (obs[z][x])
          System.out.print("O");
        else
          System.out.print(" ");
      }
      if ((z-FLOOR_LEN/2)%5 == 0)
        System.out.println("| " + (z-FLOOR_LEN/2));
      else
        System.out.println("|");
    }

    // footer: a line
   for(int x=(-FLOOR_LEN/2); x <= (FLOOR_LEN/2); x++)
      System.out.print("-");
    System.out.println("");
  }  // end of print()


  public boolean nearObstacle(Point3d pos, double radius)
  /* posn is not allowed to be outside the floor area.
     Also, every obstacle's bounding sphere is checked against pos
  */

  {
    if ((pos.x < -FLOOR_LEN/2) || (pos.x > FLOOR_LEN/2) ||
        (pos.z < -FLOOR_LEN/2) || (pos.z > FLOOR_LEN/2))     // off the floor
     return true;

    // check if near any obstacles
   BoundingBox bs = new BoundingBox( new Point3d(pos.x-0.5,pos.y,pos.z+0.5), new Point3d(pos.x+0.5,pos.y+1,pos.z-0.5));
    for (int z=0; z <= FLOOR_LEN; z++)
      for(int x=0; x <= FLOOR_LEN; x++)
        if (obs[z][x]) {
          if (obsBounds[z][x].intersect(bs))
            return true;
        }
     return false;
   }  // end of nearObstacle()



  private TransformGroup makeObs(int x, int z)
  // a red cylinder whose base is on (x,z)
 {
    // create obstacle node
   Appearance obsApp = new Appearance();
    Material mat = new Material(black, black, red, specular, 100.f);
    mat.setLightingEnable(true);
    obsApp.setMaterial( mat );
    Cylinder obs = new Cylinder( RADIUS, HEIGHT,
                  Cylinder.GENERATE_NORMALS, obsApp);

    // fix obs's position
   TransformGroup posnTG = new TransformGroup();
    Transform3D trans = new Transform3D();
    trans.setTranslation( new Vector3d(x, HEIGHT/2, z) );
    posnTG.setTransform(trans);
    posnTG.addChild(obs);
    return posnTG;
  }  // end of makeObs()
 
  private TransformGroup makeObs2(int x, int z)
  // a red cylinder whose base is on (x,z)
 {
    // create obstacle node
   Appearance obsApp = new Appearance();
    Material mat = new Material(black, black, red, specular, 100.f);
    mat.setLightingEnable(true);
    obsApp.setMaterial( mat );
   
    Box obs = new Box((float)0.2, (float)1.0,(float)1.0,obsApp);

    // fix obs's position
   TransformGroup posnTG = new TransformGroup();
    Transform3D trans = new Transform3D();
    trans.setTranslation( new Vector3d(x, HEIGHT/2, z) );
    posnTG.setTransform(trans);
    posnTG.addChild(obs);
    return posnTG;
  }  // end of makeObs()


  public Group getObsGroup()
  {  return obsGroup;  }



}  // end of Obstacles class
Offline Andrew Davison

Junior Member


Medals: 2



« Reply #1 - Posted 2007-04-23 02:24:19 »

Dear jiyanliang,

The best way to get help from a forum is to post a specific question, focussed on a single problem which you've already had a good look at yourself.

Saying that something "doesn't work", and posting the entire class, is less likely to get a good response.

For example, you should at least tell people what the error message is, whether the error occurs at run time or compile time, and narrow down the problem to a particular method.

I suggest you have a look at "How To Ask Questions The Smart Way" by Eric Raymond at http://www.catb.org/~esr/faqs/smart-questions.html.

For questions about my code, all of the above applies, and it's also a good idea to send questions to me directly at ad@fivedots.coe.psu.ac.th. I don't "prowl" these forums everyday.

- Andrew

Dr. Andrew Davison
Dept. of Computer Engineering
Prince of Songkla University, Hat Yai
Songkhla 90112, Thailand
Offline jiyanliang

Junior Newbie





« Reply #2 - Posted 2007-04-23 04:05:54 »

Dear Andrew Davison:
    I am sorry i didn't describe the question detailly;
    Thanks for your detail reply.
    I want to know  is there are any differents between BoundingBox and BoundingSphere in Java 3D api.Now I want to build obstacles whitch is box like (1.0,10.0,2.0),but the BoundingSphere is a sphere.
    In tour3D, you build the bounds with BoundingSphere.and now i want to build the Bounds with BoundingBox.
   I know both BoundingSphere and BoundingBox have intersect()  method.But this method doesn't take effect after i use BoundingBox instead of BoundingSphere.the TourSprite can go througt of the obstacles.
   I am the biginner of Java 3D programming.

  Thank you again!!

sunnyan
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Andrew Davison

Junior Member


Medals: 2



« Reply #3 - Posted 2007-04-23 05:19:45 »


You need to read the documentation for the two Bounding classes, and then you'll find out for yourself what the differences are between them.

"the method doesn't take effect" is not enough information for me to help you.

1. Try debugging the code yourself first.

2. If you still have problems, send me an e-mail *DIRECTLY*. The e-mail should contain debugging information.






Dr. Andrew Davison
Dept. of Computer Engineering
Prince of Songkla University, Hat Yai
Songkhla 90112, Thailand
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (10 views)
2014-09-21 23:33:17

BurntPizza (28 views)
2014-09-21 02:42:18

BurntPizza (18 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (29 views)
2014-09-19 03:14:18

Dwinin (46 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!