Just store the position in the entity object as well. For dynamic objects I am using both arrays and I store the position of the object inside the object as well for various reasons. One reason is to use the array for drawing, if I only want to draw from coordinate (10,10) to (26, 19) because my screen does not reach the others anyway I can use the various arrays. But if I have an object and want to know it's position I have to iterate the whole array if I do not store it inside it.
Sometimes I just use vectors for storing the position but a much better way is to create your own point/coordinate class with equals and hashtag overrides to compare. You can put (static) methods in this class to convert from screen and camera coordinates which are often vectors to your points for easy shifting between them.
You know what? Here is a basic one I use:
public class Point {
public int x;
public int y;
/**
* Creates point
* @param x X - coordinate
* @param y Y - coordinate
*/
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
/**
* Creates point from world vector
* @param v World vector
*/
public Point(Vector2 v)
{
this.x = (int)(v.x / tileWidth);
this.y = (int)(v.y / tileHeight);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
if (x != point.x) return false;
if (y != point.y) return false;
return true;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
@Override
public String toString() {
return "Point: (" + x + ", " + y;
}
/**
* Returns the world coorinate of the point.
* @return world vector.
*/
public Vector2 toWorld()
{
return new Vector2(x * tileWidth, y* tileHeight);
}
/**
* Returns point from world vector
* @param v World vector
* @return
*/
public static Point getPoint(Vector2 v)
{
return new Point((int)(v.x / tileWidth), (int)(v.y / tileHeight));
}
/**
* Returns point from Vector3 where Z is discarded.
* @param v world vector
* @return
*/
public static Point getPoint(Vector3 v)
{
return new Point((int)(v.x / tileWidth), (int)(v.y / tileHeight));
}
}
The only thing you need is to hookup tileWidth
and tileHeight
to your variables.