Classes in ArrayList updates each other properties - java

I am creating small firework simulation in LibGDX. I have ArrayList called particles and this is filling it:
for (int i = 0; i < 2; i++) {
Particle p = new Particle();
p.position = position;
p.velocity.x = MathUtils.random(-1f, 1f);
p.velocity.y = MathUtils.random(-1f, 1f);
particles.add(p);
}
And then in update loop:
for (int i = 0; i < particles.size(); i++) {
System.out.println(i + " " + particles.get(i).position.toString() + " + " + particles.get(i).velocity.toString() + " = ");
particles.get(i).update();
System.out.println(" " + particles.get(i).position.toString());
}
Particle update function:
velocity.add(acceleration);
position.add(velocity);
acceleration.set(0, 0);
Velocity is random and every particle have unique velocity but position is the same. Here is output:
0 (300.0,620.91364) + (-0.94489133,-0.45628428) =
(299.0551,620.45734)
1 (299.0551,620.45734) + (0.3956585,0.5208683) =
(299.45078,620.9782)
0 (299.45078,620.9782) + (-0.94489133,-0.45628428) =
(298.5059,620.5219)
1 (298.5059,620.5219) + (0.3956585,0.5208683) =
(298.90155,621.0428)
0 (298.90155,621.0428) + (-0.94489133,-0.45628428) =
(297.95667,620.5865)
1 (297.95667,620.5865) + (0.3956585,0.5208683) =
(298.35233,621.10736)
First is particle index, position, velocity and then output position.
Why is it using position from another particle? I am trying to figure it out but I can't.

In your for loop where you fill the ArrayList you have the line:
p.position = position;
I don't know where position comes from but here all Particles point to the same.
You must create a new Position for every Particle
p.position = new Position(x, y);
If position is the start point for your Particles you can write:
p.position = new Position(position.x, position.y);

Related

How to build a graph of 500 points?

I am using the MPAndroidChart library. I need to create a straight line of ~ 500 points. The problem is that I do not see the graph itself and the points. But if I manually add 5 points and build a graph on them, then everything will be displayed correctly.
I suspect that it is necessary to somehow solve the problem of displaying a large number of points, since they overlap each other.
ArrayList<Entry> values = new ArrayList<>();
//default: x1 = 200; x2 = 700
for (int i = x1, index = 0, j = y; i < x2; ++i, ++index)
{
float waveLength = (float) calib_a * i + (float) calib_b;
int pixel = rotated.getPixel(i, j);
float I = (Color.red(pixel) + Color.blue(pixel) + Color.green(pixel)) / 765.0f;
values.add(new Entry(waveLength, I));
Log.d("[SPECTRAl]", " WAVE: " +waveLength + " I: " + I);
}
LineDataSet lineValues = new LineDataSet(values, "");
LineData line = new LineData(lineValues);
chart.setData(line);
chart.invalidate();
xml:
<com.github.mikephil.charting.charts.LineChart
android:id="#+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Trying to RayTrace in LWJGL, detecting an object

I am trying to write Ray Tracing in the LWJGL to go from the mouse position to the end of the display. I have written the code to find the mouse positions and to create the Vector3's for the start and end position of the rays, but how do I 'fire' the ray and detect an object?
I have tried using Camera's for the positioning, but my friend is the one who wrote the main Display system and its pretty gargled up and hard to read. Instead I opted for just firing the ray from the mouse position inside of the Display to the back Z of the display.
public class Picker {
static Vector3f startRay;
static Vector3f endRay;
public Picker() {
startRay = new Vector3f();
endRay = new Vector3f();
}
private static Vector2f getMouseCoords() {
float x = (2f* Mouse.getX()) / Display.getWidth() - 1;
float y = (2f * Mouse.getY()) / Display.getHeight() - 1f;
return new Vector2f(x, y);
}
public static String getRayStr() {
float x = (2f* Mouse.getX()) / Display.getWidth() - 1;
float y = (2f * Mouse.getY()) / Display.getHeight() - 1f;
String str = "Mouse X: " + x + " Mouse Y: " + y;
str += "\nStart X: " + startRay.x + " Start Y: " + startRay.y + " Start Z: " + startRay.z;
str += "\nEnd X: " + endRay.x + " End Y: " + endRay.y + " End Z: " + endRay.z;
return str;
}
public static void setStartRay() {
Vector2f mousePos = getMouseCoords();
Vector3f tempStartPos = new Vector3f();
tempStartPos.x = mousePos.x;
tempStartPos.y = mousePos.y;
tempStartPos.z = 0;
startRay = tempStartPos;
}
public static void setEndRay() {
Vector2f mousePos = getMouseCoords();
Vector3f tempEndPos = new Vector3f();
tempEndPos.x = mousePos.x;
tempEndPos.y = mousePos.y;
tempEndPos.z = -1;
endRay = tempEndPos;
}
}
I want to be able to detect an object directly inbetween startRay and endRay. The object I want to detect is a 3d model inside of the Display area.
Returning the startRay and endRay Vector3 positions has already confirmed to work, just needing to know about how to:
1) Cast the ray
2) Detect an object between the two points
Thank you very much for your assistance.

What does LibGDX's mesh.getVerticies output?

I am working on a 3D LibGDX project, and am trying to manipulate the vertices of a 3D model. I have the model loading, and I have this as my code:
Model plain = assets.get("plain.g3db", Model.class);
for(Mesh m : plain.meshes){
float[] t = m.getVertices(new float[m.getMaxVertices()]);
float[] newVerticies = new float[m.getMaxVertices()];
for(int i = 0; i < t.length-1; i++){
newVerticies[i] = t[i];
System.out.println("X: " + t[i] + " " + i);
newVerticies[i] = t[i];
System.out.println("Y: " + t[i++] + " " + i);
newVerticies[i] = random.nextInt(1-0) + 0;
System.out.println("Z: " + t[i++] + " " + i);
newVerticies[i] = t[i];
System.out.println("R: " + t[i++]);
newVerticies[i] = t[i];
System.out.println("G: " + t[i++]);
newVerticies[i] = t[i];
System.out.println("B: " + t[i++]);
newVerticies[i] = t[i];
System.out.println("A: " + t[i++]);
}
m.setVertices(newVerticies);
}
That does not work how I want it to, but I can at least see the model. If I comment out these lines:
newVerticies[i] = t[i];
System.out.println("R: " + t[i++]);
newVerticies[i] = t[i];
System.out.println("G: " + t[i++]);
newVerticies[i] = t[i];
System.out.println("B: " + t[i++]);
newVerticies[i] = t[i];
System.out.println("A: " + t[i++]);
I just get a black screen. Even if I move around, I see nothing. What I want to know, is what exactly float[] t = m.getVertices(new float[m.getMaxVertices()]); outputs. How does the output correspond to the model? How can I make the Y value random within a range?
Mesh data is organized into VertexAttributes. These always include position, but can also include color, texture coordinates, normals, etc. For example, a mesh with a texture might have the following:
A Position VertexAttribute of size 3 for X, Y, and Z (it's possible to have a 2D mesh where the position attribute has only 2)
A TextureCooridantes VertexAttribute of size 2 for U and V
A Normal VertexAttribute with size 3 for X, Y, and Z
So the float array you get with mesh.getVertices() would be this set of 8 floats for each vertex, one after the other.
If you loaded your Mesh from a model file rather than constructing it manually, you might not be sure of what VertexAttribute setup it has, so you need to inspect it to find out what the offset of the attribute you want is:
int positionOffset = mesh.getVertexAttributes().getOffset(Usage.Position);
int yOffset = positionOffset + 1; //skip X
int vertexSize = mesh.getVertexSize() / 4; //divide to convert bytes to floats
Now if you wanted to change Y, you would loop through the vertices something like this:
//this is how to get the properly sized mesh:
float[] vertices = new float[mesh.getNumVertices() * mesh.getVertexSize() / 4];
mesh.getVertices(vertices);
for (int i = yOffset; i < vertices.length; i += vertexSize){
vertices[i] += someValue;
}
mesh.setVertices(vertices);
The indices indicate groups of three vertices that make up the triangles of the mesh. If there are vertices on your mesh that are the same on multiple triangles, they might appear in the vertices list only once. This typically happens on soft-shaded vertices of a mesh, since the UV and normal will be the same for all the adjacent triangles touching that vertex.

circular gradient blur filter

I am doing a school assignment in which I need to create a circular gradient fog filter. I did some research on how to do blur an image and founf that I need to take the color value of the pixels around the current pixel and average them to set the new color for the blurred image. So far, I am just focusing on the blurring aspect, and I have this code:
public void circularGradientBlur()
{
Pixel regularPixel = null;
Pixel L_regularPixel = null;
Pixel R_regularPixel = null;
Pixel T_regularPixel = null;
Pixel B_regularPixel = null;
Pixel blurredPixel = null;
Pixel pixels[][] = this.getPixels2D();
for (int row = 2; row < pixels.length; row++)
{
for (int col = 2; col < pixels[0].length - 1; col++)
{
regularPixel = pixels[row][col];
if (row != 0 && col != 0 && row != 498 && col != 498)
{
L_regularPixel = pixels[row - 1][col];
R_regularPixel = pixels[row + 1][col];
T_regularPixel = pixels[row][col - 1];
B_regularPixel = pixels[row][col + 1];
blurredPixel.setRed((L_regularPixel.getRed() + R_regularPixel.getRed() + T_regularPixel.getRed() + B_regularPixel.getRed()) / 4);
blurredPixel.setGreen((L_regularPixel.getGreen() + R_regularPixel.getGreen() + T_regularPixel.getGreen() + B_regularPixel.getGreen()) / 4);
blurredPixel.setBlue((L_regularPixel.getBlue() + R_regularPixel.getBlue() + T_regularPixel.getBlue() + B_regularPixel.getBlue()) / 4);
}
}
}
}
When I try use this code, I get a NullPointerException on the lines where I set the new colors- blurredPixel.setRed(), blurredPixel.setGreen(), blurredPixel.setBlue(). This error is confusing me as I have other methods that have similar code and don't throw this error. I would appreciate any help that I can get!
You have to create an instance of the blurredPixel. it will always be null if you call the setters.

Check if an array of points is inside an array of rectangles?

I have a list of vertices and a list of regions (which are square/rectangle) shaped. Vertex has x and y coordinates, and a region has (x, y, height and width). How can I efficiently check which vertex lies in which region for every vertex/region?
EDIT:
This is the code I wrote to do this.
if (!g.getVertices().isEmpty()) {
for (int i = 0; i < g.getVertices().size(); i++) {
Vertex v = g.getVertices().get(i);
Point vertexPoint = new Point(v.getX(), v.getY());
for (int j = 0; j < g.getNumberOfRegions(); j++) {
int x = g.getRegions().get(j).getX();
int y = g.getRegions().get(j).getY();
int height = g.getRegions().get(j).getHeight();
int width = g.getRegions().get(j).getWidth();
Grid regionGrid = new Grid(j+1, x, y, height, width);
Rectangle regionRectangle = new Rectangle(x, y, height, width);
if (regionRectangle.contains(vertexPoint)) {
System.out.println("Vertex " + v + " lies inside region " + regionGrid.getRegionID());
}
}
}
}
EDIT 2: I used this to generate the regions, but I need a way to assign each region in the grid a regionID from left to right. For example:
1 - 2 - 3
4 - 5 - 6
7 - 8 - 9
for a 3x3 grid. At the moment it is in the following form:
1 - 1 - 1
2 - 2 - 2
3 - 3 - 3
for (int i = 0; i < rowValue; i++) {
for (int j = 0; j < columnValue; j++) {
Grid r = new Grid(0, 20 + i * size, 20 + j * size, size, size);
r.setRegionID(j + 1);
g.addRegion(r);
}
}
checking if a vertex is inside a square or a circle can be done in O(1). you can do it with library function or elementary math. so the works algorithm you can create is O(#vertices * #regions). you can try to optimise by sorting the vertices and regions by X-axis and then by Y-axis and try to eliminate checking that for sure return false. but seems that in pessimistic scenario you will still have O(#vertices * #regions) time.
You can probably use the Core Java libraries itself:
List<Rectangle2D.Double> rectangles = Arrays.asList(
new Rectangle2D.Double(0d, 0d, 100d, 100d),
new Rectangle2D.Double(100d, 0d, 100d, 100d),
new Rectangle2D.Double(0d, 100d, 100d, 100d),
new Rectangle2D.Double(100d, 100d, 100d, 100d));
Point2D.Double aPoint = new Point2D.Double(30d, 40d);
for (Rectangle2D.Double rectangle:rectangles){
if (rectangle.contains(aPoint)){
System.out.println(rectangle + " has the point " + aPoint);
}
}
Working with plane geometry is extremely easy while using JTS. You can try convert the objects you are using to JTS-specific.

Categories