Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 13 Aug 2010
Offline Last Active Jan 25 2016 04:42 PM

#5250501 Tile picking with moving camera

Posted by on 03 September 2015 - 06:38 PM

I've been working on a isometric tilemap engine. Everything has been working fine up until recently when I tried implementing a moving camera. The tile picking has been off if the camera is moved. I've tried offsetting by the camera's displacement and it's still not giving the desired results. If I only move the camera along the x-axis, the tile picking works as intended, if any y-axis movement is involved, it doesn't behave correctly. I'm not sure what's going on here. Any help would be appreciated.


This method is called when a place on the game world is clicked.

There is no correction for the camera in this method at the moment, I took it out due to it not working correctly. It's simply the base method that worked before the moving camera was implemented.

The first two lines of code give me the camera's displacement along a given axis. cam.viewportWidth/2 is the default camera x coordinate. This is also applied to the Y-Axis.


If you need any more information, let me know and I can supply it.

    public void getTileAt(OrthographicCamera cam, Vector2 mouse){
        camOffsetX = cam.viewportWidth/2-cam.position.x;
        camOffsetY = -(cam.viewportHeight/2-cam.position.y);
        System.out.println(camOffsetX + ":" + camOffsetY);
        mouse.x -= X_OFFSET;
        mouse.y = Math.abs(mouse.y - Gdx.graphics.getHeight());
        Vector2 mp = isoToCart(mouse);
        Vector2 coords = new Vector2((float)Math.floor(mp.x), (float)Math.floor(mp.y));

           Tile t = new Tile("cube","redcube.png");
           layers.get(0).setTileAt(t, (int)coords.x, (int)coords.y);
        }catch(ArrayIndexOutOfBoundsException e){
            System.out.println("Coords out of bounds");

#5194536 Rendering sprite on isometric tilemap (LibGDX)

Posted by on 24 November 2014 - 10:06 PM

I've done work before with regular grids without an API and feel like I have a solid understanding on how those work. I don't want to rotate my sprite before I draw it, I want to draw it the way it is. I'm having trouble with screen placement though as it appears that they are not placed the same (I've tried). I'll include the API code for the isometric map renderer and hopefully someone will have an idea. All I want to do is for example, I want my player to be at (10, 3) on the map, what transformations will I need to do to that vector to get the actual screen coordinates.

public class IsometricTiledMapRenderer extends BatchTiledMapRenderer {

	private Matrix4 isoTransform;
	private Matrix4 invIsotransform;
	private Vector3 screenPos = new Vector3();

	private Vector2 topRight = new Vector2();
	private Vector2 bottomLeft = new Vector2();
	private Vector2 topLeft = new Vector2();
	private Vector2 bottomRight = new Vector2();

	public IsometricTiledMapRenderer (TiledMap map) {

	public IsometricTiledMapRenderer (TiledMap map, Batch batch) {
		super(map, batch);

	public IsometricTiledMapRenderer (TiledMap map, float unitScale) {
		super(map, unitScale);

	public IsometricTiledMapRenderer (TiledMap map, float unitScale, Batch batch) {
		super(map, unitScale, batch);

	private void init () {
		// create the isometric transform
		isoTransform = new Matrix4();

		// isoTransform.translate(0, 32, 0);
		isoTransform.scale((float)(Math.sqrt(2.0) / 2.0), (float)(Math.sqrt(2.0) / 4.0), 1.0f);
		isoTransform.rotate(0.0f, 0.0f, 1.0f, -45);

		// ... and the inverse matrix
		invIsotransform = new Matrix4(isoTransform);

	public void renderObject (MapObject object) {


	private Vector3 translateScreenToIso (Vector2 vec) {
		screenPos.set(vec.x, vec.y, 0);

		return screenPos;

	public void renderTileLayer (TiledMapTileLayer layer) {
		final Color batchColor = spriteBatch.getColor();
		final float color = Color.toFloatBits(batchColor.r, batchColor.g, batchColor.b, batchColor.a * layer.getOpacity());

		float tileWidth = layer.getTileWidth() * unitScale;
		float tileHeight = layer.getTileHeight() * unitScale;
		float halfTileWidth = tileWidth * 0.5f;
		float halfTileHeight = tileHeight * 0.5f;

		// setting up the screen points
		// COL1
		topRight.set(viewBounds.x + viewBounds.width, viewBounds.y);
		// COL2
		bottomLeft.set(viewBounds.x, viewBounds.y + viewBounds.height);
		// ROW1
		topLeft.set(viewBounds.x, viewBounds.y);
		// ROW2
		bottomRight.set(viewBounds.x + viewBounds.width, viewBounds.y + viewBounds.height);

		// transforming screen coordinates to iso coordinates
		int row1 = (int)(translateScreenToIso(topLeft).y / tileWidth) - 2;
		int row2 = (int)(translateScreenToIso(bottomRight).y / tileWidth) + 2;

		int col1 = (int)(translateScreenToIso(bottomLeft).x / tileWidth) - 2;
		int col2 = (int)(translateScreenToIso(topRight).x / tileWidth) + 2;

		for (int row = row2; row >= row1; row--) {
			for (int col = col1; col <= col2; col++) {
				float x = (col * halfTileWidth) + (row * halfTileWidth);
				float y = (row * halfTileHeight) - (col * halfTileHeight);

				final TiledMapTileLayer.Cell cell = layer.getCell(col, row);
				if (cell == null) continue;
				final TiledMapTile tile = cell.getTile();

				if (tile != null) {
					final boolean flipX = cell.getFlipHorizontally();
					final boolean flipY = cell.getFlipVertically();
					final int rotations = cell.getRotation();

					TextureRegion region = tile.getTextureRegion();

					float x1 = x + tile.getOffsetX() * unitScale;
					float y1 = y + tile.getOffsetY() * unitScale;
					float x2 = x1 + region.getRegionWidth() * unitScale;
					float y2 = y1 + region.getRegionHeight() * unitScale;

					float u1 = region.getU();
					float v1 = region.getV2();
					float u2 = region.getU2();
					float v2 = region.getV();

					vertices[X1] = x1;
					vertices[Y1] = y1;
					vertices[C1] = color;
					vertices[U1] = u1;
					vertices[V1] = v1;

					vertices[X2] = x1;
					vertices[Y2] = y2;
					vertices[C2] = color;
					vertices[U2] = u1;
					vertices[V2] = v2;

					vertices[X3] = x2;
					vertices[Y3] = y2;
					vertices[C3] = color;
					vertices[U3] = u2;
					vertices[V3] = v2;

					vertices[X4] = x2;
					vertices[Y4] = y1;
					vertices[C4] = color;
					vertices[U4] = u2;
					vertices[V4] = v1;

					if (flipX) {
						float temp = vertices[U1];
						vertices[U1] = vertices[U3];
						vertices[U3] = temp;
						temp = vertices[U2];
						vertices[U2] = vertices[U4];
						vertices[U4] = temp;
					if (flipY) {
						float temp = vertices[V1];
						vertices[V1] = vertices[V3];
						vertices[V3] = temp;
						temp = vertices[V2];
						vertices[V2] = vertices[V4];
						vertices[V4] = temp;
					if (rotations != 0) {
						switch (rotations) {
						case Cell.ROTATE_90: {
							float tempV = vertices[V1];
							vertices[V1] = vertices[V2];
							vertices[V2] = vertices[V3];
							vertices[V3] = vertices[V4];
							vertices[V4] = tempV;

							float tempU = vertices[U1];
							vertices[U1] = vertices[U2];
							vertices[U2] = vertices[U3];
							vertices[U3] = vertices[U4];
							vertices[U4] = tempU;
						case Cell.ROTATE_180: {
							float tempU = vertices[U1];
							vertices[U1] = vertices[U3];
							vertices[U3] = tempU;
							tempU = vertices[U2];
							vertices[U2] = vertices[U4];
							vertices[U4] = tempU;
							float tempV = vertices[V1];
							vertices[V1] = vertices[V3];
							vertices[V3] = tempV;
							tempV = vertices[V2];
							vertices[V2] = vertices[V4];
							vertices[V4] = tempV;
						case Cell.ROTATE_270: {
							float tempV = vertices[V1];
							vertices[V1] = vertices[V4];
							vertices[V4] = vertices[V3];
							vertices[V3] = vertices[V2];
							vertices[V2] = tempV;

							float tempU = vertices[U1];
							vertices[U1] = vertices[U4];
							vertices[U4] = vertices[U3];
							vertices[U3] = vertices[U2];
							vertices[U2] = tempU;
					spriteBatch.draw(region.getTexture(), vertices, 0, 20);

#5162579 java Constructor parameters

Posted by on 24 June 2014 - 11:24 AM

I would have to agree with Glass_Knife, you should favor composition over monolithic inheritance trees. Basically what that means is that an in game object(or entity) is made up of mutiple components. So for example if you had a Iron sword, it could have a wieldable component, a sword component, an iron component, it might even have a firey enchantment component.


You could code all of this into your game, but I'd suggest making it as data-driven as possible. So perhaps you could read from a text file or an XML file what each item is built of and your game would populate the world with those items. This is just a suggestion though, on smaller scale projects this may be overkill.


There's lots of good reading available on the topic of Entity-Component systems. There are a few articles on this site that go over how they work, a simple google search for "Entity-Component systems" should give you quite a bit to chew on for now.


Another good resource is the Artemis framework, so that might be something to look into(Although I'd say it's a bit beyond the scope of this project).

#5158772 Deciding to switch to C++

Posted by on 06 June 2014 - 01:52 PM

What exactly do you mean that the tools in relation to refactoring and such are more primivite


Would you say it's worth the time in learning C++ and dealing with all the learning pitfalls so I can work with better game APIs and have more support? I feel like my main issue is just that it's really hard to find a good game programming book in Java.


Also, I'm not sure whether or not I should finish my current game(It's going to take a ton of time to finish) or just switch now.



#5158757 Deciding to switch to C++

Posted by on 06 June 2014 - 12:44 PM

I've been programming in Java since grade 11 in highschool(Just finished first year university) and I've made some really good progress with the language. I've made a few simple games like the ones recommended on this site. I'm currently working on a 2d RPG in Java using LibGDX and it's going fairly well.


My concen is that there really isn't that much support for Java game development... I mean, for the level of programming I'm doing the performance difference is negligible so that really isn't a factor. But it seems that there are WAY more good APIs and engines for C++ than you can find for Java. 99% of the gamedev books I see written are intended for C++. I'm sure the concepts transfer, but it would be better to just have a book in a langauge that you'd know.


Another thing I've conisdered is that I'll have to learn C++ eventually anyways if I want to become a professional gamedev after school. Not many AAA games use Java, even runescape has left it for HTML5.


Ranting aside, what do you guys think I should do? I was considering ordering C++ Primer, then maybe Game Coding Complete and seeing where that takes me.


Any suggestions would be greatly appreciated.



#5112963 How to continue RPG?

Posted by on 29 November 2013 - 07:43 AM

add an inventory system and bags.


How much from your game is done so far? I am curious what you have realized

Well I've taken a bit of a break from the project due to school. But so far:

-I've updated the inventory system. There's all kinds of different types of items now. You can equip equipment, eat food, etc.

-There's equipment(as stated above)

-I have 2 skills(mining and woodcutting, pretty basic but they can be expanded later)

-Quests are about half done(Just need to add in different types of objectives that I can give the quests)

-NPC have been added

-NPCs were given dialogue, dialogue has trees it follows to guide the conversation/Hand out quests



That's about all I can think of at the moment. I'll post more if I remember anything additional.



This is a very good question that plenty of budding game developers hit. We actually recently wrote a teaching document that covers this issue here:




It's a bit long, but your problem is quite complex. Quite simply, you've created a nice little game engine with some features. Which don't get me wrong, is freaking awesome. So many people never get this far at all (Maybe 15-20%?). I commend your efforts.


The problem is you're not really experienced in making an entire game, you've definitely gotta learn this stuff but when professional developers make their games they don't code anything at first. They go through a long pre-production process where they design, playtest, and refine the game. Because every bug you fix BEFORE you have to code, is a whole hell of a lot less work.


I'd suggest you read the  thing  linked, it addresses the very beginning of a game project and how we went about it for our current little project. Specifically, you probably want to design out and solidify your vision for what you want your game to be, what you want it to accomplish, and let that lead into all the things you have to do to make that happen.


If you got a moment, check out our project on Kickstarter, it's specifically designed with folk like you in mind (We're trying to teach people game development!)




Thank you for your help. I'll give it a read when I get home(I'm just on my way out the door at the moment). Once I'm finished reading it, I'll post in here again and let you know what I think :)


Thanks to everyone for the tips and advice!

#5103265 Is XNA still worth learning?

Posted by on 21 October 2013 - 06:12 PM

Hey everyone, thanks for all the replies. I really appreciate all the time you guys have put in to answering my questions. I think I'm going to take the common suggestion and "wait" to see what happens for the Xbox One indie support(I saw on the microsoft site that they were planning to have something free for indies anyway).


I have a lot of experience in LibGdx and Slick2D, right now I'm working with LibGdx but I don't really like it due to poor documentation and deployment seems like a hastle.


So now my three thoughts are:

1) Work on a few projects with LibGdx and try getting a few finished, polished projects done by the time I can work on something for the Xbox One

2) Learn XNA anyways because I have free access to XNA creators club for Xbox360 due to being a student

3) Pick up a SFML book and attempt learning C++/SFML simultaneously (Not sure if good idea)


Also, this isn't something I'm going to jump on immediately. I'm working on a fairly large project with LibGdx at the moment, but I like to plan a little ways into the future.

#5090717 Implementing a component-based entity system?

Posted by on 31 August 2013 - 09:13 PM

I've been trying to implement a component-based entity system for my game items. I'm not sure if I'm doing it correctly and if there's a better way. I'll post some code, can you guys help me improve my current design?


ItemCreator Class:

import java.io.IOException;
import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.XmlReader;
import com.badlogic.gdx.utils.XmlReader.Element;

public class ItemCreator {

	XmlReader reader;
	Element root;
	int i = 0;
	ArrayList<Item> items;
	public ItemCreator(){
		items = new ArrayList<Item>();
		reader = new XmlReader();
		try {
			root = reader.parse(Gdx.files.internal("items/Items.xml"));
		} catch (IOException e) {
		Array<Element> items = root.getChildrenByName("item");
		//Loop through each "Item"
		for (Element child : items){
			int ID = Integer.parseInt(child.getAttribute("id"));
			String name = child.getAttribute("name");
			//Loop through each Item's components
			int numberOfChildren = child.getChildCount();
			Array<Element> components = new Array<Element>();
			for(int i = 0; i < numberOfChildren; i++){
	public void createItem(Array<Element> components, int ID, String name){
		Item item = new Item();
		//add components
		for(Element component : components){
			if(component.getName().equals("description")){item.addComponent(new ItemDescription(component.getAttribute("text")));}
			if(component.getName().equals("wieldable")){item.addComponent(new ItemWieldable(component.getAttribute("slot")));}
			if(component.getName().equals("weapon")){item.addComponent(new ItemWeapon(component.getAttribute("damage")));}
		//Add item to itemList

Item Class:

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;

public class Item {

	ArrayList<ItemComponent> components;
	String name;
	int ID;
	public Item() {
		components = new ArrayList<ItemComponent>();
	public void addComponent(ItemComponent component){
	public void printItem(){
		Gdx.app.log("Name", name);
		for(ItemComponent c : components){
	public String getName() {
		return name;

	public void setName(String name) {
		this.name = name;

	public int getID() {
		return ID;

	public void setID(int iD) {
		ID = iD;

Thanks for any suggestions.

#5089703 [LibGdx] Parsing XML

Posted by on 27 August 2013 - 10:32 PM

I've got it figured out.

		//Loop through each "Item"
		for (Element child : items){
			//Loop through each Item's components
			int numberOfChildren = child.getChildCount();
			Array<Element> components = new Array<Element>();
			for(int i = 0; i < numberOfChildren; i++){

#5087710 YAML vs JSON vs XML?

Posted by on 20 August 2013 - 06:39 PM

I think I'll stick to XML like was suggested above. No need to learn extra technologies to achieve something that can be done with a protocol  that I already know.


Does this look like a good structure to you? Or is there a better way?

<?xml version="1.0"?>

#5087686 YAML vs JSON vs XML?

Posted by on 20 August 2013 - 04:32 PM

I'm working on a 2D RPG in Java using LibGdx, and need to find a way to store my game data. The things I've considered are YAML, JSON and XML. I only have experience in working with XML, but the others can't be too hard to learn. What I need to achieve is being able to define items to be generated by listing the components that the item would have and my EntityGenerator would turn them into game objects.


Additionally, I would like to be able to build myself an "Item creator" utility where I can use a GUI to simply check off the components to add for my item and it would add that into the data file.


Any suggestions?

#5085276 How to structure a 2d RPG?

Posted by on 12 August 2013 - 01:50 PM

My goal is to write a top-down 2D RPG(Similar to runescape classic, but much simpler) in Java using LibGdx. My main question is how I should structure my game? I've made a previous thread asking about Component-Entity Systems and that seems like a good option, but I want to explore my choices.


I want to be able to have a few XML or Json files with most of the game content in it, and I could read all the data into my game and have it create the required game objects.


So back to my main question, how should I be structuring this game? I would really appreciate any help or advice anyone can give me.



#5059604 Java and graphics APIs

Posted by on 05 May 2013 - 06:33 PM

Slick2d/LWJGL is a good option.

#5048237 Breakout collision problem?

Posted by on 30 March 2013 - 12:10 AM

This is my code for the ball colliding with the paddle in my game. As of right now, if the ball approaches the paddle and hits it on the corner, the ball seems to get trapped inside of the paddle. Any suggestions?


I'm using the Slick2d API if it makes a difference.

		if (ball.ballCircle.intersects(paddle.paddleRect)) {
			if(ball.ballCircle.getMaxX() <= paddle.paddleRect.getMinX()){
			if( ball.ballCircle.getMinX() >= paddle.paddleRect.getMaxX()){
			if(ball.ballCircle.getMaxY() <= paddle.paddleRect.getMinY()){

#5048214 Vector collision

Posted by on 29 March 2013 - 09:41 PM

What's a good way to find out which side of the brick I'm colliding with?