I am trying to rotate a sprite in an android app using LibGDX. I have used sprite.rotate and sprite.setRotation in an attempt to rotate the sprite, but none have worked.
public Cannon(){
cannend = new Texture("cannonend.png");
cannon = new Sprite(cannend, (Gdx.graphics.getWidth() / 2) - (cannend.getWidth() / 2) + 3, (Gdx.graphics.getHeight() / 2) + (Gdx.graphics.getHeight() / 6), (cannend.getWidth()), (cannend.getHeight()));
cannon.setPosition((Gdx.graphics.getWidth()/ 2) - (cannend.getWidth() / 2) + 3, (Gdx.graphics.getHeight() / 2) + (Gdx.graphics.getHeight() / 6));
cannon.setRotation(70);
cannon.setCenter((Gdx.graphics.getWidth()/ 2) - (cannend.getWidth() / 2), (Gdx.graphics.getHeight() / 2) + (Gdx.graphics.getHeight() / 6) - (cannend.getWidth() / 2));
}
When I draw this sprite in my Playstate class this is the code that I use.
public PlayState(GameStateManager gsm) {
super(gsm);
cannon = new Cannon();
cannontop = new Texture("Cannon.png");
man = new Man(0, (Gdx.graphics.getHeight() / 4));
bg = new Texture("background.png");
cam.setToOrtho(false, 2160, 3840);
}
//skipped some irrelevant code
#Override
public void render(SpriteBatch sb) {
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(bg, cam.position.x - (cam.viewportWidth / 2), 0);
sb.draw(man.getTexture(), man.getPosition().x, man.getPosition().y);
sb.draw(cannontop, (bg.getWidth()/ 2) - (cannon.getTextureWidth() / 2) - 23, (bg.getHeight() / 2) + (bg.getHeight() / 5));
sb.draw(cannon.getTexture(), (bg.getWidth()/ 2) - (cannon.getTextureWidth() / 2), (bg.getHeight() / 2) + (bg.getHeight() / 5) - (cannontop.getWidth() / 2) + 8);
sb.end();
}
I have just learnt Java, so please forgive me if some of my code is bad. I have also added and taken away single pixels for the widths and heights when drawing things - I'm not sure whether this is ok, as I normally use ratios because they dont seem to affect gameplay when screen sizes change. Anyway, what could I use to rotate the sprite a certain amount every frame? Also, what does sprite.getX() do?
Thanks.
You are not drawing sprite you are drawing texture of sprite.
sprite.draw(batch);
Related
In LibGDX I'm trying to have my OrthographicCamera follow the playersprite when I move it around. Currently, the playersprite gets updated first and then in the next frame the camera updates to the position of the playersprite in the previous frame. Here is an example:
https://i.imgur.com/wxJUizU.gifv
I have tried moving gameMap.render(cam) to the bottom of the method, but not only does it not solve my problem, it also places the map textures above the playersprite, so you won't be able to see the playersprite anymore when it moves under the map.
Here is the code for rendering the playersprite and camera:
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.viewportHeight = Gdx.graphics.getHeight() / 3;
cam.viewportWidth = Gdx.graphics.getWidth() / 3;
gameMap.render(cam);
gameMap.getTiledMapRenderer().getBatch().begin();
player.draw(gameMap.getTiledMapRenderer().getBatch());
gameMap.getTiledMapRenderer().getBatch().end();
cam.position.x = player.getX() + (player.getWidth() / 2);
cam.position.y = player.getY() + (player.getHeight() / 2);
cam.update();
}
Typically, you want to break your logic into two sections:
Update
Render
So, before each frame you want to perform any pertinent calculations.
Your camera is currently updating late, because it isn't performing logic until after the gameMap has been updated.
For example:
// Perform logic and update the state
public void Update () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.viewportHeight = Gdx.graphics.getHeight() / 3;
cam.viewportWidth = Gdx.graphics.getWidth() / 3;
gameMap.getTiledMapRenderer().getBatch().begin();
cam.position.x = player.getX() + (player.getWidth() / 2);
cam.position.y = player.getY() + (player.getHeight() / 2);
}
// Draw map
public void Render () {
gameMap.render(cam);
player.draw(gameMap.getTiledMapRenderer().getBatch());
gameMap.getTiledMapRenderer().getBatch().end();
cam.update();
}
I'm not familiar with the framework you're using, so I can't tell if this would run or not, but when working with frames in any game development situation, this is how you want to handle things.
Like Bradley says you should split your logic into two parts: update and render
Create a udpate() method where you place all your things to update and call this method first in the render() method:
public void update(){
//Update camera position
cam.position.x = player.getX() + (player.getWidth() / 2);
cam.position.y = player.getY() + (player.getHeight() / 2);
//apply all updates to the camera before this the changed position won't apply to the camera
cam.update();
}
#Override
public void render() {
//First call the update method we created
update();
//Clear the Screen
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//Render
gameMap.render(cam);
gameMap.getTiledMapRenderer().getBatch().begin();
player.draw(gameMap.getTiledMapRenderer().getBatch());
gameMap.getTiledMapRenderer().getBatch().end();
}
You don't need to call:
cam.viewportHeight = Gdx.graphics.getHeight() / 3;
cam.viewportWidth = Gdx.graphics.getWidth() / 3;
every frame. It's enough to call it ones in the create() method
Im working with Box2D on LibGDX, and I'm working with bodies and collisions etc..
HERE, i had the problem of a body colliding with another, when it shouldn't do that.
Now after Knowing that i need to use ChainShapes, i started with that.
Whenever u run my project, i get an assertion error:
Assertion failed: (count >= 2), function CreateChain, file
/Users/tom/Coding/slave/workspace/libgdx-mac/extensions/gdx-box2d/gdx-box2d/jni/Box2D/Collision/Shapes/b2ChainShape.cpp,
line 62.
So i tried a small debug to print the vertex count, and the vertex out printed as 0.
Problem is that i am adding the vertices and they dont appear to be added...
Code for adding verticies:
chain = new ChainShape();
chain.setNextVertex(new Vector2((posx - size) / PPM, (posy + size) / PPM));
chain.setNextVertex(new Vector2((posx + size) / PPM, (posy + size) / PPM));
chain.setNextVertex(new Vector2((posx + size) / PPM, (posy - size) / PPM));
chain.setNextVertex(new Vector2((posx - size) / PPM, (posy - size) / PPM));
System.out.println(chain.getVertexCount());
Vertex count is printed as 0, thats why i am getting the error, I dont know how to fix it, so please help :)
Create your ChainShape in this way :
ChainShape chain=new ChainShape();
Vector2 vector[]=new Vector2[4];
vector[0]=new Vector2((posx - size) / PPM, (posy + size) / PPM);
vector[1]=new Vector2((posx + size) / PPM, (posy + size) / PPM);
vector[2]=new Vector2((posx + size) / PPM, (posy - size) / PPM);
vector[3]=new Vector2((posx - size) / PPM, (posy - size) / PPM);
chain.createChain(vector);
System.out.println(chain.getVertexCount()); // 4 on console
If still you've problem, check the value of posx, posy, size , PPM
I'm currently working on a simple textured rectangle class using LWJGL (GL11). This is what I have so far in the draw method.
glEnable(GL_TEXTURE_2D);
color.bind();
glBegin(GL_QUADS);
t.bind();
if (centered)
{
glTexCoord2d(0, 0);
glVertex2d(x - (width / 2), y - (height / 2));
glTexCoord2d(t.getWidth(), 0);
glVertex2d(x + (width / 2), y - (height / 2));
glTexCoord2d(t.getWidth(), t.getHeight());
glVertex2d(x + (width / 2), y + (height / 2));
glTexCoord2d(0, t.getHeight());
glVertex2d(x - (width / 2), y + (height / 2));
}
else
{
glTexCoord2d(0, 0);
glVertex2d(x, y);
glTexCoord2d(t.getWidth(), 0);
glVertex2d(x + width, y);
glTexCoord2d(t.getWidth(), t.getHeight());
glVertex2d(x + width, y + height);
glTexCoord2d(0, t.getHeight());
glVertex2d(x, y + height);
}
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
The problem is with the second to last line (glBindTexture(GL_TEXTURE_2D, 0)). If I leave this on, the image will display for a split second (probably one frame) and then turn solid white. If I comment this out, it will display, but always displays the last texture that was loaded. Any ideas?
I think it's because you are binding your texture between the glBegin() and glEnd() calls. This is invalid. I'm guessing when you leave the glBindTexture(GL_TEXTURE_2D, 0) call off, then the binding is taking effect the next frame. Not sure why it would work for one frame, though.
You can use glGetError to see if there are any errors generated.
I'm in the process of building a isometric framework so far I've got the physical tile mapping algorithm down.
The thing is I don't want per tile collision, as in looping through all the tiles to check if my mouse is selecting a specific tile.
What I tried to accomplish is get cell index from the mouse using this.
int col = (int)(((double)mouse_x / (double)tileWidth) - ((double)mouse_y / (double)tileHeight) - 0.5);
int row = (int)(((double)mouse_y / (double)tileHeight) + ((double)mouse_x / (double)tileWidth) - 0.5);
But using this method my 0,15 is the 0,0 according to the mouse and the 0,0 is the 0,15.
So my question is how can I swap that around by editing the mouse code perhaps using offset?Even so I wouldn't know how, so my question is how.
I just went with the simple way out, instead of editing the initials equation.
I modified the outcome by:
int col = (int)(((double)mouse_x / (double)tileWidth) - ((double)mouse_y / (double)tileHeight) - 0.5);
int row = (int)(((double)mouse_y / (double)tileHeight) + ((double)mouse_x / (double)tileWidth) - 0.5);
row = mapHeight - row - 1;
This flips the outcome, and thus fixes my problem in a non-artistic equational manner.
From isometric coordinates to screen coordinates:
screenX = (isoX - isoY + isoMapMaxY) * (tileWidth / 2);
screenY = (isoX + isoY) * (tileHeight / 2);
From screen coordinates to isometric coordinates:
isoX = ( ((screenY + scrollY) / tileHeight) + ((screenX + scrollX) - (isoMapMaxY * tileWidth/2)) / tileWidth )
isoY = ( ((screenY + scrollY) / tileHeight) - ((screenX + scrollX) - (isoMapMaxY * tileWidth/2)) / tileWidth )
isoX = (screenY / 32) + (screenX - 320) / 64
isoY = (screenY / 32) - (screenX - 320) / 64
I have a sprite that moves from the bottom left of the screen to the top right. What I want to know is how would i make it turn around and go in the opposite direction, and continue this loop.
I tried negating the direction of the vector but that doesnt work.
This what i have right now:
public void create() {
// Game Initialization
v = new Vector2(Gdx.graphics.getWidth() - 0, Gdx.graphics.getHeight() - 0);
v.nor();
v.scl(100);
spriteBatch = new SpriteBatch();
bug = new Sprite(new Texture("EnemyBug.png"));
bug.setSize(50, 85);
bug.setOrigin(0,0);//Gdx.graphics.getHeight() / 5, Gdx.graphics.getHeight() / 5);
bug.setPosition(1,1);//Gdx.graphics.getWidth() - 50, Gdx.graphics.getHeight() - 50);
bug.rotate(v.angle());
rotDeg = 5;
}
#Override
public void render() {
// Game Loop
Gdx.gl.glClearColor(0.7f, 0.7f, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
if(bug.getX() >= (int)(Gdx.graphics.getWidth() - 100) && bug.getY() >= (int)(Gdx.graphics.getHeight() - 100)){
turn = !turn;
}
else if(bug.getX() <= 50 && bug.getY() <= 50){
turn = !turn;
}
if(!turn){
bug.translate(v.x * Gdx.graphics.getDeltaTime(), v.y * Gdx.graphics.getDeltaTime());
}
else{
bug.translate(-(v.x * Gdx.graphics.getDeltaTime()), -(v.y * Gdx.graphics.getDeltaTime()));
}
bug.translate(v.x * Gdx.graphics.getDeltaTime(), v.y * Gdx.graphics.getDeltaTime());
bug.draw(spriteBatch);
spriteBatch.end();
}
You are translating the sprite twice per frame:
if(!turn){
bug.translate(v.x * Gdx.graphics.getDeltaTime(), v.y * Gdx.graphics.getDeltaTime());
}
else{
bug.translate(-(v.x * Gdx.graphics.getDeltaTime()), -(v.y * Gdx.graphics.getDeltaTime()));
}
//REMOVE THIS LINE
bug.translate(v.x * Gdx.graphics.getDeltaTime(), v.y * Gdx.graphics.getDeltaTime());
That extra line may be causing your problem, nullifying your negative translation and doubling the positive one.