Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Shifting from Java 2D to Java3D Physics  (Read 1071 times)
0 Members and 1 Guest are viewing this topic.
Offline jibbylala

Senior Newbie





« Posted 2011-05-18 22:17:57 »

Hi Experts,


I am new to  game physics and really bad in physics and mathematical calculations, i am trying to adapt a simple 2d balls Simulation code for the 3d simulation with java3D library, but i have this problem:

Two things i noted down the values generated by engine,

for
X/Y are too high and minX / minY/ maxY /maxX values are causing troubles and i m unable to select/define/set the default correct/suitable values considering the 3D graphics scaling/resolution while they are set with respect to 2d screen coordinates,


 that is my only problem.


please help,

this is code :

  
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  
 package Library;
   
    import java.awt.*;
   
    public class Ball extends GameObject {
   
       private float x, y; // Ball's center (x, y)
      private float speedX, speedY; // Ball's speed per step in x and y
      private float radius; // Ball's radius
      private Color color; // Ball's color
      // Collision detected by collision detection and response algorithm?
      boolean collisionDetected = false;
       public static boolean run = false;

   // If collision detected, the next state of the ball.
  // Otherwise, meaningless.
  private float nextX, nextY;
   private float nextSpeedX, nextSpeedY;
   
   private static final float BOX_WIDTH = 640;
   private static final float BOX_HEIGHT = 480;

   /**
    * Constructor The velocity is specified in polar coordinates of speed and
    * moveAngle (for user friendliness), in Graphics coordinates with an
    * inverted y-axis.
    */

   public Ball(String name1,float x, float y, float radius, float speed,
         float angleInDegree, Color color) {
      this.x = x;
      this.y = y;
      // Convert velocity from polar to rectangular x and y.
     this.speedX = speed * (float) Math.cos(Math.toRadians(angleInDegree));
      this.speedY = speed * (float) Math.sin(Math.toRadians(angleInDegree));
      this.radius = radius;
      this.color = color1;
   }

   public void move()  {
      if (collisionDetected) {
         // Collision detected, use the values computed.
        x = nextX;
         y = nextY;
         speedX = nextSpeedX;
         speedY = nextSpeedY;
      } else {
         // No collision, move one step and no change in speed.
        x += speedX;
         y += speedY;
      }
      collisionDetected = false; // Clear the flag for the next step
  }

   public void collideWith()  {
      // Get the ball's bounds, offset by the radius of the ball
     float minX = 0.0f + radius;
      float minY = 0.0f + radius;
      float maxX = 0.0f + BOX_WIDTH - 1.0f - radius;
      float maxY = 0.0f + BOX_HEIGHT - 1.0f - radius;

      double gravAmount = 0.9811111f;
      double gravDir = (90 / 57.2960285258);

      // Try moving one full step
     nextX = x + speedX;
      nextY = y + speedY;
      System.out.println("In serializedBall in collision.");

      // If collision detected. Reflect on the x or/and y axis
     // and place the ball at the point of impact.
     if (speedX != 0) {
         if (nextX > maxX) { // Check maximum-X bound
           collisionDetected = true;
            nextSpeedX = -speedX; // Reflect
           nextSpeedY = speedY; // Same
           nextX = maxX;
            nextY = (maxX - x) * speedY / speedX + y; // speedX non-zero
        } else if (nextX < minX) { // Check minimum-X bound
           collisionDetected = true;
            nextSpeedX = -speedX; // Reflect
           nextSpeedY = speedY; // Same
           nextX = minX;
            nextY = (minX - x) * speedY / speedX + y; // speedX non-zero
        }
      }
      // In case the ball runs over both the borders.
     if (speedY != 0) {
         if (nextY > maxY) { // Check maximum-Y bound
           collisionDetected = true;
            nextSpeedX = speedX; // Same
           nextSpeedY = -speedY; // Reflect
           nextY = maxY;
            nextX = (maxY - y) * speedX / speedY + x; // speedY non-zero
        } else if (nextY < minY) { // Check minimum-Y bound
           collisionDetected = true;
            nextSpeedX = speedX; // Same
           nextSpeedY = -speedY; // Reflect
           nextY = minY;
            nextX = (minY - y) * speedX / speedY + x; // speedY non-zero
        }
      }

      speedX += Math.cos(gravDir) * gravAmount;
      speedY += Math.sin(gravDir) * gravAmount;
   }

        public float getSpeed()  {
      return (float) Math.sqrt(speedX * speedX + speedY * speedY);
   }

   public float getMoveAngle()  {
      return (float) Math.toDegrees(Math.atan2(speedY, speedX));
   }

   public float getRadius()  {
      return radius;
   }
   
   public float getX() {
      return x;
   }

   public float getY() {
      return y;
   }
   
   public void setX(float f) {
      x =  f;
   }

   public void setY(float f) {
      y =  f;
   }
    }

 here how i m drawing the balls:

  
1  
2  
3  
4  
5  
6  
 for (int i = 0; i < currentNumBalls; i++) {
             printTG(objTrans[i], "SteerTG");
             trans[i]
                   .setTranslation(new Vector3f(ball[i].getX(), ball[i].getY(), 0.0f));
             objTrans[i].setTransform(trans[i]);
          }
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2011-05-20 00:21:31 »

What do you mean "are too high" and "are causing troubles?"

Perhaps your viewport is not set to correctly match your data?

See my work:
OTC Software
Offline jibbylala

Senior Newbie





« Reply #2 - Posted 2011-05-20 00:48:03 »

many thanks for your reply,

as far as my poor knowledge about Java3d, the values should be 1 and 20 or less for Xs and Ys, but the values are like 300 and 400 and above than these...
and trouble means

some time the balls are drawing but not moving

some time going out of the panel

some time moving on little area

some time just stick at one place...

 these all happening on my hit and tries to decrease the values but  still i m not getting the normal results.


for me these values are causing all problems as these are set with respect to 2D , but i m unable to fix these:

 
1  
2  
3  
4  
5  
6  
7  
8  
        private static final float BOX_WIDTH = 640;
        private static final float BOX_HEIGHT = 480;

  // Get the ball's bounds, offset by the radius of the ball
      float minX = 0.0f + radius;
       float minY = 0.0f + radius;
       float maxX = 0.0f + BOX_WIDTH - 1.0f - radius;
       float maxY = 0.0f + BOX_HEIGHT - 1.0f - radius;
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #3 - Posted 2011-05-20 17:08:12 »

Why are you unable to fix those? Why not just, you know, type in a smaller number? Or make your viewport larger?

The balls sometimes not moving etc. sounds just like code logic errors that have nothing to do with 3D.

Sounds to me like you need to understand frustum better and how to make it match your game area.

See my work:
OTC Software
Offline jibbylala

Senior Newbie





« Reply #4 - Posted 2011-05-20 20:55:47 »

No,it's not the code is working perfect with in java 2D.
Offline avm1979
« Reply #5 - Posted 2011-05-21 01:37:55 »

Perhaps slightly off-topic, but if you're just moving into 3d graphics, OpenGL (via either LWJGL or JOGL) might be a better place to start. Java3D isn't, to put it gently, very good. And you'll find loads more examples and tutorials for OpenGL.

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.

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

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

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

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

Tekkerue (47 views)
2014-09-09 02:24:56

mitcheeb (68 views)
2014-09-08 06:06:29

BurntPizza (51 views)
2014-09-07 01:13:42

Longarmx (38 views)
2014-09-07 01:12:14

Longarmx (44 views)
2014-09-07 01:11:22

Longarmx (40 views)
2014-09-07 01:10:19
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!