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");
}
}
Related
I was working on an animation on processing. Then, I have a question about the lights. Normally, my code is more long. However, I made a simple code which can usefull also for the beginners.
void setup()
{
size(400, 400, P3D);
noStroke();
}
void draw()
{
background(0);
if (mousePressed) { // lights should work if the mouse pressed on the sphere
lights(); // It should continue till press again on the sphere
} // If the mouse pressed again on the sphere, lights should close
translate(200,200,0); // translate the sphere to the middle of window
sphere(100); // making a sphere for see ligts
}
So, as you can see on the comments. If the mouse pressed on the sphere the lights should work and it should keep working till the mousepressed again on the sphere. Then, if mouse pressed on the sphere it should close the lights.It should keep working again and again. If you know how to make it. You are welcome. Thanks.
You need to have a variable that keeps the state of the light and switch it on if it's off or switch it off if it's on.
After doing that, using mousePressed in the if statement might create some problems since if the click is not quick enough (maybe you press for a bit too long) it will turn on and then off the light so it will look like it was never turned on.
To avoid that I suggest using mouseReleased() function.
Heres the final code:
boolean isOn = false; // variable keeping the state of the light
void setup()
{
size(400, 400, P3D);
noStroke();
}
void draw()
{
background(0);
if (isOn) // checks the state in which the light should be
lights();
translate(200,200,0); // translate the sphere to the middle of window
sphere(100); // making a sphere for see ligts
}
void mouseReleased() { // this function is automatically called in draw method
if (isOn) //after a click the state of the light is inverted
isOn = false;
else isOn = true;
}
Anyway if for some reason you needed to use specifically mousePressed function her's some code also working:
void draw()
{
background(0);
if (mousePressed) { // lights should work if the mouse pressed on the sphere
if (isOn)
isOn = false;
else isOn = true;
delay(200); // delay added to minimize the problem explained above
} // If the mouse pressed again on the sphere, lights should close
if (isOn)
lights();
translate(200,200,0); // translate the sphere to the middle of window
sphere(100); // making a sphere for see ligts
}
I would like to disable zooming by mouse dragging(which paints that rectangle), but not disable zooming by MouseWheel. I found in another topic how to disable zoom reset while dragging mouse to left (restoreAutoBounds) and I'm interested in how to solve this problem. Is there a little shortcut to do that?
Ok, I've done it, by overriding MouseWheelListener. After chartPannel.setMouseZoomable(false).:
chartPanel.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent arg0) {
if (arg0.getWheelRotation() > 0) {
chartPanel.zoomOutDomain(0.5, 0.5);
} else if (arg0.getWheelRotation() < 0) {
chartPanel.zoomInDomain(1.5, 1.5);
}
}
});
zoom(In/Out)Domain, because I wanted to rescale only domain axis.
The mouse wheel listener implementation in the previous answer removes the zoom animation and does not zoom from the current mouse position. I found a workaround by hidding the rectangle using a transparent paint:
chartPanel.setZoomTriggerDistance(Integer.MAX_VALUE);
chartPanel.setFillZoomRectangle(false);
chartPanel.setZoomOutlinePaint(new Color(0f, 0f, 0f, 0f));
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?
i am writing a game using Libgdx and i need to detect when the user touches a sprite.
I tried to do so with this following code:
this code set the rect's position
for(int i = 0;i<circlesArray.length;i++)
{
rect[i] = new Rectangle(circles.getPosition(i).x,circles.getPosition(i).y,height/8,height/8);
}
and this code set the click as rectangle and checks if it overlaps the sprite
if(Gdx.input.isTouched())
{
click = new Rectangle(Gdx.input.getX(),Gdx.input.getY(),Gdx.input.getX(),Gdx.input.getY());
if(Intersector.overlaps(click,rect[1]));
{
System.out.println("clicked");
x[1] = 0;
}
}
From some reason that i cant understand the program detects a collision even when it is not happened, when i tap anywhere on the screen it says that i pressed the sprite.
What should i do to fix it?
This is your issue:
click = new Rectangle(Gdx.input.getX(), Gdx.input.getY(), Gdx.input.getX(), Gdx.input.getY());
Should be:
click = new Rectangle(Gdx.input.getX(), Gdx.input.getY(), 1, 1);
The last two parameters of a Rectangle are width and height. Here we give the rectangle a width and a height of 1 pixel but you can set it to whatever you like. You were setting those parameters as the inputs x and y which are different every time giving you varying results.
Edit:
To translate the input coordinates to your game world's coordinates based on the camera you have to do this:
Vector3 clickPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(clickPos);
click = new Rectangle(clickPos.x, clickPos.y, 1, 1);
When you use camera.unproject(Vector) it translates your inputs coordinates to work based off of where your camera is positioned in the game world. So now we're using a vector clickPos.
The reason we use a Vector3 is because this is what is required by the unproject function. We supply this vector with the inputs x and y but give it a 0 for the third parameter z since this is a 2d world.
I am currently trying to snap a crosshair sprite to my game's cursor using libgdx. The game is top-down view:
Texture crosshair_text = new Texture(Gdx.files.internal("data/crosshair1.png"));
this.crosshair = new Sprite(crosshair_text, 0, 0, 429, 569);
//...
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
cam.update();
batch.setProjectionMatrix(cam.combined);
batch.begin();
//.. sprites that scale based on camera (top-down view)
batch.end();
//draws 'ui' elements
floatingBatch.begin();
//...
//snap to cursor
this.crosshair.setPosition( Gdx.input.getX(), (Gdx.graphics.getHeight()-Gdx.input.getY()) );
//transform
this.crosshair.setScale(1/cam.zoom);
//draw
this.crosshair.draw(floatingBatch);
floatingBatch.end();
}
Sorry if there are errors that I didn't catch, this isn't an exact copy of my code. The problem here is that 1. The sprite doesn't snap to the correct position and 2. the crosshair sprite lags behind the current position of the mouse on the screen. Can anyone give me insight on how to fix either of these two issues?
Your position might not be correct because the screen location isn't necessarily the right location to draw onto using your OrthographicCamera, try using the unproject first. E.g:
Vector3 mousePos = new Vector3( Gdx.input.getX(), (Gdx.graphics.getHeight()-Gdx.input.getY()), 0); //Get the mouse-x and y like in your code
cam.unproject(mousePos); //Unproject it to get the correct camera position
this.crosshair.setPosition(mousePos.x, mousePos.y); //Set the position
Plus you need to make sure your floating batch is set to the projection matrix of the camera, add the following code:
floatingBatch.setProjectionMatrix(cam.combined);
Instead of drawing a Sprite at the mouse position, you could change the cursor image:
Gdx.input.setCursorImage(cursorPixMap, hotspotX, hotspotY);
Where cursorPixMap is a PixMap of the new cursor image an hotspotX and hotspotY is the "origin" of the PixMap/cursor image. In your case it would be the center of the crosshair.
So basicly the Gdx.input.getX() and Gdx.input.getY() return the current position of the hotspotX and hotspotY.
I suggest to read the wiki artikcle about the cursor.