I reading about Mathematics in Games and am wondering what is the best way to represent a Vector location in Java.
I know there is a Vector class but I don't think this is what I need.
There is also a Matrix class which looks like it may be what I want (a 1 dimensional matrix maybe).
In particular, if I were to create a location Vector such as:
v(x,y,z) where x,y and z are the coordinates in 3D space, what would be the best way to represent this in Java. It would be nice if I could also add, subtract and find the dot-product of Vectors.
ideas?
maybe you could create a 3DVector class
example:
class 3DVector {
int x , y, z;
public 3DVector(int x, int y, int z){
//constructor
}
public 3DVector add(3DVector anotherVector){
}
public 3DVector subtract()....
public 3DVector doProduct().....
}
There is a library for this, See :
javax.vecmath.Vector3d
There is a math library out there but a simple class with 3 floating point numbers is what everyone else uses
Related
I am trying to generate a list of a hundred thousand random points in 3d space within a 3d boundary without having any of the points occupy the same position. I'm literally trying to create a non-repeating Vector3 generator. Is there any efficient way of doing this? Also, it is okay if these points are not evenly distributed, it is actually preferable if they are someone clustered here there, just as long as they do not occupy the same position.
To clarify I am not trying to generate 300,000 unique points. But instead 100,000 3d points. So a vector values of (0, 0, 0) and (0, 0, 1) is acceptable.
But (4, 4, 4) and (4, 4, 4) is unacceptable.
public class Vector3
{
public float x;
public float y;
public float z;
Vector3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
public static ArrayList<Vector3> generateVector3s()
{
ArrayList<Vector3> tempVector3List = new ArrayList<>();
for (int i = 0; i < 100000; i++)
{
tempVector3List.add(new Vector3(RANDOMVALUE, RANDOMVALUE, RANDOMVALUE));
}
return tempVector3List ;
}
}
First, generation of Random Numbers in Java:
Geeks for Geeks on Random Numbers in Java
What I might do is generate a value then use the triplet as a key for a hashset. If it exists, the value is already there, and toss it and try again. This will ensure uniqueness and should be relatively efficient.
If you want your datapoints spread equally about your problem space, you might need a fancier algorithm.
It sounds like you are looking for a 3D version of Poisson disc sampling, which guarantees points will not cluster in the same areas.
The algorithm is fairly complicated so I won't describe it here fully, but the basic idea is that you choose a seed point and grow outwards by always adding points that are at least a given distance away from the existing points.
You can find detailed descriptions and implementations here:
https://bost.ocks.org/mike/algorithms/
http://gregschlom.com/devlog/2014/06/29/Poisson-disc-sampling-Unity.html
https://github.com/kchapelier/poisson-disk-sampling
https://youtu.be/7WcmyxyFO7o
https://youtu.be/flQgnCUxHlw
I need to get the horizontal and vertical movements of a vehicle using the speed, time and angle input. In my target class I have:
public double toRadians = Math.toRadians(angle);
public double getHorizontal() {
return (speed*time*Math.cos(toRadians));
}
public double getVertical() {
return (speed*time*Math.cos(toRadians));
}
however, the values of horizontal and vertical movement give me just the speed*time, so I assume there must be something wrong in the way I declared the math function?
There is something not right about your code.
Hint: what is the difference between these two functions apart from their respective names?
public double getHorizontal() {
return (speed*time*Math.cos(toRadians));
}
public double getVertical() {
return (speed*time*Math.cos(toRadians));
}
Hint 2: Suppose you plot a graph with f(t) on the X axis against f(t) on the Y access, and vary the values of t. what will that graph look like?
I assume there must be something wrong in the way I declared the math function?
The declarations are not the problem. It is the function bodies that the problem.
(Though actually, it is a bit of the problem that, speed, time and radians (or angle) are instance variables rather than arguments to the functions / methods. But deal with that problem after the problem with .... the formulae.)
Apologies if the title is not appropriate, was having trouble what to call this.
Scenario:
I have a universe type project in java where there are different types of things you can find in a universe (stars, planets, comets etc).
This is part of my university coursework and I'm stuck on one part
I have a class called Space_Object which is a superclass and all things found in the universe inherit it. The superclass has variables such as xPosition, yPosition.
I am currently stuck on trying to get planets to orbit around stars. I am trying to get the x,y coordinates of a star so that the planet can orbit around it (there can be multiple planets and stars). Right now I am passing the star that the planet will orbit around as a field whenever making a new planet.
I created getters inside of Planet to retrieve the x,y of the Star (which works). I am stuck on how can I use that x and y to alter the starting point of the planet. This is what I added to Universe class:
public void setCoordsOfPlanet(Planet planetObj)
{
planetObj.xPosition = planetObj.getSolarSystemX();
}
Which gave me an error of:
xPosition has private access in Space_Object
I am not allowed to make any of the fields public.
Planet class:
public class Planet extends Space_Object
{
private int distanceFromStar;
private int orbitSpeed;
static Star solarSystem;
public Planet(int disFromStar, int orbSpeed, Star solSystem, int objectDiameter, Color objectColor, Universe theUniverse)
{
super(0, 0, 0, 0, objectDiameter, objectColor, theUniverse);
distanceFromStar = disFromStar;
orbitSpeed = orbSpeed;
solarSystem = solSystem;
}
public int getSolarSystemX ()
{
return solarSystem.getXPosition();
}
public int getSolarSystemY ()
{
return solarSystem.getYPosition();
}
}
Just in case, the Space_Object constructor:
public Space_Object(int xPos, int yPos, int xVel, int yVel, int objectDiameter, Color objectColor, Universe theUniverse)
{
xPosition = xPos;
yPosition = yPos;
xSpeed = xVel;
ySpeed = yVel;
color = objectColor;
diameter = objectDiameter;
universe = theUniverse;
universeHeight = universe.getUniverseHeight();
universeWidth = universe.getUniverseWidth();
lifeTime = 1000000;
}
Am I approaching this from the completely wrong angle? I been trying to change things regarding this matter for past three hours and made no progress - any help is appreciated. If you need more code let me know.
PS: All items in the universe are objects and are represented as colour circles on a canvas.
If you are asking how do I modify private fields from another class: then all you need to do is to add setter methods in your Space_Object or Planet class, for example:
public class Planet {
...
public setCoor(int x, int y) {
this.xPosition = x;
this.yPosition = y;
}
}
Now you can call this method from the Star class: planet.setCoor(x, y)
If you want this method to only be accessible from classes of the same package only, remove public.
There are multiple issues here.
Programming stuff
Model of planetary rotation
Use of 'Solar System' when it should be 'Star System' :-) The Solar System is our star system; that's because our star is "Sol"
A1. You need xposition to have a method to set it.
A2a. There are no x and y for our solar system or even a star system. If you're going to model spinning galaxies and/or expanding universe (in which case the galaxies also move in 3d space away from each other,) then the star positions (or positions of any object for that matter) are not fixed.
A2b. If you're going with immobile stars and galaxies, a star (not its system) will have an x and a y.
A2c. A planet revolves around its star in a Kepler orbit with eccentricity greater than 0 and less than 1. To calculate the path, you need axis information for the orbit in addition to the star's location. Wikipedia will have the equations.
A2d. There is no starting position of a planet unless you plan to have planets with unstable orbits. (Or comets which will have their orbits modified during every revolution by the planets they pass by). Planets with stable orbits have always followed and will forever follow the same path (not really, but...) You can place the planet at any point on the orbit and give it appropriate initial velocity (=speed+direction) and watch it go. A3. Self-explanatory
Sounds like a fun project, especially you're animating the model onscreen. In such a case, you also need to decide on your system's clock-speed; the numbers of days that will pass in real time for each second of your simulation time. Additionally, you'll need to select your refresh frequency; how often will you update the screen.
Okay, so i created an an int array like so int position = new int[160][250][160] to represent a possition in a 3D space ([x][y][z]) what i want to do is draw a line from one point to another, but am confused about how to get a number from that array?
if i want to draw a line from point [78][89][30] to [34][75][25] how would i get a specific number from just one of those arrays? or would it be best just to create 3 seperate arrays? (keeping voxels in mind)?
Thanks - Shamus
I would suggest to have a class 3DPoint
class Point3D{
int x;
int y;
int z;
}
class Line{
Point3D pointFrom;
Point3D pointTo;
}
This is how you do things easily in Object Oriented World
We're working on a library that does calculations in 2D space. However, the 'natural' interpretation of the 2D coordinate system is where increasing Y values represent points that lie higher, while the awt coordinates do the reverse. This reflects in Rectangle(10,100).maxY() returning 0, while the mathematician would expect it to return 100.
How can we properly deal with that difference? Is there another java library to do geometrical calculations?
You should try things before asking. The following code prints 100, so there is no problem :)
import java.awt.*;
public class A {
public static void main(String[] args) {
double maxX = new Rectangle(10, 100).getMaxY();
System.out.println("A::main: maxX = " + maxX);
}
}
I turns out that maxY returns 'the biggest y-coordinate`, which is exactly the intuitive behavior. I was confused: only when actually drawing it to screen the objects have a 'top-left' and a 'bottom-right' are merely conventions when visualizing it.