How to rotate four sprites to the focus of these sprites - java

I wrote the following code:
int a=0, b=0, c=0, d=0;
sprite[number[0]].setPosition(160, 600);
sprite[number[1]].setPosition(560, 600);
sprite[number[2]].setPosition(360, 400);
sprite[number[3]].setPosition(360, 800);
seiten[number[4]].setPosition(-35, 0);
seiten1[number[4]].setPosition(825, 0);
sprite[number[0]].setOrigin(200,0);
sprite[number[1]].setOrigin(-200,0);
sprite[number[2]].setOrigin(0,-200);
sprite[number[3]].setOrigin(0,-200);
sprite[number[0]].setRotation(a++);
sprite[number[1]].setRotation(b++);
sprite[number[2]].setRotation(c++);
sprite[number[3]].setRotation(d++);
I want to rotate the sprites like:
Unfortunately, this code doesn´t work for this purpose. How can I improve my code?
EDIT:
For better understanding:
I have ten sprites with different colors, four of them are shown on the game screen. The coincidence decides which sprite will be shown, but each sprite of them has a different color. Now I want to rotate the four sprites in the lane which is shown in the image. The four sprites should turn on the pink sprite which in fact doesn´t exist in my code. This pink sprite is just a symbol for the centre of the rotation (P(360|600)). The four sprites rotate all in the same lane.
With my code the four sprites rotate but not in the same lane as it is shown in the image. I hope my question is now more clear.

I guess you could use sprite.rotate() and set the sprite origin to the point you want to rotate about with sprite.setorigin().

Try this :
Sprite sprites[],centralSprite;
float centerX=200,centerY=200,radius=100;
SpriteBatch spriteBatch;
#Override
public void create() {
spriteBatch=new SpriteBatch();
Texture badlogic=new Texture("badlogic.jpg");
centralSprite=new Sprite(badlogic);
centralSprite.setSize(50,50);
centralSprite.setPosition(centerX,centerY);
centralSprite.setOriginCenter();
sprites=new Sprite[4];
for (int i=0;i<sprites.length;i++) {
sprites[i] = new Sprite(badlogic);
sprites[i].setSize(50,50);
sprites[i].setRotation(90 * i);
sprites[i].setPosition(centerX + radius * MathUtils.cosDeg(sprites[i].getRotation()),
centerY + radius * MathUtils.sinDeg(sprites[i].getRotation()));
sprites[i].setOriginCenter();
}
}
#Override
public void render() {
Gdx.gl.glClearColor(1,0,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
for (int i=0;i<sprites.length;i++) {
sprites[i].rotate(1);
sprites[i].setPosition(centerX + radius * MathUtils.cosDeg(sprites[i].getRotation()),
centerY + radius * MathUtils.sinDeg(sprites[i].getRotation()));
sprites[i].draw(spriteBatch);
}
centralSprite.draw(spriteBatch);
spriteBatch.end();
}

Related

Java Graphics 2d avoid Polyline distorted corners

i'm working on a graphic interface for drawing subway maps. A line is represented with station as circles and a polyline to link them.You can move the stations with a mouseDrag and of course it updates the displaying map in real time. My problem is when stations comes to a certain angle, there is a polyline distortion and the corner created by the 2 lines is out of the station circle display, i'd like to know if there is a way to avoid this.
screenshots of the app with the polyline issue
here's my code for the polyline's draw
//x and y point array creation
xPoints = new int[this.stationViews.size()];
yPoints = new int[this.stationViews.size()];
for (int i=0;i<this.stationViews.size();i++) {
//fill arrays with the center point of circles representing stations
xPoints[i] = this.stationViews.get(i).getStation().getPosX()-this.stationViews.size()/2;
yPoints[i] = this.stationViews.get(i).getStation().getPosY()-this.stationViews.size();
}
//setting color
g2D.setColor(this.line.getColor());
//set stroke width relative to the zoom level
int strokeWidth=5;
if(!this.stationViews.isEmpty()) {
if (this.stationViews.get(0).getStationSize()>14) {
strokeWidth = this.stationViews.get(0).getStationSize()-13;
}else {
strokeWidth = 3;
}
}
g2D.setStroke(new BasicStroke(strokeWidth));
//draw the polyline
if (this.stationViews.size() >1) {
g2D.drawPolyline(xPoints, yPoints, this.stationViews.size());
}
//draw the station (g2D.drawCircle)
for (StationView stationView : stationViews) {
stationView.affiche(g2D,this.line.getColor());
}
thank you for your help
That is called the miter. You seem to be per default using JOIN_MITER, sharp joining of extended lines at the end, which can point far out of the join for small angles.
g2d.setStroke(new BasicStroke(strokeWidth,
BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 5));
miter a surface forming the beveled end or edge of a piece where a joint is made by cutting two pieces at an angle and fitting them together.
It is also a bishop's cap with a pointy top, hence the name.

positioning a sprite in libgdx

I've got a problem to draw a sprite to my project.
I have a map (960x900) divided into tiles (64x64).
As you can see in the picture, when i click on the bottom left corner of the purple square, the position is (0;0), and when I click on the top right corner of purple square, the position is (36;47)
The problem is that the picture of the purple square has a size of 32x32, and when I draw this picture with libgdx on the screen, the size doesn't match.
Another example: the square with black border has a size of 64x64. So if I draw the purple square in front of the black, the purple should be the half (in height and in width) of the black, no?
Does anyone know why libgdx resizes the purple square?
Sprite sprite = new Sprite(new Texture("assets/purpleSquare.png"));
i draw it in a method
public void render(SpriteBatch batch) {
batch.draw(sprite, 0, 0);
}
I don't know why the picture is resized by libgdx.. I have also tried to do
batch.draw(sprite, 0, 0, width, heigth);
To precise the sprite's size but it doesn't work too..
The size on screen bears no direct relation to the size of the original image. When you draw a sprite you provide the SpriteBatch with a position, width, and height in world coordinates. The sprite will be stretched to fit these world coordinates, regardless of the original image size.
When you click the screen, you are clicking in screen coordinates. The relation between screen and world coordinates is determined by the projection matrix that you use with the SpriteBatch. The projection matrix is typically controlled with a Camera or Viewport object, which you can use to convert between the two coordinate systems using the project and unproject methods.
I'm happy to see that after many hours i found a solution, even if i know which is not correct.
I would like some help to understand the problem's origin.
With this parts of code :
public void update(float delta) {
batch.begin();
drawBackground(); // Draw the background
drawButton(); // Draw the play/pause button
batch.end();
drawMap(); // draw a tmx map made with tiled
batch.begin();
if(!world.isInitial()) {
renderMonster(); // method which draw the monster
}
renderTower(); // method which draw the tower's level
batch.end();
}
I don't understand why i have to do "batch.begin()" and "batch.end()" twice.
I dont' understant why with this code, the purple square is resized.
With this code :
public void update(float delta) {
batch.begin();
drawBackground();
drawButton();
batch.end();
drawMap();
------->sb = new SpriteBatch();
sb.begin();
if(!world.isInitial()) {
renderMonster();
}
renderTower();
sb.end();
}
this line that i add fix the bug with the purple square. If i work with two SpriteBatch (because with one, if i reinitialize SpriteBatch in update method, my pause/play button diseapper) and i initialise the second (SpriteBatch sb) in the update method.
It is correct to initialise a SpriteBatch every time i'm passing on the update method ? There's no method with a SpriteBatch to avoid this problem ?

Moving objects in libgdx

I have a bug object that I want to move across the screen as soon as the game starts. The bug starts from the bottom left of the screen and is supposed to move to the top right and stop. What I have is the bug never really gets to the top right because the game screen(X and Y) size are not equal. How do I make the bug move to that position?
This is what I have.
public void create() {
spriteBatch = new SpriteBatch();
bug = new Sprite(new Texture("EnemyBug.png"));
bug.setSize(50, 85);
bug.setPosition(0,0);
}
public void render() {
xdeg++;
ydeg++;
Gdx.gl.glClearColor(0.7f, 0.7f, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
bug.translate(xdeg, ydeg);
bug.draw(spriteBatch);
spriteBatch.end();
}
I'll assume that you know your window width (W) and height (H). First find the W / H ratio:
float ratio = screenWidth / screenHeight;
Then update your bug position accordingly:
bug.translate(ratio, 1);
This will make the sprite move through the screen diagonal.

Sprite starts in lower left corner of screen - libgdx

I have a player setup with box2d and I am trying to draw a sprite over the player. The player spawns in the middle of the screen, while the sprite spawns in the lower left hand corner of the screen but does move along with the player entity, just starting at a different location.
GameScreen snippet:
#Override
public void render(float delta) {
super.render(delta);
player.update();
world.step(TIMESTEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
}
#Override
public void show() {
player = new Player(world, 0, 0);
}
Player class snippet:
public Player(World world, float x, float y) {
texture = new Texture(Gdx.files.internal("sprites/Player.png"));
sprite = new Sprite(texture);
}
public void update() {
batch = new SpriteBatch();
batch.begin();
sprite.draw();
sprite.setPosition(body.getPosition().x, body.getPosition().y);
batch.end();
body.setLinearVelocity(impulse);
}
I tried setting the position of the sprite in the constructor based on the body's coordinates but it doesn't seem to be working. I have removed body & fixture code. Any push in the right direction is appreciated.
If you enable box2d debug render, you'll probably find that both the texture and the body are starting at the left corner of the screen. Actually, 0, 0 are supposed to be the coordinates for the left bottom corner. In order to set your body in the center of the screen, you should set something like
(SCREEN_WIDTH / 2) / PTM_RATIO, (SCREEN_HEIGHT / 2) / PTM_RATIO
as your body initial position.

LibGDX - properly using Polygon class

I have created Polygon object to wrap my airplane (size of airplane's TextureRegion is 256x74, but size of this one in a game is 70x20). So:
TextureRegion[] texRegsAirplane = TextureRegion.split(textureAirplane, 256, 74);
Rectangle bounds = new Rectangle(0, 0, 70, 20);
Polygon polygon = new Polygon(new float[]{0,0,bounds.width,0,bounds.width,bounds.height,0,bounds.height,0,0});
After that in my update function I update position of it:
public void update(float delta){
Vector2 v = getPosition();
v.add(velocity);
polygon.setPosition(v.x, v.y);
}
Then I render polygon to know where it is:
public void render(SpriteBatch spriteBatch, float pixelPerUnitX, float pixelPerUnitY){
spriteBatch.draw(testTexture,polygon.getX()*pixelPerUnitX, polygon.getY()*pixelPerUnitY,
polygon.getBoundingRectangle().width*pixelPerUnitX,polygon.getBoundingRectangle().height*pixelPerUnitY);
}
At the end I create 2 airplanes and make them fly to each other and every iteration I try to detect collision like below:
public void detectCollision(){
for(Airplane airplane1 : Airplanes){
for(Airplane airplane2 : Airplanes){
if(Intersector.overlapConvexPolygons(airplane1.getPolygon(), airplane2.getPolygon())){
//COLLISION DON'T HAPPEN!!!
}
}
}
I see that 2 rectangles move to each other and intersect, but overlapConvexPolygons function doesn't work! Why?
I've solved this problem. I incorrectly specified vertices. I needed to get rectangular polygon, so I had to use following:
polygon = new Polygon(new float[]{0,0,bounds.width,0,bounds.width,bounds.height,0,bounds.height});
and do not forget set origin if you are going to rotate polygon object:
polygon.setOrigin(bounds.width/2, bounds.height/2);
Now it works perfect!

Categories