java - libgdx animation inside of a rectangle - collison detection - rectangles -



java - libgdx animation inside of a rectangle - collison detection - rectangles -

i writing rpg game similar style of pokemon (top downwards view). working on issue of collision detection. wanting create collision detection based on rectangles. problem having difficulty drawing rectangle around animation have set. have searched google , youtube answer/tutorial of how handle problem yet found nothing.

player.class

public class player { public vector2 position; private float movespeed; private spritebatch batch; //animation public animation an; private texture tex; public textureregion currentframe; private textureregion[][] frames; private float frametime; public player(vector2 position){ this.position = position; movespeed = 5f; createplayer(); } public void createplayer(){ tex = new texture("sprites/image.png"); frames = textureregion.split(tex, tex.getwidth()/3, tex.getheight()/4); = new animation(0.10f, frames[0]); } public void render(float delta){ handleinput(); frametime += delta; currentframe = an.getkeyframe(frametime, true); } public void handleinput(){ if(gdx.input.iskeypressed(keys.up)){ = new animation(0.10f, frames[3]); position.y += movespeed; } if(gdx.input.iskeypressed(keys.down)){ = new animation(0.10f, frames[0]); position.y -= movespeed; } if(gdx.input.iskeypressed(keys.left)){ = new animation(0.10f, frames[1]); position.x -= movespeed; } if(gdx.input.iskeypressed(keys.right)){ = new animation(0.10f, frames[2]); position.x += movespeed; } if(!gdx.input.iskeypressed(keys.any_key)){ = new animation(0.10f, frames[0]); } } public void dispose(){ batch.dispose(); } public float getx(){ homecoming position.x; } public float gety(){ homecoming position.y; } public int getwidth(){ homecoming 32; } public int getheight(){ homecoming 32; } }

worldrenderer.class

public class worldrenderer { private orthogonaltiledmaprenderer renderer; public orthographiccamera camera; private player player; //tilemap private tiledmaptilelayer collision; private tiledmap map; public worldrenderer() { map = new tmxmaploader().load("maps/testmap.tmx"); renderer = new orthogonaltiledmaprenderer(map); //tiled layers collision = (tiledmaptilelayer) map.getlayers().get("collision"); player = new player(new vector2(100, 100)); photographic camera = new orthographiccamera(); camera.viewportwidth = gdx.graphics.getwidth()/2; camera.viewportheight = gdx.graphics.getheight()/2; } public void render (float delta) { camera.update(); renderer.setview(camera); renderer.render(); player.render(delta); // calculate tile size in pixels mapproperties prop = renderer.getmap().getproperties(); int mapwidth = prop.get("width", integer.class); //how many tiles in map int mapheight = prop.get("height", integer.class); int tilepixelwidth = prop.get("tilewidth", integer.class); //size of each tile int tilepixelheight = prop.get("tileheight", integer.class); // calculate total map size int worldsizex = mapwidth * tilepixelwidth; int worldsizey = mapheight * tilepixelheight; // calculate min/max photographic camera points within map float mincamerax = camera.zoom * (camera.viewportwidth / 2); float maxcamerax = worldsizex - mincamerax; float mincameray = camera.zoom * (camera.viewportheight / 2); float maxcameray = worldsizey - mincameray; // set photographic camera either player or min/max of photographic camera based on player position camera.position.set( math.min(maxcamerax, math.max(player.position.x + 32 / 2, mincamerax)), math.min(maxcameray, math.max(player.position.y + 32 / 2, mincameray)), 0); camera.update(); renderer.getspritebatch().setprojectionmatrix(camera.combined); renderer.getspritebatch().begin(); renderer.getspritebatch().draw(player.currentframe, player.position.x, player.position.y); renderer.getspritebatch().end(); } }

if want observe collision between 2 rectangles, there illustration used sprites.

public class testspriteone extends sprite{ private rectangle rectangle;

add in constructor.

rectangle = new rectangle(getx(), gety(), getwidth(), getheight());

in update method

rectangle.setposition(getx(), gety());

is new nethod

public rectangle getcollideractor(){ homecoming this.rectangle; }

other classes

public class testspritetwo extends sprite{ private rectangle rectangle;

in constructor.

rectangle = new rectangle(getx(), gety(), getwidth(), getheight());

in update method

rectangle.setposition(getx(), gety());

in new method

public rectangle getcollideractor(){ homecoming this.rectangle; }

// phone call in testspriteone class

boolean hascollided = testspritetwo.getcollideractor().overlaps(getcollideractor());

//call in gamescreen class

boolean hascollided = testspriteone.getcollideractor().overlaps(testspritetwo.getcollideractor());

it overlaps whether rectangle overlaps other rectangle, hascollided variable become true.

edit: if animation changes width or height, resize rectangle in update method

rectangle.setsize (width, height);

java libgdx collision topdown

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -