Trying to create a randomized "solar system" - java

Here is the Planet class:
public class Planet extends CelestialBody {
private static Random r = new Random();
private static Star star;
public Planet(Star star, int orbitRadius, int x, int y){
name = "PLACEHOLDER";
radius = Tools.intBetween(Settings.MAX_PLANET_SIZE, Settings.MIN_PLANET_SIZE);
color = Tools.rObjFromArray(Settings.PLANET_COLORS);
this.star = star;
this.orbitRadius = orbitRadius;
this.x = x; this.y = y;
}
public static Planet createNewPlanet(Star star, int orbitRadius){
int px = (int) (star.x + orbitRadius * Math.cos(Math.toRadians(r.nextInt(360))));
int py = (int) (star.y + orbitRadius * Math.sin(Math.toRadians(r.nextInt(360))));
return new Planet(star, orbitRadius, px, py);
}
public void render(Graphics g){
//Draw orbit
g.setColor(Color.white);
g.drawOval(star.x - orbitRadius, star.y - orbitRadius, orbitRadius * 2, orbitRadius * 2);
//Draw planet
g.setColor(color);
g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
}
}
orbitRadius = distance from planet to star (random);
radius = planet's radius (random)
Ask in the comments if you need any more code, and I know this is a noob question, but I just can't figure out why the orbits dont line up with the planets. Thanks.

The problem is in the following two lines:
int px = (int) (star.x + orbitRadius * Math.cos(Math.toRadians(r.nextInt(360))));
int py = (int) (star.y + orbitRadius * Math.sin(Math.toRadians(r.nextInt(360))));
Because you call r.nextInt(360) two separate times, you get a different random number each time.
The consequence is that the x and y coordinates are for different angles, I think it's clear why this would be a problem.
The solution is very simple: call r.nextInt once and save the result:
double randomAngle = Math.toRadians(r.nextInt(360));
int px = (int) (star.x + orbitRadius * Math.cos(randomAngle));
int py = (int) (star.y + orbitRadius * Math.sin(randomAngle));
I think this should solve the problem.

Related

How to place the ball position properly in Wheel game?

I have a static number of slots for the number in the wheel game. Start from number 00 to number 1 (clockwise). The position count is 0 to 37 in my Collection array. Currently, I successful place the ball position in the middle of the number with 1 degree. But I unable to calculate a correct value for each of the position when I tried to pass in the number position.
I already tried messing with the calculation, it either the ball sit in the middle between the number or out of range!
public class WheelDisplay extends JPanel implements ConstantVariable {
private Image image;
private ImageObserver imageObserver;
private float degrees = 1;
private int post = 0;
public WheelDisplay() {
ImageIcon icon = new ImageIcon(IMAGE_LOCATION);
image = icon.getImage();
imageObserver = icon.getImageObserver();
}
/** Credit to stackoverflow forum : https://stackoverflow.com/questions/25923480/simple-circle-rotation-simulate-motion **/
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int circleDiameter = Math.min(getWidth(), getHeight());
double circleRadius = circleDiameter / 2;
int offSetX = (getWidth() - circleDiameter) / 2;
int offSetY = (getHeight() - circleDiameter) / 2;
g2d.drawImage(image, offSetX, offSetY, circleDiameter, circleDiameter, imageObserver);
g2d.setColor(Color.BLUE);
int ballDiameter = (int) (circleDiameter * 0.02);
int ballRadius = ballDiameter / 2;
Point p = getPointOnCircle(this.degrees, circleRadius * 0.9, circleRadius);
int valueX = offSetX + p.x - ballRadius;
int valueY = offSetY + p.y - ballRadius;
g2d.fillOval(valueX, valueY, ballDiameter, ballDiameter);
g2d.dispose();
}
private Point getPointOnCircle(float degress, double circleRadius, double innerCircleRadius) {
//The calculation that allow ball sit in the middle of each number when spin
double rads = ((Math.PI * degress) / 38);
// Calculate the outter point of the line
int xCordinate = Math.round((float) (innerCircleRadius + Math.cos(rads) * circleRadius));
int yCordinate = Math.round((float) (innerCircleRadius + Math.sin(rads) * circleRadius));
return new Point(xCordinate, yCordinate);
}
public void setDegree(int x) {
this.degrees += 2;
this.post = x; // The number of position coming from Collection (0-37)
}
}
The ball supposes to sit in the middle of each 38 numbers in the wheel image.
Hope that someone can point out which part I'm doing wrong!
The image that I'm using is this.

Drawing paths from a point where each path is distanced from the initial point

I am having a bit of trouble trying to figure out how to draw paths from a point on a canvas with the start of each path being equally distanced from the initial point. To illustrate what I mean, the code that I have so far is able to generate this:
and the desired result is something like this:
My code:
int n = 3;
int r;
double x;
double y;
point1 = new Point(mWidth/2, mHeight/2);
double angle;
double angleFactor;
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < 3; i++){
angleFactor = 2 * Math.PI / n;
angle = i * angleFactor;
x = (point1.x) + r * Math.cos(angle);
y = (point1.y) + r * Math.sin(angle);
//Draw paths
path.reset();
path.moveTo(point1.x, point1.y);
path.lineTo((float) x, (float) y);
canvas.drawPath(path, paint);
}
}
Is there a simple solution to this?
Since you want to have a tiny distance between the offset of a line and the center point, you can define start coordinates like this:
double xStart, xEnd;
double yStart, yEnd;
double offsetFraction = 0.1;
Inside the for loop in onDraw() :
double lengthX = r * Math.cos(angle);
double lengthY = r * Math.sin(angle);
xStart = (point1.x) + offsetFraction * lengthX;
yStart = (point1.y) + offsetFraction * lengthY;
xEnd = (point1.x) + lengthX;
yEnd = (point1.y) + lengthY;
//Draw paths
path.reset();
path.moveTo((float) xStart, (float) yStart);
path.lineTo((float) xEnd, (float) yEnd);
canvas.drawPath(path, paint);

How can I make my shape "point" at my player

I am trying to make a game where enemies spawn from the top (like a vertical scrolling game) and one kind of enemy will basically follow the player's X coordinate while going down. The enemy class is called Follower and right now it does points to the player (see update function on Follower) but it's not as accurate as I need it to be. For example, if the player accelerates, the follower won't be able to see him all the time.
One way to look at it is that I want the position of the player to be a coordinate in a radiant system and make the vertices of my Follower accurately just rotate and create a straight line looking at it every frame
here is the Follower Class:
public class Follower {
Player target; //follow this
//position
private Vector2 position;
private float x;
private float y;
//speed
private Vector2 velocity;
private float speed;
private float radians;
private float faceTarget;
//dimensions
private float[] shapeX;
private float[] shapeY;
private int numPoints; //vertices for the shape
private boolean remove; //to remove from the game
public Follower(float x,float y, Player target){
this.target = target;
this.x = x;
this.y = y;
velocity = new Vector2(0, 0);
numPoints = 4;
speed = 200;
shapeX = new float[numPoints];
shapeY = new float[numPoints];
radians = 3.1415f / 2;
setShape();
}
public void setShape(){
//top vertice
shapeX[0] = x + MathUtils.cos(radians) * 30;
shapeY[0] = y + MathUtils.sin(radians) * 30;
//left vertice
shapeX[1] = x + MathUtils.cos(radians - 4 * 3.1415f / 10) * 30;
shapeY[1] = y + MathUtils.sin(radians - 4 * 3.1415f / 10) * 30;
//bottom vertice
shapeX[2] = x + MathUtils.cos(radians + 3.1415f) * 60;
shapeY[2] = y + MathUtils.sin(radians + 3.1415f) * 60;
//left vertice
shapeX[3] = x + MathUtils.cos(radians + 4 * 3.1415f / 10) * 30;
shapeY[3] = y + MathUtils.sin(radians + 4 * 3.1415f / 10) * 30;
}
public boolean shouldRemove() {
return remove;
}
public void update(float dt) {
float angle = (float) Math.atan2(target.getPosition().y - y, target.getPosition().x - x); //angle between the follower and target
velocity.set((float) Math.cos(angle) * speed , -speed); //setting direction to follow the target
radians += Math.cos(angle) * dt; //THIS HERE IS MAKING IT ROTATE
x += velocity.x * dt;
y += velocity.y * dt;
setShape();
if(y <= 0 - 60)
remove = true;
else
remove = false;
}
public void draw(ShapeRenderer sp){
sp.setColor(1, 1, 1 ,1);
sp.begin(ShapeRenderer.ShapeType.Line);
for(int i = 0, j = shapeX.length - 1;
i < shapeX.length;
j = i++) {
sp.line(shapeX[i], shapeY[i], shapeX[j], shapeY[j]);
}
sp.end();
}
}
I am not adding the GameScreen because I do not see the need of showing how they are rendered, either way, it'll stay the same.
Also, with the line of code, I am using the Follower points to the player with the bottom vertice as the "eyes"
Thanks for the answers!

Java how to make a circle slowly move around?

Hello I have an app where there are circles floating around. At the moment the don't float around, which is the problem. I want them to slowly move around in random directions. How can I do this?
Here is my circle class:
public class data {
public int x,y, size,id;
public data(int x,int y){
this.x = x;
this.y = y;
size = new Random().nextInt(50);
id = new Random().nextInt(10);
}
public void tick(){
}
public void render(Graphics g){
g.setColor(new Color(38,127,0));
g.fillOval(x, y, size, size);
g.setColor(Color.black);
g.drawOval(x, y, size, size);
}
}
You can have a very random movement by adding a random value to x and to y every tick:
private Random random = new Random();
public void tick() {
x = x + random.nextFloat();
y = y + random.nextFloat();
}
This will result in a very fuzzy motion.
Another option is to have 2 variables: motionX and motionY. Those get added to x and y every tick, after which you add random values to motionX and motionY:
private Random random = new Random();
private float xMotion = 0f, yMotion = 0f;
private float factor = 0.5f; //just a value to reduce speed
private void tick() {
x = x + xMotion;
y = y + yMotion;
xMotion = xMotion + random.nextFloat() * factor;
yMotion = yMotion + random.nextFloat() * factor;
}
First of all, I think you messed up which one you should random, you random selected a size and the id(what is this for anyways?), not the x,y value. Also, I don't know why, but it seems that the render program must be called paint.

Why won't my Quaternion rotate properly?

Ok, we are not talking about OpenGL with this question, but this will be used with OpenGL ES 2.0.
Question: How do create and rotate a Quaternion with the following code?
I have been reading up and studying about this and still can't quite gasp the concepts. I thought I understood it, but once I started making some calculations to rotate the quaternion I realized I can't even get back to where I started.
So let us say that we have a cube, and the center of it is at (0, 0, 0). We want to rotate it on the x-axis by 45 degrees. What would I do? (Only the Quaternion)
Assuming success, how would you get the amount of rotation from 'W'? I know that '1' indicates that there is no rotation, but what if it was rotated 173 degrees?
Trying to rotate to a given direction, 45 degrees, and then get that value from W. I feel like I need to convert the angle to rads or something, but not exactly sure. Tutorials online vary from one to the next.
Here is my code:
import java.util.Scanner;
import Quaternion;
public class Main {
public static void main(String[] args) {
Quaternion q1 = new Quaternion(0, 0, 0, 1);
Quaternion q2 = new Quaternion(0, 0, 0, (float) Math.cos(toRAD(45.0f) / 2));
q1 = q2.mul(q1);
System.out.println("q1: " + q1.getX() + ", " + q1.getY() + ", " + q1.getZ() + " with " + toANGLE(2.0f * Math.acos(q1.getW())));
}
private static double toRAD(float angle) {
return angle * (Math.PI / 180.0f);
}
private static float toANGLE(double rad) {
return (float) (rad * (180.0f / Math.PI));
}
}
Here is the code for a Quaternion:
public class Quaternion // Credit goes to 'thebennybox' (http://www.youtube.com/user/thebennybox)
{
private float x;
private float y;
private float z;
private float w;
public Quaternion(float x, float y, float z, float w)
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public float length()
{
return (float)Math.sqrt(x * x + y * y + z * z + w * w);
}
public Quaternion normalize()
{
float length = length();
return new Quaternion(x / length, y / length, z / length, w / length);
}
public Quaternion conjugate()
{
return new Quaternion(-x, -y, -z, w);
}
public Quaternion mul(Quaternion r)
{
float w_ = w * r.getW() - x * r.getX() - y * r.getY() - z * r.getZ();
float x_ = x * r.getW() + w * r.getX() + y * r.getZ() - z * r.getY();
float y_ = y * r.getW() + w * r.getY() + z * r.getX() - x * r.getZ();
float z_ = z * r.getW() + w * r.getZ() + x * r.getY() - y * r.getX();
return new Quaternion(x_, y_, z_, w_);
}
public Quaternion mul(Vector3f r)
{
float w_ = -x * r.getX() - y * r.getY() - z * r.getZ();
float x_ = w * r.getX() + y * r.getZ() - z * r.getY();
float y_ = w * r.getY() + z * r.getX() - x * r.getZ();
float z_ = w * r.getZ() + x * r.getY() - y * r.getX();
return new Quaternion(x_, y_, z_, w_);
}
public float getX()
{
return x;
}
public void setX(float x)
{
this.x = x;
}
public float getY()
{
return y;
}
public void setY(float y)
{
this.y = y;
}
public float getZ()
{
return z;
}
public void setZ(float z)
{
this.z = z;
}
public float getW()
{
return w;
}
public void setW(float w)
{
this.w = w;
}
}
I'm still not 100% sure what your question is asking, but I'll give it a shot.
Problem: Given a quaternion representing a 0 degree rotation about x, y, z, generate a new quaternion representing a 45 degree rotation about the x axis
Start with a quaternion representing no rotation, call it q1
q1 = (w1, x1, y1, z1)
q1.w1 = cos(0/2) = 1
q1.x1 = 0 * sin(0/2) = 0
q1.y1 = 0 * sin(0/2) = 0
q1.z1 = 0 * sin(0/2) = 0
So q1 = (1, 0, 0, 0)
Generate a new rotation that is 45 degrees (PI/4 radians) about the X axis
We need a temporary quaternion to modify q1. Let's call it q2.
q2 = (w2, x2, y2, z2)
q2.w2 = cos(PI/4 / 2) = cos(PI/8)
q2.x2 = 1.0 * sin(PI/4 / 2) = 1.0 * sin(PI/8) = sin(PI/8)
q2.y2 = 0.0 * sin(PI/4 / 2) = 0.0
q2.z2 = 0.0 * sin(PI/4 / 2) = 0.0
so q2 = (cos(PI/8), sin(PI/8), 0, 0)
Now this last step is important, you modify your original quaternion by a left-hand multiplication of the temporary quaternion
What I mean is this:
q1 = q2 * q1
Your multiplication function is written correctly, so the problem is not there. Remember that quaternion multiplications are not commutative. That is q2 * q1 is NOT the same as q1*q2!
At this point q1 is modified to represent a 45 degree rotation about the X axis.
To print out the angle in degrees, you need to compute 2.0 * acos(q1.w) / PI * 180
Your code is incorrectly computing q1.w/PI * 180 to get the angle in degrees.
More specifically, change
toANGLE(resQuat.getW())
to
toANGLE(2.0f * Math.acos(resQuat.getW()))
I haven't looked at your code beyond that, but try applying these concepts and see if that fixes your problem.

Categories

Resources