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