I have an ArrayList called rectangles that are at specific x,y,width,height values and another Rect called bar that moves up the screen and is supposed to intersect those rectangles. This is my code:
for (Rect rect : rectangles) {
if(Rect.intersects(bar, rect)) {
Log.d("GameScreen", "intersected");
intersected = rect;
checkButtons();
}
}
However, when I look at my logcat, it isn't logging "GameScreen, intersected" and the counter I have set up from checkButtons() doesn't change the score on the screen when they intersect and bar just seems to pass through the rectangles.
How can I make it so that I am sure that the Rects are intersecting?
Related
i have created a rectangle
public Rectangle rectangle_hitbox;
It has the coordinates and dimensions of the enemies. In the render method, I want that when this rectangle is pressed, the string "Done" is printed. I tried with:
if(Gdx.input.isTouched()){
System.out.println("Done");
} else {
System.out.println("Missed");
}
But it works anywhere on the map and not only in rectangles
The Gdx.input.isTouched() method doesn't know about the position of your rectagle. It just check whether the screen is touched or not.
Try using Gdx.input.getX() and Gdx.input.getY() to get the coordinates of the click or touch event when the screen is touched and check these coordinates against the coordinates of your rectangle.
Also don't forget to unproject the coordinates if you are using a camera. See this answer for more information.
if(Gdx.input.isTouched()){
Vector3 touchPosition = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0f);
// TODO maybe unproject the position using camera.unproject(touchPosition);
if (isTouchInsideRectangle(touchPosition)) {
System.out.println("Done");
}
else {
System.out.println("Missed");
}
}
So my ultimate goal is to draw 50 circles(with processing.core.PApplet) structured around a ring that transitions colors like a neon sign so that it would look like Psychadelics
The circles have to be random sizes and with a diameter of under 210 pixels the circles have to have 8 bars with each "shells" changing colors in order, and at the center, there must be an empty circle with the same color as the background.
Right now I am trying to break this problem into lots of small problems, and currently, I am struggling to make the spaces between the bars to be equally spaced.
Culprit:
(I found the problem to be the ratio between the bar size and the circle size, due to the undefined range random sizing it made seemingly empty circles)
Here is my next problem, The circles seem to be vibrating instead of remaining static, I want the 50 circles to be static while the colors on each shell change each frame.
Here is the part where I tell it to draw smaller and smaller circles
This is the "Drawing component"
public Donut(float x, float y , float d) { //constructor
this.x =x;
this.y =y;
diameter=d;
}
public void draw(PApplet p) {
p.circle(x, y, diameter);
float bar=(float)(Math.random()*(1-10)-1)+1;
for(int i =0; i<8; i++) {
bar+=10;
p.fill(REDS[i],GREENS[i],BLUES[i]);
p.circle(x, y, diameter-bar);
}
}
And here is the part where I tell it to have random sizes and positions(still haven't told it to be placed around a ring yet) //This is the Main Class
public class Psychadelics extends PApplet{
Donut [] DonutList = new Donut [50];
public static void main(String[] args) {
PApplet.main("test.Psychadelics");
}
public void settings() {
size(SCR_W, SCR_H);
}
public void setup() {
for(int i =0; i<DonutList.length;i++) {
float x = (float)(Math.random()*600);
float y = (float)(Math.random()*400);
float diameter = (float)(Math.random()*210);
DonutList [i]= new Donut(x,y,diameter);
}
I have another drawing method inside the main class to tell the Donut class to keep drawing and to keep updating it.
I expect each circle to remain static and to transition colors, each frame but my actual results were the circles each with different colors on each shell vibrating on their specified coordinates
The vibration is caused by float bar=(float)(Math.random()*(1-10)-1)+1;. Because bar is determined inside the draw function, the exact diameter will be slightly different on each frame. Instead, you should create an array of these random floats in the constructor and use random_diameter[i] in the draw loop, so the sizes of the inner circles are created random, but remain constant.
The color remaining constant is caused by p.fill(REDS[i],GREENS[i],BLUES[i]);. You assign specific colors, based on the index, to a circle that is also based on the index. This is where you should use random. For red, green and blue use int(Math.random(0,255));. That way, on each frame, a random color is generated for each circle. If you want more gradual color changes, you'll need to store the color for each circle and add/subtract a small random number. If you want to limit the number of colors, you can use the function you have now, but instead of i, use a random number with the size of the array.
I hope you see that, interestingly, the problems/solutions are each others inverse :)
I'm trying to code a board game and have created the board with for loops. As it stands now I can click a rectangle and am able to change its color. Now I want to have it where if I click a rectangle that is a certain color to change it to a specific color. For example iff it's blue then make it grey. However I get the error Unlikely argument type for equals(): Color seems to be unrelated to Rectangle and my squares turn black instead. Here is my code. Thank you.
public void game (MouseEvent eventGame) {
for(Rectangle r: rectangles) {
if (r.equals(Color.BLUE)) {
r.setOnMouseClicked(event->{
r.setFill(Color.GREY);
});
} else { r.setOnMouseClicked(event->{
r.setFill(Color.BLACK);
});}
}
}
I should also mention when creating the Array I do this: r.setFill(Color.BLUE);.
By calling
r.equals(Color.BLUE)
you are trying to compare a rectangle instance with a Color type. Looking at the API of Rectangle, its equals-method is described as follows:
Checks whether two rectangles are equal.
The result is true if and only if the argument is not null and is a Rectangle object that has the same upper-left corner, width, and height as this Rectangle.
Instead you need to compare the actual Color of the rectangle by calling
r.getFill().equals(Color.WHITE)
(see Post Can you return the color of a rectangle object in java?)
Hope, I could help you.
I am new to 'Collision Detection'; I have found resources on collisions, such as:
Rect rc_img1 = new Rect();
image.getDrawingRect(rc_img1);
Rect rc_img2 = new Rect();
bottomLayout.getDrawingRect(rc_img2);
if (Rect.intersects(rc_img1, rc_img2)){
Toast.makeText(MainActivity.this, "Detected", Toast.LENGTH_LONG).show();
}
The image coming from the top of the screen should collide with the layout at the bottom of the screen. When the image hits the bottom layout I want to perform my actions.
For rectangles colliding with other rectangles, you can check all 8 points of the 2 rectangles with .contains() of the other rectangle. More generally, you can use Area, and Area.intersect(otherArea) != null
Ok I have this code
#Override
public void render() {
// do not update game world when paused
if (!paused) {
// Update game world by the time that has passed
// since last render frame
worldController.update(Gdx.graphics.getDeltaTime());
}
// Sets the clear screen color to: Cornflower Blue
Gdx.gl.glClearColor(0x64/255.0f, 0x95/255.0f, 0xed/255.0f,
0xff/255.0f);
// Clears the screen
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
// Render game world to screen
worldRenderer.render();
}
And it draws a light blue background onto the screen. I am attempting to create a gradient that goes from a dark blue at the top, to a light blue towards the bottom. Is there a simple way to do this? I'm new to Libgdx, and OpenGL so i'm trying to learn from a book but I can't seem to find the answer to this one. I've heard of drawing a big square and having the vertices different colors, but I'm unsure of how to do this.
In libGDX, the ShapeRenderer object contains a drawRect() method that takes arguments for its position and size as well as four colors. Those colors are converted to a 4-corners gradient. If you want a vertical gradient, just make the top corners one color and the bottom corners another color. Something like this:
shapeRenderer.filledRect(x, y, width, height, lightBlue, lightBlue, darkBlue, darkBlue);
From the API for ShapeRenderer:
The 4 color parameters specify the color for the bottom left, bottom right, top right and top left corner of the rectangle, allowing you to create gradients.
It seems ShapeRenderer.filledRect method has been removed in late libGDX versions. Now the way to do this is as follows:
shapeRenderer.set(ShapeRenderer.ShapeType.Filled);
shapeRenderer.rect(
x,
y,
width,
height,
Color.DARK_GRAY,
Color.DARK_GRAY,
Color.LIGHT_GRAY,
Color.LIGHT_GRAY
);
The parameters for rect method work in the same way as those in filledRect used to do, like in Kevin Workman answer.
There are some further details worth bearing in mind before comitting to ShapeRenderer. I for one will be sticking with stretching and tinting Texture.
private Color topCol = new Color(0xd0000000);
private Color btmCol = new Color(0xd0000000);
#Override
public void render(float delta) {
...
batch.end(); //Must end your "regular" batch first.
myRect.setColor(Color.YELLOW); // Must be called, I don't see yellow, but nice to know.
myRect.begin(ShapeRenderer.ShapeType.Filled); //Everyone else was saying call `set`.
//Exception informed me I needed `begin`. Adding `set` after was a NOP.
myRect.rect(
10, 400,
//WORLD_H - 300, // WORLD_H assumed 1920. But ShapeRenderer uses actual pixels.
420,
300,
btmCol, btmCol, topCol, topCol
);
myRect.end();
I was hoping to change transparency dynamically as player health declines. The btmCol and topCol had no effect on transparency, hence I'll stick to Textures. Translating pixel space is no biggie, but this is much more than the proferred single or double line above.