Creating Boundaries to prevent player from moving into in java - java

In the Game I'm going to have a set path of JButtons. I also have an Array which holds all and only the values of the JButtons I want the player to be able to move onto.
JButton[] x = new JButton[71];
x[0] = y[0];
x[1] = y[15];
x[2] = y[16];
x[3] = y[31];
x[4] = y[32];
x[5] = y[47];
x[6] = y[62];
x[7] = y[93];
x[8] = y[63];
x[9] = y[78];
x[10] = y[108];
x[11] = y[168];
x[12] = y[183];
x[13] = y[184];
x[14] = y[153];
x[15] = y[123];
x[16] = y[138];
x[17] = y[197];
x[18] = y[195];
x[19] = y[185];
x[20] = y[186];
x[21] = y[171];
x[22] = y[172];
x[23] = y[173];
I also have another array which holds every single value of the JButtons in my Map.
y = new JButton[225];
for (int j = 0; j < 225; j++) {
y[j] = new JButton();
y[j].addActionListener(this);
panel.add(y[j]);
}
Using this code, how do I prevent the player from moving off the desired path/JButtons and stay on that singular path?

Create a point that follows the button, if the point strikes a certain point on the grid (in this case, your bounds) than flip the x/y-dir or set the speed to 0.
Point point = new Point(xloc, yLoc);
if (point.getX() >= bounds) {
flipSpeed/stopSpeedHere
}
Also you can probably use a forLoop, and just iterate through all that code instead of just typing it out.

Related

The type of the expression must be an array type but it resolved to TileSet

I am trying to program a Java RPG game, but the tileSet Loader won't work.
Here is the code:
TileSet tileSet1 = new TileSet("/tiles/rpg.png", 12, 12, 0, null);
Level level = new Level(this, "/level/level1.txt", tileSet);
That is causing errors. And this should be executed through it, but the problem is that it is an array. But it needs to be an array for other methods in the class:
public Level(Game game, String path, TileSet[] ts1) {
this.game = game;
this.ts = ts1;
String file = Utils.loadFileAsString(path);
String[] tokens = file.split("\\s");
sizeY = Utils.parseInt(tokens[1]);
tileMap = new int[1][sizeX][sizeY];
int i = 2;
for(int y = 0; y < sizeY; y++){
for(int x = 0; x < sizeX; x++){
tileMap[0][x][y] = Utils.parseInt(tokens[i++]);
}
}
}
Why might this be?
Constructor is expecting an array in last parameter, but you passed just single object. Try to wrap it into array like :
Level level = new Level(this, "/level/level1.txt", new TileSet[]{tileSet});

Trouble understanding Vectors, my object gets stuck after a few iterations

Im trying to write a class that draws a square shape with 4 vertexes. I want each vertex to have its own velocity and path for each iteration in the drawloop. It seems to work for a few iterations until it gets "stuck" and Im having trouble understanding why that is. Any help or information regarding this is helpful, I suspect I dont have a full grasp of using vectors properly.Heres an image of when the code is running.
void draw() {
p.display();
//Stop loop setting
if (loop == 0) {
noLoop();
}
}
//----------------------------------------------------------------//
class Pillar
{
PVector v1Velocity,v2Velocity,v3Velocity,v4Velocity,
v1Pos,v2Pos,v3Pos,v4Pos,
v1End,v2End,v3End,v4End,
v1Acceleration,v2Acceleration,v3Acceleration,v4Acceleration,
origin;
float life,scale,randDegrees1,randDegrees2,randDegrees3,randDegrees4,maxVelocity;
Pillar (float _x, float _y, float _scale) {
scale = _scale; // scale of pillar
origin = new PVector(_x,_y); //pillar point of origin
v1Pos = new PVector(origin.x-(scale*1.5),origin.y); //vertex 1 start left
v2Pos = new PVector(origin.x,origin.y-scale); //vertex 2 start top
v3Pos = new PVector(origin.x+(scale*1.5),origin.y); //vertex 3 start right
v4Pos = new PVector(origin.x,origin.y+scale); //vertex 4 start bottom
v1End = new PVector(origin.x+random(50,200),origin.y-random(50,200));
v2End = new PVector(origin.x+random(50,200),origin.y-random(50,200));
v3End = new PVector(origin.x+random(50,200),origin.y-random(50,200));
v4End = new PVector(origin.x+random(50,200),origin.y-random(50,200));
randDegrees1 = random(360);
randDegrees2 = random(360);
randDegrees3 = random(360);
randDegrees4 = random(360);//Fixa denna.
v1Velocity = new PVector(cos(radians(randDegrees1)),sin(radians(randDegrees1)));
v2Velocity = new PVector(cos(radians(randDegrees2)),sin(radians(randDegrees2)));
v3Velocity = new PVector(cos(radians(randDegrees3)),sin(radians(randDegrees3)));
v4Velocity = new PVector(cos(radians(randDegrees4)),sin(radians(randDegrees4)));
maxVelocity = 5;
life = 100;
}
void calculateVector() {
v1Acceleration = PVector.sub(v1End,v1Pos);
v1Velocity.add(v1Acceleration);
v2Acceleration = PVector.sub(v2End,v2Pos);
v2Velocity.add(v2Acceleration);
v3Acceleration = PVector.sub(v3End,v3Pos);
v3Velocity.add(v3Acceleration);
v4Acceleration = PVector.sub(v4End,v4Pos);
v4Velocity.add(v4Acceleration);
v1Acceleration.setMag(life);
v2Acceleration.setMag(life);
v3Acceleration.setMag(life);
v4Acceleration.setMag(life);
v1Velocity.limit(maxVelocity);
v2Velocity.limit(maxVelocity);
v3Velocity.limit(maxVelocity);
v4Velocity.limit(maxVelocity);
v1Pos.add(v1Velocity);
v2Pos.add(v2Velocity);
v3Pos.add(v3Velocity);
v4Pos.add(v4Velocity);
life -= 1;
}
void display () {
beginShape();
stroke(0,0,0,50);
vertex(v1Pos.x,v1Pos.y);
vertex(v2Pos.x,v2Pos.y);
vertex(v3Pos.x,v3Pos.y);
vertex(v4Pos.x,v4Pos.y);
endShape(CLOSE);
calculateVector();
}
}

displaying a circling text using loop

circling text
hi, i need to know how to made this using loop in javafx.
am i need to set the coordinate and rotation manually , or is there something i need to know to make the text circling neatly ?
here's my code so far :
int x = 0;
int y = 100;
int r = -50;
for(int i =0; i<=14; i++){
Text text2 = new Text(x,y, ary[i]);
text2.setRotate(r);
pane.getChildren().add(text2);
x=x+10;
y=y-5;
if(x<=90){
x=x+10;
y=y-5;
r=r+5;
}
else if(x>=100){
x = x-10;
y = y+5;
r = r+3;
}
}

Catmull Rom Spline implementation (LibGDX)

I want to generate a random spline across my screen.
Here is what I have so far:
public class CurvedPath {
Random rn;
CatmullRomSpline<Vector2> curve;
float[] xPts;
float[] yPts;
Vector2[] points;
public CurvedPath(){
points = new Vector2[10];
rn = new Random();
curve = new CatmullRomSpline<Vector2>(points,false);
for(int i = 0 ; i < 10; i++){
xPts[i] = rn.nextFloat()*SampleGame.WIDTH;
yPts[i] = SampleGame.HEIGHT*i/10;
}
}
}
I'm pretty confused on the documentation that has been provided on how to use the CatmullRomSpline object ( https://github.com/libgdx/libgdx/wiki/Path-interface-&-Splines )
Basically what I am trying to do here is generate 10 random points equally distributed across the height of my screen, and randomly placed along the width of the screen to create a randomized curved path.
So within the constructor's for loop you can see that I generate the x and y values of each control point for the spline.
How can I give input these points into the spline object and render it on the screen?
-thanks
update
Let me reword my question to be a little more specific..
I have my control points represented by xPts and yPts. Now I want to get the points that fall along the spline, how do I do this using these two vectors? The constructor for a CatmullRomSpline takes a Vector2, not two float[] 's
what you did. Fill with points:
curve = new CatmullRomSpline<Vector2>(points,false);
To get a point on the curve:
Vector2 point = new Vector2();
curve.valueAt(point, 0.5f);
valueAt() Parameter explanation:
1 (point) the point you are looking for is stored in the Vector2 object.
float between 0 and 1, 0 is the first point, 1 the last one. 0.5f is middle. This float represents the hole distance from first to last point.
Getting and render 100 points can look like this:
Vector2 point = new Vector2();
for (int i = 0; i <= 100; i++) {
curve.valueAt(point, i * 0.01f);
// draw using point.x and point.y
}
answer to you edited question:
for(int i = 0 ; i < 10; i++){
points[i].x = rn.nextFloat()*SampleGame.WIDTH;
points[i].y = SampleGame.HEIGHT*i/10;
}
curve = new CatmullRomSpline<Vector2>(points,false);
The process is also detailed here: https://github.com/libgdx/libgdx/wiki/Path-interface-&-Splines
/*members*/
int k = 100; //increase k for more fidelity to the spline
Vector2[] points = new Vector2[k];
/*init()*/
CatmullRomSpline<Vector2> myCatmull = new CatmullRomSpline<Vector2>(dataSet, true);
for(int i = 0; i < k; ++i)
{
points[i] = new Vector2();
myCatmull.valueAt(points[i], ((float)i)/((float)k-1));
}

Checking 2 arrays to see if they have the same values?

I would like to compare two arrays to see if they have the same values.
If I have a array called
public static float data[][]
which holds Y coordinates of a terrain, how can I check that array with another
public static int coords[][]
without iterating through all the coordinates?
Both arrays have over 1000 values in them. Iterating through them is not an option, since I must iterate through them over four times per second.
I am doing this to attempt to find if two objects are colliding. I have attempted using libraries for this, however I cannot find per-coordinate collision detection as specific as I need it.
Edit: Why I am unable to just iterate through this small amount of vertices is this.
The problem is, this is a MultiPlayer game,and I would have to iterate through all 1000 coordinates for every player. Meaning that just 10 players online is 10,000 100 online is 100,000. You can see how easily that would lag or at least take up a large percentage of the CPU.
Input of coordinates into the "Data" variable:
try {
// Load the heightmap-image from its resource file
BufferedImage heightmapImage = ImageIO.read(new File(
"res/images/heightmap.bmp"));
//width = heightmapImage.getWidth();
//height = heightmapImage.getHeight();
BufferedImage heightmapColour = ImageIO.read(new File(
"res/images/colours.bmp"));
// Initialise the data array, which holds the heights of the
// heightmap-vertices, with the correct dimensions
data = new float[heightmapImage.getWidth()][heightmapImage
.getHeight()];
// collide = new int[heightmapImage.getWidth()][50][heightmapImage.getHeight()];
red = new float[heightmapColour.getWidth()][heightmapColour
.getHeight()];
blue = new float[heightmapColour.getWidth()][heightmapColour
.getHeight()];
green = new float[heightmapColour.getWidth()][heightmapColour
.getHeight()];
// Lazily initialise the convenience class for extracting the
// separate red, green, blue, or alpha channels
// an int in the default RGB color model and default sRGB
// colourspace.
Color colour;
Color colours;
// Iterate over the pixels in the image on the x-axis
for (int z = 0; z < data.length; z++) {
// Iterate over the pixels in the image on the y-axis
for (int x = 0; x < data[z].length; x++) {
colour = new Color(heightmapImage.getRGB(z, x));
data[z][x] = setHeight;
}
}
}catch (Exception e){
e.printStackTrace();
System.exit(1);
}
And how coordinates are put into the "coords" variable (Oh wait, it was called "Ship", not coords. I forgot that):
try{
File f = new File("res/images/coords.txt");
String coords = readTextFile(f.getAbsolutePath());
for (int i = 0; i < coords.length();){
int i1 = i;
for (; i1 < coords.length(); i1++){
if (String.valueOf(coords.charAt(i1)).contains(",")){
break;
}
}
String x = coords.substring(i, i1).replace(",", "");
i = i1;
i1 = i + 1;
for (; i1 < coords.length(); i1++){
if (String.valueOf(coords.charAt(i1)).contains(",")){
break;
}
}
String y = coords.substring(i, i1).replace(",", "");;
i = i1;
i1 = i + 1;
for (; i1 < coords.length(); i1++){
if (String.valueOf(coords.charAt(i1)).contains(",")){
break;
}
}
String z = coords.substring(i, i1).replace(",", "");;
i = i1 + 1;
//buildx.append(String.valueOf(coords.charAt(i)));
////System.out.println(x);
////System.out.println(y);
////System.out.println(z);
//x = String.valueOf((int)Double.parseDouble(x));
//y = String.valueOf((int)Double.parseDouble(y));
//z = String.valueOf((int)Double.parseDouble(z));
double sx = Double.valueOf(x);
double sy = Double.valueOf(y);
double sz = Double.valueOf(z);
javax.vecmath.Vector3f cor = new javax.vecmath.Vector3f(Float.parseFloat(x), Float.parseFloat(y), Float.parseFloat(z));
//if (!arr.contains(cor)){
if (cor.y > 0)
arr.add(new javax.vecmath.Vector3f(cor));
if (!ship.contains(new Vector3f((int) sx, (int) sy, (int) sz)))
ship.add(new Vector3f((int) sx, (int) sy, (int) sz));
// arr.add(new javax.vecmath.Vector3f(Float.parseFloat(x), Float.parseFloat(y), Float.parseFloat(z)));
}
Thanks!
You can do like this but only applicable for same data type.
Arrays.deepEquals(data, coords);
For single dimension Array you can use this
Arrays.equals(array1, array1);
Arrays.deepEquals(a, b);
Try this but this will work only if the elements are in order.
No way around it, I'm afraid. Comparing data sets to see if they are identical demands looking at all elements, by definition. On a side note, comparing 1000 values is nothing even on relatively old hardware. You can do it thousands of time per second.

Categories