Shifting from Java 2D to Java3D Physics

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:

[i]Two things i noted down the values generated by engine,

for[/i] 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 :

 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:

 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]);
    		}


What do you mean “are too high” and “are causing troubles?”

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

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:


        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;

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.

No,it’s not the code is working perfect with in java 2D.

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.