Skip to content

Commit

Permalink
Fixed the handling of the anim controller affine transform
Browse files Browse the repository at this point in the history
With this change, the RenderEngine will take care of the offset in which the image is being rendered for an entity instead of incorporating it into the AffineTransfrom instance.
This way the AffineTransform instance of an AnimationController only needs to account for the actual changes it wants to carry out and will then be concatenated with the Camera translation transform during the rendering process.
This includes any offset due to divergent dimensions between an entity and the respective image.

PR #324
  • Loading branch information
steffen-wilke committed Apr 5, 2020
1 parent b0310ba commit 7909f05
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
10 changes: 9 additions & 1 deletion src/de/gurkenlabs/litiengine/graphics/RenderEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,19 @@ public void renderEntity(final Graphics2D g, final IEntity entity) {
final double ratioY = entity.getHeight() / img.getHeight();
ImageRenderer.renderScaled(g, img, Game.world().camera().getViewportLocation(entity.getLocation()), ratioX, ratioY);
} else {
// center the image relative to the entity dimensions -> the pivot point for rendering is the center of the entity
double deltaX = (entity.getWidth() - img.getWidth()) / 2.0;
double deltaY = (entity.getHeight() - img.getHeight()) / 2.0;

final AffineTransform transform = animationController.getAffineTransform();
if (transform != null) {
// center the scaled image relative to the desired render location if the transform provides a scaling element
deltaX += (img.getWidth() - (img.getWidth() * transform.getScaleX())) / 2.0;
deltaY += (img.getHeight() - (img.getHeight() * transform.getScaleY())) / 2.0;
}

Point2D renderLocation = Game.world().camera().getViewportLocation(entity.getX() + deltaX, entity.getY() + deltaY);
ImageRenderer.renderTransformed(g, img, renderLocation.getX(), renderLocation.getY(), animationController.getAffineTransform());
ImageRenderer.renderTransformed(g, img, renderLocation.getX(), renderLocation.getY(), transform);

if (Game.config().debug().renderBoundingBoxes()) {
g.setColor(new Color(255, 0, 0, 50));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package de.gurkenlabs.litiengine.graphics.animation;

import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
Expand Down Expand Up @@ -172,21 +170,7 @@ public void setAutoScaling(boolean scaling) {

@Override
public void scaleSprite(float scaleX, float scaleY) {
final Point2D point = Game.world().camera().getViewportLocation(this.getEntity());
double deltaX = (point.getX() - (point.getX() * scaleX));
double deltaY = (point.getY() - (point.getY() * scaleY));

BufferedImage img = this.getCurrentImage();
if (img != null) {
double imgDeltaX = (img.getWidth() - (img.getWidth() * scaleX)) / 2.0;
double imgDeltaY = (img.getHeight() - (img.getHeight() * scaleY)) / 2.0;

deltaX += imgDeltaX;
deltaY += imgDeltaY;
}

AffineTransform trans = new AffineTransform();
trans.translate(deltaX, deltaY);
trans.scale(scaleX, scaleY);

this.setAffineTransform(trans);
Expand Down

0 comments on commit 7909f05

Please sign in to comment.