Java Code Examples for com.badlogic.gdx.math.Vector2#add()

The following examples show how to use com.badlogic.gdx.math.Vector2#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LevelLoader.java    From ud406 with MIT License 6 votes vote down vote up
private static void loadNonPlatformEntities(Level level, JSONArray nonPlatformObjects) {
    for (Object o : nonPlatformObjects) {
        JSONObject item = (JSONObject) o;

        final Vector2 imagePosition = extractXY(item);

        if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.POWERUP_SPRITE)) {
            final Vector2 powerupPosition = imagePosition.add(Constants.POWERUP_CENTER);
            Gdx.app.log(TAG, "Loaded a powerup at " + powerupPosition);
            level.getPowerups().add(new Powerup(powerupPosition));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.STANDING_RIGHT)) {
            final Vector2 gigaGalPosition = imagePosition.add(Constants.GIGAGAL_EYE_POSITION);
            Gdx.app.log(TAG, "Loaded GigaGal at " + gigaGalPosition);
            level.setGigaGal(new GigaGal(gigaGalPosition, level));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.EXIT_PORTAL_SPRITE_1)) {
            final Vector2 exitPortalPosition = imagePosition.add(Constants.EXIT_PORTAL_CENTER);
            Gdx.app.log(TAG, "Loaded the exit portal at " + exitPortalPosition);
            level.setExitPortal(new ExitPortal(exitPortalPosition));
        }

    }
}
 
Example 2
Source File: LevelLoader.java    From ud406 with MIT License 6 votes vote down vote up
private static void loadNonPlatformEntities(Level level, JSONArray nonPlatformObjects) {
    for (Object o : nonPlatformObjects) {
        JSONObject item = (JSONObject) o;

        final Vector2 imagePosition = extractXY(item);

        if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.POWERUP_SPRITE)) {
            final Vector2 powerupPosition = imagePosition.add(Constants.POWERUP_CENTER);
            Gdx.app.log(TAG, "Loaded a powerup at " + powerupPosition);
            level.getPowerups().add(new Powerup(powerupPosition));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.STANDING_RIGHT)) {
            final Vector2 gigaGalPosition = imagePosition.add(Constants.GIGAGAL_EYE_POSITION);
            Gdx.app.log(TAG, "Loaded GigaGal at " + gigaGalPosition);
            level.setGigaGal(new GigaGal(gigaGalPosition, level));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.EXIT_PORTAL_SPRITE_1)) {
            final Vector2 exitPortalPosition = imagePosition.add(Constants.EXIT_PORTAL_CENTER);
            Gdx.app.log(TAG, "Loaded the exit portal at " + exitPortalPosition);
            level.setExitPortal(new ExitPortal(exitPortalPosition));
        }

    }
}
 
Example 3
Source File: LevelLoader.java    From ud406 with MIT License 6 votes vote down vote up
private static void loadNonPlatformEntities(Level level, JSONArray nonPlatformObjects) {
    for (Object o : nonPlatformObjects) {
        JSONObject item = (JSONObject) o;

        final Vector2 imagePosition = extractXY(item);

        if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.POWERUP_SPRITE)) {
            final Vector2 powerupPosition = imagePosition.add(Constants.POWERUP_CENTER);
            Gdx.app.log(TAG, "Loaded a powerup at " + powerupPosition);
            level.getPowerups().add(new Powerup(powerupPosition));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.STANDING_RIGHT)) {
            final Vector2 gigaGalPosition = imagePosition.add(Constants.GIGAGAL_EYE_POSITION);
            Gdx.app.log(TAG, "Loaded GigaGal at " + gigaGalPosition);
            level.setGigaGal(new GigaGal(gigaGalPosition, level));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.EXIT_PORTAL_SPRITE_1)) {
            final Vector2 exitPortalPosition = imagePosition.add(Constants.EXIT_PORTAL_CENTER);
            Gdx.app.log(TAG, "Loaded the exit portal at " + exitPortalPosition);
            level.setExitPortal(new ExitPortal(exitPortalPosition));
        }

    }
}
 
Example 4
Source File: LevelLoader.java    From ud406 with MIT License 6 votes vote down vote up
private static void loadNonPlatformEntities(Level level, JSONArray nonPlatformObjects) {
    for (Object o : nonPlatformObjects) {
        JSONObject item = (JSONObject) o;

        final Vector2 imagePosition = extractXY(item);

        if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.POWERUP_SPRITE)) {
            final Vector2 powerupPosition = imagePosition.add(Constants.POWERUP_CENTER);
            Gdx.app.log(TAG, "Loaded a powerup at " + powerupPosition);
            level.getPowerups().add(new Powerup(powerupPosition));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.STANDING_RIGHT)) {
            final Vector2 gigaGalPosition = imagePosition.add(Constants.GIGAGAL_EYE_POSITION);
            Gdx.app.log(TAG, "Loaded GigaGal at " + gigaGalPosition);
            level.setGigaGal(new GigaGal(gigaGalPosition, level));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.EXIT_PORTAL_SPRITE_1)) {
            final Vector2 exitPortalPosition = imagePosition.add(Constants.EXIT_PORTAL_CENTER);
            Gdx.app.log(TAG, "Loaded the exit portal at " + exitPortalPosition);
            level.setExitPortal(new ExitPortal(exitPortalPosition));
        }

    }
}
 
Example 5
Source File: LevelLoader.java    From ud406 with MIT License 6 votes vote down vote up
private static void loadNonPlatformEntities(Level level, JSONArray nonPlatformObjects) {
    for (Object o : nonPlatformObjects) {
        JSONObject item = (JSONObject) o;

        final Vector2 imagePosition = extractXY(item);

        if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.POWERUP_SPRITE)) {
            final Vector2 powerupPosition = imagePosition.add(Constants.POWERUP_CENTER);
            Gdx.app.log(TAG, "Loaded a powerup at " + powerupPosition);
            level.getPowerups().add(new Powerup(powerupPosition));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.STANDING_RIGHT)) {
            final Vector2 gigaGalPosition = imagePosition.add(Constants.GIGAGAL_EYE_POSITION);
            Gdx.app.log(TAG, "Loaded GigaGal at " + gigaGalPosition);
            level.setGigaGal(new GigaGal(gigaGalPosition, level));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.EXIT_PORTAL_SPRITE_1)) {
            final Vector2 exitPortalPosition = imagePosition.add(Constants.EXIT_PORTAL_CENTER);
            Gdx.app.log(TAG, "Loaded the exit portal at " + exitPortalPosition);
            level.setExitPortal(new ExitPortal(exitPortalPosition));
        }

    }
}
 
Example 6
Source File: LevelLoader.java    From ud406 with MIT License 6 votes vote down vote up
private static void loadNonPlatformEntities(Level level, JSONArray nonPlatformObjects) {
    for (Object o : nonPlatformObjects) {
        JSONObject item = (JSONObject) o;

        final Vector2 imagePosition = extractXY(item);

        if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.POWERUP_SPRITE)) {
            final Vector2 powerupPosition = imagePosition.add(Constants.POWERUP_CENTER);
            Gdx.app.log(TAG, "Loaded a powerup at " + powerupPosition);
            level.getPowerups().add(new Powerup(powerupPosition));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.STANDING_RIGHT)) {
            final Vector2 gigaGalPosition = imagePosition.add(Constants.GIGAGAL_EYE_POSITION);
            Gdx.app.log(TAG, "Loaded GigaGal at " + gigaGalPosition);
            level.setGigaGal(new GigaGal(gigaGalPosition, level));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.EXIT_PORTAL_SPRITE_1)) {
            final Vector2 exitPortalPosition = imagePosition.add(Constants.EXIT_PORTAL_CENTER);
            Gdx.app.log(TAG, "Loaded the exit portal at " + exitPortalPosition);
            level.setExitPortal(new ExitPortal(exitPortalPosition));
        }

    }
}
 
Example 7
Source File: LevelLoader.java    From ud406 with MIT License 6 votes vote down vote up
private static void loadNonPlatformEntities(Level level, JSONArray nonPlatformObjects) {
    for (Object o : nonPlatformObjects) {
        JSONObject item = (JSONObject) o;

        final Vector2 imagePosition = extractXY(item);

        if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.POWERUP_SPRITE)) {
            final Vector2 powerupPosition = imagePosition.add(Constants.POWERUP_CENTER);
            Gdx.app.log(TAG, "Loaded a powerup at " + powerupPosition);
            level.getPowerups().add(new Powerup(powerupPosition));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.STANDING_RIGHT)) {
            final Vector2 gigaGalPosition = imagePosition.add(Constants.GIGAGAL_EYE_POSITION);
            Gdx.app.log(TAG, "Loaded GigaGal at " + gigaGalPosition);
            level.setGigaGal(new GigaGal(gigaGalPosition, level));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.EXIT_PORTAL_SPRITE_1)) {
            final Vector2 exitPortalPosition = imagePosition.add(Constants.EXIT_PORTAL_CENTER);
            Gdx.app.log(TAG, "Loaded the exit portal at " + exitPortalPosition);
            level.setExitPortal(new ExitPortal(exitPortalPosition));
        }

    }
}
 
Example 8
Source File: LevelLoader.java    From ud406 with MIT License 6 votes vote down vote up
private static void loadNonPlatformEntities(Level level, JSONArray nonPlatformObjects) {
    for (Object o : nonPlatformObjects) {
        JSONObject item = (JSONObject) o;

        final Vector2 imagePosition = extractXY(item);

        if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.POWERUP_SPRITE)) {
            final Vector2 powerupPosition = imagePosition.add(Constants.POWERUP_CENTER);
            Gdx.app.log(TAG, "Loaded a powerup at " + powerupPosition);
            level.getPowerups().add(new Powerup(powerupPosition));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.STANDING_RIGHT)) {
            final Vector2 gigaGalPosition = imagePosition.add(Constants.GIGAGAL_EYE_POSITION);
            Gdx.app.log(TAG, "Loaded GigaGal at " + gigaGalPosition);
            level.setGigaGal(new GigaGal(gigaGalPosition, level));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.EXIT_PORTAL_SPRITE_1)) {
            final Vector2 exitPortalPosition = imagePosition.add(Constants.EXIT_PORTAL_CENTER);
            Gdx.app.log(TAG, "Loaded the exit portal at " + exitPortalPosition);
            level.setExitPortal(new ExitPortal(exitPortalPosition));
        }

    }
}
 
Example 9
Source File: Scene2dUtils.java    From gdx-vfx with Apache License 2.0 6 votes vote down vote up
public static Vector2 setPositionRelative(Actor srcActor, int srcAlign, Actor dstActor, int dstAlign, float dstX, float dstY, boolean round) {
    Vector2 pos = tmpVec2.set(srcActor.getX(srcAlign), srcActor.getY(srcAlign));

    if ((dstAlign & right) != 0)
        pos.x -= dstActor.getWidth();
    else if ((dstAlign & left) == 0)
        pos.x -= dstActor.getWidth() / 2;

    if ((dstAlign & top) != 0)
        pos.y -= dstActor.getHeight();
    else if ((dstAlign & bottom) == 0)
        pos.y -= dstActor.getHeight() / 2;

    pos.add(dstX, dstY);

    if (round) {
        pos.set(pos.x, pos.y);
    }
    dstActor.setPosition(pos.x, pos.y);
    return pos;
}
 
Example 10
Source File: LevelLoader.java    From ud406 with MIT License 6 votes vote down vote up
private static void loadNonPlatformEntities(Level level, JSONArray nonPlatformObjects) {
    for (Object o : nonPlatformObjects) {
        JSONObject item = (JSONObject) o;

        final Vector2 imagePosition = extractXY(item);

        if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.POWERUP_SPRITE)) {
            final Vector2 powerupPosition = imagePosition.add(Constants.POWERUP_CENTER);
            Gdx.app.log(TAG, "Loaded a powerup at " + powerupPosition);
            level.getPowerups().add(new Powerup(powerupPosition));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.STANDING_RIGHT)) {
            final Vector2 gigaGalPosition = imagePosition.add(Constants.GIGAGAL_EYE_POSITION);
            Gdx.app.log(TAG, "Loaded GigaGal at " + gigaGalPosition);
            level.setGigaGal(new GigaGal(gigaGalPosition, level));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.EXIT_PORTAL_SPRITE_1)) {
            final Vector2 exitPortalPosition = imagePosition.add(Constants.EXIT_PORTAL_CENTER);
            Gdx.app.log(TAG, "Loaded the exit portal at " + exitPortalPosition);
            level.setExitPortal(new ExitPortal(exitPortalPosition));
        }

    }
}
 
Example 11
Source File: LevelLoader.java    From ud406 with MIT License 6 votes vote down vote up
private static void loadNonPlatformEntities(Level level, JSONArray nonPlatformObjects) {
    for (Object o : nonPlatformObjects) {
        JSONObject item = (JSONObject) o;

        final Vector2 imagePosition = extractXY(item);

        if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.POWERUP_SPRITE)) {
            final Vector2 powerupPosition = imagePosition.add(Constants.POWERUP_CENTER);
            Gdx.app.log(TAG, "Loaded a powerup at " + powerupPosition);
            level.getPowerups().add(new Powerup(powerupPosition));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.STANDING_RIGHT)) {
            final Vector2 gigaGalPosition = imagePosition.add(Constants.GIGAGAL_EYE_POSITION);
            Gdx.app.log(TAG, "Loaded GigaGal at " + gigaGalPosition);
            level.setGigaGal(new GigaGal(gigaGalPosition, level));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.EXIT_PORTAL_SPRITE_1)) {
            final Vector2 exitPortalPosition = imagePosition.add(Constants.EXIT_PORTAL_CENTER);
            Gdx.app.log(TAG, "Loaded the exit portal at " + exitPortalPosition);
            level.setExitPortal(new ExitPortal(exitPortalPosition));
        }

    }
}
 
Example 12
Source File: LevelLoader.java    From ud406 with MIT License 6 votes vote down vote up
private static void loadNonPlatformEntities(Level level, JSONArray nonPlatformObjects) {
    for (Object o : nonPlatformObjects) {
        JSONObject item = (JSONObject) o;

        final Vector2 imagePosition = extractXY(item);

        if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.POWERUP_SPRITE)) {
            final Vector2 powerupPosition = imagePosition.add(Constants.POWERUP_CENTER);
            Gdx.app.log(TAG, "Loaded a powerup at " + powerupPosition);
            level.getPowerups().add(new Powerup(powerupPosition));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.STANDING_RIGHT)) {
            final Vector2 gigaGalPosition = imagePosition.add(Constants.GIGAGAL_EYE_POSITION);
            Gdx.app.log(TAG, "Loaded GigaGal at " + gigaGalPosition);
            level.setGigaGal(new GigaGal(gigaGalPosition, level));
        } else if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.EXIT_PORTAL_SPRITE_1)) {
            final Vector2 exitPortalPosition = imagePosition.add(Constants.EXIT_PORTAL_CENTER);
            Gdx.app.log(TAG, "Loaded the exit portal at " + exitPortalPosition);
            level.setExitPortal(new ExitPortal(exitPortalPosition));
        }

    }
}
 
Example 13
Source File: SinglePlayer.java    From uracer-kotd with Apache License 2.0 6 votes vote down vote up
@Override
public void updateCameraPosition (Vector2 positionPx) {
	if (hasPlayer()) {
		// update player's headlights and move the world camera to follows it, if there is a player
		if (gameWorld.isNightMode()) {
			gameWorldRenderer.updatePlayerHeadlights(playerCar);
		}
		positionPx.set(playerCar.state().position);
		if (!paused) {
			positionPx.add(camShaker.compute(getCollisionFactor()));
			positionPx.add(camShaker.compute(getOutOfTrackFactor()));
		}
	} else if (isGhostActive(0)) {
		// FIXME use available/choosen replay
		positionPx.set(getGhost(0).state().position);
	} else {
		// no ghost, no player, WTF?
		positionPx.set(Convert.mt2px(gameWorld.playerStart.position));
	}
}
 
Example 14
Source File: CalibrationActivity.java    From tilt-game-android with MIT License 5 votes vote down vote up
private void calculateAverageGravity() {
    Vector2 gravity = Vector2Pool.obtain();

    for (int i = 10; i < _gravityPoints.size(); i++) {
        gravity.add(_gravityPoints.get(i));
    }
    gravity.mul(-1.0f / _gravityPoints.size());

    Prefs.putFloat(PrefKeys.CALIBRATION_X, gravity.x);
    Prefs.putFloat(PrefKeys.CALIBRATION_Y, gravity.y);
}
 
Example 15
Source File: ChunkManager.java    From TerraLegion with MIT License 5 votes vote down vote up
/**
 * Recursive method that searches in a specified direction for a block.
 *
 * @param position The last position checked
 * @param direction The direction to search
 * @param origin The original position to check against the maxReach
 * @param maxReach The max pixels that the search can go
 * @return The tile position of a block if found; null if nothing found within reach
 */
private Vector2 findFarthestAirBlock(Vector2 position, Vector2 direction, Vector2 origin, float maxReach, float xCount, float yCount, Vector2 farthestAirFound) {
	xCount += direction.x; //Add direction
	yCount += direction.y; //Add direction
	boolean newVal = false; //Flag to see if we switch blocks
	if (xCount >= 1 || xCount <= -1) { //If we went left or right of the last tile
		xCount = 0; //reset
		position.add((direction.x < 0 ? -1 : 1) * ChunkManager.TILE_SIZE, 0); //Go over a tile
		newVal = true; //set flag
	}
	if (yCount >= 1 || yCount <= -1) { //If we went up or down from the last tile
		yCount = 0; //reset
		position.add(0, (direction.y < 0 ? -1 : 1) * ChunkManager.TILE_SIZE); //Go vertical
		newVal = true; //set flag
	}

	if (origin.dst(position) > maxReach) { //If we're too far from the origin then return air location if we found it
		return farthestAirFound;
	}

	if (!newVal) { //If we didn't change blocks we're checking, go to the next revolution
		return findFarthestAirBlock(position, direction, origin, maxReach, xCount, yCount, farthestAirFound);
	}

	int tx = pixelToTilePosition(position.x);
	int ty = pixelToTilePosition(position.y);

	BlockType type = getBlockFromTilePos(tx, ty);
	if (type == BlockType.AIR) { // we found air!
		if (farthestAirFound == null)
			farthestAirFound = Vector2Factory.instance.getVector2(tx, ty); //Set the air block and keep searching for farther air blocks
		else
			farthestAirFound.set(tx, ty);
		return findFarthestAirBlock(position, direction, origin, maxReach, xCount, yCount, farthestAirFound); //Keep searching
	} else {
		return farthestAirFound;
	}
}
 
Example 16
Source File: Piece.java    From Klooni1010 with GNU General Public License v3.0 5 votes vote down vote up
Vector2 calculateGravityCenter() {
    int filledCount = 0;
    Vector2 result = new Vector2();
    for (int i = 0; i < cellRows; ++i) {
        for (int j = 0; j < cellCols; ++j) {
            if (shape[i][j]) {
                filledCount++;
                result.add(
                        pos.x + j * cellSize - cellSize * 0.5f,
                        pos.y + i * cellSize - cellSize * 0.5f);
            }
        }
    }
    return result.scl(1f / filledCount);
}
 
Example 17
Source File: IsometricCamera.java    From riiablo with Apache License 2.0 4 votes vote down vote up
/**
 * Converts tile coords to screen coords.
 */
public Vector2 toScreen(float x, float y, Vector2 dst) {
  EngineUtils.worldToScreenCoords(x, y, dst);
  return dst.add(pixOffset);
}
 
Example 18
Source File: ChunkManager.java    From TerraLegion with MIT License 4 votes vote down vote up
/**
 * Recursive method that searches in a specified direction for a block.
 *
 * @param position The last position checked
 * @param direction The direction to search
 * @param origin The original position to check against the maxReach
 * @param maxReach The max pixels that the search can go
 * @return The tile position of a block if found; null if nothing found within reach
 */
private Vector2 findBlock(ToolItem tool, Vector2 position, Vector2 direction, Vector2 origin, float maxReach, float xCount, float yCount) {
	xCount += direction.x; //Add direction
	yCount += direction.y; //Add direction
	boolean newVal = false; //Flag to see if we switch blocks

	if (xCount >= 1 || xCount <= -1) { //If we went left or right of the last tile
		xCount = 0; //reset
		position.add((direction.x < 0 ? -1 : 1) * ChunkManager.TILE_SIZE, 0); //Go over a tile
		newVal = true; //set flag
	}

	if (yCount >= 1 || yCount <= -1) { //If we went up or down from the last tile
		yCount = 0; //reset
		position.add(0, (direction.y < 0 ? -1 : 1) * ChunkManager.TILE_SIZE); //Go vertical
		newVal = true; //set flag
	}

	if (origin.dst(position) > maxReach) { //If we're too far from the origin then return nothing
		return null;
	}

	if (!newVal) { //If we didn't change blocks we're checking, go to the next revolution
		return findBlock(tool, position, direction, origin, maxReach, xCount, yCount);
	}

	int tx = pixelToTilePosition(position.x);
	int ty = pixelToTilePosition(position.y);

	BlockType type = getBlockFromTilePos(tx, ty);
	if ((tool != null && type != BlockType.AIR)) { // we found a block
		if (tool.canDamageBlock(type)) {
			return Vector2Factory.instance.getVector2(tx, ty); //Return the tile position
		} else {
			return null;
		}
	} else if (tool == null && type != BlockType.AIR) { //Doing a search without a tool bounding the selection
		return Vector2Factory.instance.getVector2(tx, ty); //Return the tile position
	} else {
		return findBlock(tool, position, direction, origin, maxReach, xCount, yCount); //Keep searching
	}
}
 
Example 19
Source File: PolygonIntersector.java    From Codelabs with MIT License 4 votes vote down vote up
public static PolygonProperties computePolygonProperties(Vector2[] polygon) {
	PolygonProperties polygonProperties = null;

	int count = polygon.length;

	if (count >= 3) {
		Vector2 centroid = new Vector2(0, 0);
		float area = 0;

		Vector2 refPoint = new Vector2(0, 0);
		float threeInverse = 1 / 3f;

		for (int i = 0; i < count; i++) {
			/*
			 * Create a new vector to represent the reference point for
			 * forming triangles. Then use refPoint, polygonVertex and
			 * thirdTriangleVertex as vertices of a triangle.
			 */
			refPoint.set(0, 0);
			Vector2 polygonVertex = polygon[i];
			Vector2 thirdTriangleVertex = i + 1 < count ? polygon[i + 1]
					: polygon[0];

			Vector2 firstDirectionVector = polygonVertex.sub(refPoint);
			Vector2 secondDirectionVector = thirdTriangleVertex
					.sub(refPoint);

			float triangleArea = firstDirectionVector
					.crs(secondDirectionVector) / 2;
			area += triangleArea;

			/* Area weighted centroid */
			centroid.add(refPoint.add(polygonVertex)
					.add(thirdTriangleVertex)
					.scl(triangleArea * threeInverse));
		}

		if (area > EPSILON) {
			centroid.scl(1 / area);
		} else {
			area = 0;
		}

		polygonProperties = new PolygonProperties(centroid, area);
	}

	return polygonProperties;
}
 
Example 20
Source File: LevelLoader.java    From ud406 with MIT License 3 votes vote down vote up
private static void loadNonPlatformEntities(Level level, JSONArray nonPlatformObjects) {
    for (Object o : nonPlatformObjects) {

        // First we need to cast the object to a JSONObject
        JSONObject item = (JSONObject) o;

        // TODO: Get the lower left corner of the object
        // Remember to use safeGetFloat()
        Vector2 lowerLeftCorner = new Vector2();


        // Check if this object is GigaGal
        if (item.get(Constants.LEVEL_IMAGENAME_KEY).equals(Constants.STANDING_RIGHT)) {

            // If so, add GigaGal's eye position to find her spawn position
            final Vector2 gigaGalPosition = lowerLeftCorner.add(Constants.GIGAGAL_EYE_POSITION);
            Gdx.app.log(TAG, "Loaded GigaGal at " + gigaGalPosition);

            // Add our new GigaGal to the level
            level.setGigaGal(new GigaGal(gigaGalPosition, level));
        }

        // TODO: Go through the same process to load the Exit Portal


        // TODO: Load the Powerups

    }
}