Skip to content

Instantly share code, notes, and snippets.

@ImMichey
Created October 7, 2020 22:51
Show Gist options
  • Save ImMichey/87cf6643691c1d892e261c266b3d8850 to your computer and use it in GitHub Desktop.
Save ImMichey/87cf6643691c1d892e261c266b3d8850 to your computer and use it in GitHub Desktop.
LibGDX 2D shadows with affines
/*
* Creates a custom Affine2 for shadows with rotation
*
* x, y --> position in world (usually the entity position)
* rotation --> angle in degrees
* offsetX, offsetY --> if the original sprite is drawn with an offset
* originX, originY --> center of the sprite
* shearX, shearY --> shear amount (usually day/night cycle shear values)
* scaleX, scaleY --> scale amount (usually day/night cycle scale values)
*/
public static Affine2 createAdvancedShadowAffine(float worldX, float worldY,
float rotation,
float offsetX, float offsetY,
float originX, float originY,
float shearX, float shearY,
float scaleX, float scaleY) {
Affine2 shearingAffine = new Affine2();
shearingAffine.setToShearing(shearX, shearY);
Affine2 offsetAffine = new Affine2();
offsetAffine.setToTranslation(offsetX, offsetY);
Affine2 rotationAffine = new Affine2();
rotationAffine.translate(originX, originY);
rotationAffine.rotate(rotation);
rotationAffine.translate(-originX, -originY);
rotationAffine.preMul(offsetAffine);
return new Affine2()
.preMul(rotationAffine)
.preMul(shearingAffine)
.preScale(scaleX, scaleY)
.preTranslate(worldX, worldY);
}
public static Affine2 createSimpleShadowAffine(float worldX, float worldY) {
WorldTime worldTime = PhantomsContainer.singleton.getPhantomsWorld().getWorldTime();
return createAdvancedShadowAffine(worldX, worldY, 0, 0, 0, 0, 0,
worldTime.getCurrentShadowShearX(), 0, 1, worldTime.getCurrentShadowSizeY());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment