Color Libgdx Rectangle object - java

I'm making collisions for my game using libgdx and I'm having some issues. Things don't fall right. I'm using Rectangle objects around my character and terrain and would like to know if there is any way to color/show the rectangle so I can see exactly why are collisions are buggy. Is this possible?
These are the rectangles :
private Rectangle bottom, left, right, top, full;
full = new Rectangle(0f,0f,128f,10f);
bottom = new Rectangle(0f,0f,128f,16f);
left = new Rectangle(0f,16f,64f, 96f);
right = new Rectangle(64f,16f,64f,96f);
top = new Rectangle(0f,112f,128f,16);

You will need to use ShapeRenderer
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/glutils/ShapeRenderer.html
ShapeRenderer shapeRenderer;
shapeRenderer = new ShapeRenderer();
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.rect(x, y, width, height);
shapeRenderer.end();

Related

Move a shape to place where my finger is touched

#Override
public void create()
{
batch = new SpriteBatch();
shape = new ShapeRenderer();
velocity = new Vector2(100, 0);
position = new Rectangle(0, 5, 100, 100);
font = new BitmapFont();
font.setColor(Color.BLACK);
font.getData().scale(3f);
camera = new OrthographicCamera();
confCamera();
}
#Override
public void render()
{
if(Gdx.input.isTouched())
position.x = Gdx.input.getX() - position.width/2;
position.y = (Gdx.input.getY() - position.height/2);
shape.begin(ShapeRenderer.ShapeType.Filled);
shape.setColor(Color.BLACK);
shape.rect(position.x, position.y, position.width, position.height);
shape.end();
}
It's a simple code, but I'm not undestanding Y axis, my shape moves like a mirror. If I touch on top, my shape goes to bottom. If I touch on bottom, my shape goes to top. How to fix it?
LibGDX (by default) for rendering uses coordinate system where 0 is at bottom of the screen and the more you go up Y coordinate grows.
Also, when you read input coordinates (touches, moves...) you get screen coordinates, but when you render your graphics you are using "world" coordinates. They are in 2 different coordinate system so to convert from screen to world you have to use camera.unproject() call. Should be like:
Vector3 touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
and then use touchPos.x and touchPos.y.
The similar question is asked here so you can find more answers there:
Using unProject correctly in Java Libgdx

How to get a region of rotated Actor in libgdx

In a small game I am developing using libgdx, I have spears as game objects.
I implemented them using Scene2D, as a subclass of Actor. The spears can be rotated by multitudes of 90° . I want to have just the "speartip" part of my image as a part that "hurts" the Player, the shaft shall be harmless. So when I construct my Spear object, I also construct 2 rectangles that cover the tip and the shaft of the spear. But when my actor gets rotated using setRotation() , the rectangles obviously don't, cause they aren't "attached" to the spear object.
Do you have some suggestions on how to handle this kind of stuff? Code below.
public TrapEntity(Texture texture, float pos_x, float pos_y, float width, float height, Vector2 center,
float rotation, Rectangle hurtZone, Rectangle specialZone) {
super(texture, pos_x, pos_y, width, height, center, rotation);
this.hurtZone = new Rectangle(hurtZone.getX(), hurtZone.getY(), hurtZone.getWidth(), hurtZone.getHeight());
this.specialZone = new Rectangle(specialZone.getX(), specialZone.getY(), specialZone.getWidth(), specialZone.getHeight());
}
And the render method in the same class. I use it to render the bounds of the "hurtZone" rectangle:
#Override
public void draw(Batch batch, float alpha){
super.draw(batch, alpha);
batch.end();
sr.setProjectionMatrix(batch.getProjectionMatrix());
sr.setTransformMatrix(batch.getTransformMatrix());
sr.begin(ShapeRenderer.ShapeType.Line);
sr.setColor(Color.RED);
sr.rect(hurtZone.getX(), hurtZone.getY(), hurtZone.getWidth(), hurtZone.getHeight());
sr.end();
batch.begin();
}
Rectangles can't be rotated, I had to do a similar thing and struggled with finding a solution. The best solution I have found yet is to use a Polygon. You would do something like this;
//Polygon for a rect is set by vertices in this order
Polygon polygon = new Polygon(new float[]{0, 0, width, 0, width, height, 0, height});
//properties to update each frame
polygon.setPosition(item.getX(), item.getY());
polygon.setOrigin(item.getOriginX(), item.getOriginY());
polygon.setRotation(item.getRotation());
Then, to check if a point is within the rotated Polygon use;
polygon.contains(x, y);

Collision Detection between a Oval and Arc

I am writing a simple game, or so it seemed. I created a class that draws a Arc2D (half a circle shape), that same class will repaint the arch as the mouse move.
Then I created a new class that draws ovals. This class has some simple mathematics to move the ovals on the screen. The movement of the ovals are not very important. So now that this is done I want to detect if the Oval collides with the arc(half a circle, Only the arc line) at any point.
What I have attempted is making the oval a Rectangle and use the intersect method. This code is in the draw method for the arc.
Arc2D temp= new Arc2D.Double(200, 200, 100, 100, angle, 180, Arc2D.OPEN);
MasterOval m = new MasterOval();
Rectangle r1 = m.bounds();//This gets the bounds of the oval
if(r1.intersects(temp.getBounds()))
System.out.println("hit");//display if intersects
For some reason I cant figure out why it will not display the word hit when it collides with the arc. Is there a way to see if they intercect? This is all code I can provide due to privacy policies. Please help.
Well, I'm not sure if your MasterOval class implements the Shape interface or not, but if it does (if it doesn't, consider using Ellipse2D.Double or something of that sort), the easiest way (standard perhaps ?) of checking for collision between Shape instances is using Area:
Shape1 shape1 = new Arc2D.Double(...);
Shape2 shape2 = new Ellipse2D.Double(...);
Area area1 = new Area(shape1);
Area area2 = new Area(shape2);
if (area1.intersect(area2)) {
...
}

Libgdx - Box2D: Attach Physics Body Editor Loader mask to dynamic texture

I have a texture of a circle, which gets drawn to a new position when a touch drag occurs. It isn’t set up as a body.
I have made a physics map using Aurelien Ribon's Physics Body Editor Loader GUI to the circle's upper and lower part, and I’d like to draw that mask over the texture’s position, and to its new position when a drag occurs.
How can I do this? In my create method I initialize the variables, the mask gets drawn to the texture’s initial position, but when I move it the mask stays at the circle’s initial position.
Here's my code:
Create() method:
//... rest of the method ommited for clarity
karika = gameWorld.getKarika();
World world = new World(new Vector2(0, 0), false);
Box2DDebugRenderer renderer = new Box2DDebugRenderer();
BodyEditorLoader karikaLoader = new BodyEditorLoader(Gdx.files.internal("data/collision-masks/karika.json"));
BodyDef karikaDef = new BodyDef();
karikaDef.type = BodyType.DynamicBody;
karikaDef.position.set(karika.getPosition().x, karika.getPosition().y);
karikaDef.angle = karika.getRotation();
Body karikaBody = world.createBody(karikaDef);
FixtureDef karikaFixture = new FixtureDef();
karikaFixture.density = 0.5f;
karikaFixture.friction = 0.8f;
karikaFixture.restitution = 0.6f;
karikaLoader.attachFixture(karikaBody, "karika", karikaFixture, karika.getWidth());
Vector2 karikaBodyOrigin = karikaLoader.getOrigin("karika", karika.getWidth()).cpy();
//rest of the method ommited for clarity
My render() method:
//...
batch.begin();
batch.draw(karikaTexture, karika.getPosition().x, karika.getPosition().y, karika.getWidth() / 2, karika.getHeight() / 2, karika.getWidth(), karika.getHeight(), 1, 1, karika.getRotation(), 0, 0, karikaTexture.getWidth(), karikaTexture.getHeight(), false, false);
batch.end();
renderer.render(world, cam.combined);
world.step(1 / 60f, 6, 2);
//...
The texture that is being drawn in the render method is my circle's texture. As said before, I haven't set that up as a body, only the collision mask.
What I'd like to do, is attach the mask to the texture, and keep up with it's position, for example when I drag the circle, the mask should stay on the circle.
Look into the DebugRenderer. It works very well, and does precisely what you're asking.
Here's how to use it.
debugRenderer.render(this.world, camera.combined);
And here's a more thorough tutorial.
I recommend you assign your circle's position using physics object's position rather than trying to assign the physics object to your texture's position.
In your code, it looks like you create your karikaBody using the current position of karika, but the karikaBody position is never being updated after that. So your "collision mask" (your physics body) position never changes.

Drawing font inside moving sprite libgdx

I have some problems with font drawing in the center of the sprite. The sprite is moving along the screen. I set my orthographic camera to:
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h/w);
and:
Sprite spr = new Sprite(starTx);
spr.setSize(0.3f, 0.3f);
spr.setOrigin(0.15f, 0.15f);
spr.setPosition(0.2f, 0.25f*(i+1));
Inside render I have the code:
batch.setProjectionMatrix(camera.combined);
batch.begin();
spr.draw(batch);
font.setScale(1, (w/h)*3);
font.draw(batchFont, mDate, valueX, valueY);
batch.end();
I must draw the font in the center of the sprite. Could anyone tell me how to calculate valueX and ValueY?
Not exactly what you posted, the screen width is 1, so multiplying for it is useless :p More general approach:
valueX = spr.getX()+spr.getWidth()/2F;
valueY = spr.getY()+spr.getHeight()/2F;

Categories

Resources