/** * Attempts to add an image to this {@link GLTexture} * * @param image The {@link Image} to add * @return The resulting {@link Texture}, or <code>null</code> if it didn't fit */ public Texture addImage(Image image) { assert image.format == format; RectanglePacker.Rectangle rpr = packer.insert(image.getWidth(), image.getHeight(), image); if (rpr != null) { assert rpr.width == image.getWidth(); assert rpr.height == image.getHeight(); writeToTexture(rpr, image.getData()); Vector2f bottomleft = new Vector2f(rpr.x, rpr.y); Vector2f topRight = new Vector2f(rpr.x + rpr.width, rpr.y + rpr.height); bottomleft.x /= size.getWidth(); bottomleft.y /= size.getHeight(); topRight.x /= size.getWidth(); topRight.y /= size.getHeight(); Texture t = new Texture(this, bottomleft, topRight, image); residentTextures.add(t); return t; } else { return null; } }
private Vector2f converttoimagesizes( Vector2f oldvec, Vector2f offset, BufferedImage oldimage, BufferedImage newimage) { Vector2f vec = new Vector2f(); vec.x = oldvec.x * oldimage.getWidth(); vec.x += offset.x; vec.x /= newimage.getWidth(); vec.y = oldvec.y * oldimage.getHeight(); vec.y += offset.y; vec.y /= newimage.getHeight(); return vec; }
// Depending on the degrees at which this projectile was fired, the X and Y speeds can // be calculated using math... private void calculateSpeed(float degrees) { // Using simple trigonometry we can calculate the sides of a triangle by given (shooting) angel // and // the length of the hypotenuse, which we will keep as 1 for simplicity's sake // sin(a) = opposite / hypotenuse // cos(a) = adjacent / hypotenuse // -> lengths of the opposites and adjacent sides can be calculated by using the following // formula // (Hypotenuse is 1, so we can disregard it completely) // opposite = sin(a) / 1 // adjacent = cos(a) / 1 speed.x = (float) Math.cos(Math.toRadians(degrees)) * 20; speed.y = (float) Math.sin(Math.toRadians(degrees)) * 20; }
private void calculateOrigin(int x, int y, float degrees) { // this is the middle point of the tank (and turret) from where the projectile was shot // since the projectile has to originate from the end of the turret and not the middle // point, we can calculate its actual starting position by knowing the angle (degrees) // and the size of the tank (radius of drawing circle) which is 150 // Note: The hypotenuse is equal to the radius of the drawing circle, which is 150 // -> Delta Y is then defined by sin(degrees) = opposite / 150 // -> opposite = sin(degrees)*150 (and we need to add 150 because the drawing circle // originates from the screen's 0/0 and not the middle of the circle) // Delta X is the same as above, but with using cos(degrees) instead // we use 175 here to account for drawing over the turret, which we dont want to do position.x = x + (float) Math.cos(Math.toRadians(degrees)) * 175 + 150; position.y = y + (float) Math.sin(Math.toRadians(degrees)) * 175 + 150; }
public void move(float scale) { Vector2f oldPosition = new Vector2f(position); // make our move position.y += velocity.y * scale; // figure our new bounds BoundingBox box = getBoundingBox(); // check for floor collision int y = (int) Math.floor(box.bottomY); for (int x = (int) Math.floor(box.leftX); x < Math.ceil(box.rightX); x++) { TileMapTile tile = null; try { tile = map.getMap()[x][y]; } catch (Exception e) { } if (tile != null && velocity.y <= 0) { WalkingEntity walker = this instanceof WalkingEntity ? (WalkingEntity) this : null; if (walker != null && !walker.dead && tile.isDangerous()) { ((WalkingEntity) this).damage(5); position.y = (int) oldPosition.y; velocity.y = 15f; velocity.x = -0.5f * velocity.x; break; } if (tile.isSolid() || tile.isPlatform()) { if (velocity.y < -1) { floorCollision(); System.out.println(this.getClass().getSimpleName() + ": " + box + " " + this.position); } position.y = (int) oldPosition.y; velocity.y = 0f; // apply friction if (Math.abs(velocity.x) > 0) { float sign = Math.abs(velocity.x) / velocity.x; velocity.x -= sign * 9.8 * scale; // if we managed to reverse the direction of the horiz velocity, just stop if (velocity.x != 0 && sign != Math.abs(velocity.x) / velocity.x) { velocity.x = 0; } } break; } } } position.x += velocity.x * scale; // figure our new bounds box = getBoundingBox(); // check for wall collision for (int x = (int) box.leftX; x <= box.rightX; x++) { for (y = (int) box.bottomY; y < box.topY; y++) { TileMapTile tile = null; try { tile = map.getMap()[x][y]; } catch (Exception e) { } if (tile != null && tile.isSolid()) { wallCollision(); // System.out.print(" WALL [" + x + ", " + y + "]"); if (velocity.x > 0) position.x = (int) Math.ceil(oldPosition.x); else position.x = (int) Math.floor(oldPosition.x); velocity.x = 0f; break; } } } }
@Override public void update(BaseObject parent) { Vector2f acceleration = ((GameObject) parent).getAcceleration(); acceleration.set(0, 0); if (sys.keyboard.isKeyPressed(ButtonActions.MOVE_UP)) { acceleration.y = ACCELERATION; } if (sys.keyboard.isKeyPressed(ButtonActions.MOVE_DOWN)) { acceleration.y = -ACCELERATION; } if (sys.keyboard.isKeyPressed(ButtonActions.MOVE_LEFT)) { acceleration.x = -ACCELERATION; } if (sys.keyboard.isKeyPressed(ButtonActions.MOVE_RIGHT)) { acceleration.x = ACCELERATION; } if (sys.currentTime - lastTime < fireRate) { return; } // FIXME I really shouldn't be using hardcoded values to figure out where to // shoot the bullets from. // FIXME I also REALLY REALLY need to get my vectors sorted out so that I don't have to // resort to this ugliness. bulletVelocity.set( ((GameObject) parent).getVelocity().x, ((GameObject) parent).getVelocity().x); bulletPosition.set(16f, 0f); if (sys.keyboard.isKeyPressed(ButtonActions.FIRE_LEFT)) { bulletVelocity.x -= BULLET_VELOCITY; bulletPosition.y = 16f; bulletPosition.x = 0f; } if (sys.keyboard.isKeyPressed(ButtonActions.FIRE_RIGHT)) { bulletVelocity.x += BULLET_VELOCITY; bulletPosition.y = 16f; bulletPosition.x = 32f; } if (sys.keyboard.isKeyPressed(ButtonActions.FIRE_UP)) { bulletVelocity.y += BULLET_VELOCITY; bulletPosition.y = 32f; } if (sys.keyboard.isKeyPressed(ButtonActions.FIRE_DOWN)) { bulletVelocity.y -= BULLET_VELOCITY; bulletPosition.y = 0f; } if (bulletVelocity.x != 0f || bulletVelocity.y != 0f) { Vector2f position = ((GameObject) parent).getPosition(); bulletPosition.x += position.x; bulletPosition.y += position.y; GameObject bullet = bulletPool.obtain(); if (bullet != null) { bullet.setPosition(bulletPosition.x, bulletPosition.y); bulletVelocity.rotate(bulletCounter * spreadDegree); bullet.setVelocity(bulletVelocity.x, bulletVelocity.y); sys.manager.add(bullet); if (bulletCounter == spreadCount / 2) { bulletCounter = -bulletCounter; } else { bulletCounter++; } } } lastTime = sys.currentTime; }
public void update() { position.x += speed.x; position.y += speed.y; }
public void setUV(float[] uv) { unwrap.x = uv[0]; unwrap.y = uv[1]; }