/** Sets this matrix to a translation matrix. * @param trn The translation vector. * @return This matrix for the purpose of chaining operations. */ public Affine2 setToTranslation (Vector2 trn) { return setToTranslation(trn.x, trn.y); }
/** Returns the transform for this group's coordinate system. */ protected Matrix4 computeTransform () { Affine2 worldTransform = this.worldTransform; float originX = this.originX, originY = this.originY; worldTransform.setToTrnRotScl(x + originX, y + originY, rotation, scaleX, scaleY); if (originX != 0 || originY != 0) worldTransform.translate(-originX, -originY); // Find the first parent that transforms. Group parentGroup = parent; while (parentGroup != null) { if (parentGroup.transform) break; parentGroup = parentGroup.parent; } if (parentGroup != null) worldTransform.preMul(parentGroup.worldTransform); computedTransform.set(worldTransform); return computedTransform; }
/** Postmultiplies this matrix by a shear matrix. * @param shear The shear vector. * @return This matrix for the purpose of chaining. */ public Affine2 shear (Vector2 shear) { return shear(shear.x, shear.y); }
Affine2 afn1 = new Affine2(); Affine2 afn2 = new Affine2(); checkEqual(mat1, mat2.idt()); checkEqual(mat1, afn1); checkEqual(mat1, afn1.idt()); afn1.setToTranslation(trn); checkEqual(mat1, afn1); afn1.setToScaling(scl); checkEqual(mat1, afn1); afn1.setToRotation(rot); checkEqual(mat1, afn1); mat1.setToRotationRad(MathUtils.degreesToRadians * rot); checkEqual(mat1, afn1); afn1.setToRotationRad(MathUtils.degreesToRadians * rot); checkEqual(mat1, afn1); afn1.setToShearing(shear); checkEqual(mat1.set(afn1), new float[] {1, shear.y, 0, shear.x, 1, 0, 0, 0, 1}); afn1.setToTrnRotScl(trn, rot, scl); afn2.setToTrnRotRadScl(trn, MathUtils.degreesToRadians * rot, scl); checkEqual(afn1, afn2); afn2.setToTranslation(trn).rotate(rot).scale(scl); checkEqual(afn1, afn2);
/** Sets the transform matrix to be used by this Batch. Even if this is called inside a {@link #begin()}/{@link #end()} block, * the current batch is <em>not</em> flushed to the GPU. Instead, for every subsequent draw() the vertices will be transformed * on the CPU to match the original batch matrix. This adjustment must be performed until the matrices are realigned by * restoring the original matrix, or by calling {@link #flushAndSyncTransformMatrix()}. */ @Override public void setTransformMatrix (Matrix4 transform) { Matrix4 realMatrix = super.getTransformMatrix(); if (checkEqual(realMatrix, transform)) { adjustNeeded = false; } else { if (isDrawing()) { virtualMatrix.setAsAffine(transform); adjustNeeded = true; // adjust = inverse(real) x virtual // real x adjust x vertex = virtual x vertex if (haveIdentityRealMatrix) { adjustAffine.set(transform); } else { tmpAffine.set(transform); adjustAffine.set(realMatrix).inv().mul(tmpAffine); } } else { realMatrix.setAsAffine(transform); haveIdentityRealMatrix = checkIdt(realMatrix); } } }
class SkewActor extends Actor { private TextureRegion tex; private Affine2 affine = new Affine2(); public SkewActor(TextureRegion textureRegion) { this.tex = textureRegion; } @Override public void draw(Batch batch, float parentAlpha) { Color color = getColor(); batch.setColor(color.r, color.g, color.b, color.a * parentAlpha); affine.setToTranslation(getX(), getY()); affine.shear(0.5f, 0); // <- modify skew here batch.draw(tex,getWidth(), getHeight(), affine); } }
/** Sets this matrix to a concatenation of translation, rotation and scale. It is a more efficient form for: * <code>idt().translate(trn).rotate(degrees).scale(scale)</code> * @param trn The translation vector. * @param degrees The angle in degrees. * @param scale The scale vector. * @return This matrix for the purpose of chaining operations. */ public Affine2 setToTrnRotScl (Vector2 trn, float degrees, Vector2 scale) { return setToTrnRotScl(trn.x, trn.y, degrees, scale.x, scale.y); }
/** Sets this matrix to a scaling matrix. * @param scale The scale vector. * @return This matrix for the purpose of chaining operations. */ public Affine2 setToScaling (Vector2 scale) { return setToScaling(scale.x, scale.y); }
/** Premultiplies this matrix by a translation matrix. * @param trn The translation vector. * @return This matrix for the purpose of chaining. */ public Affine2 preTranslate (Vector2 trn) { return preTranslate(trn.x, trn.y); }
/** Postmultiplies this matrix with a scale matrix. * @param scale The scale vector. * @return This matrix for the purpose of chaining. */ public Affine2 scale (Vector2 scale) { return scale(scale.x, scale.y); }
/** Premultiplies this matrix with a scale matrix. * @param scale The scale vector. * @return This matrix for the purpose of chaining. */ public Affine2 preScale (Vector2 scale) { return preScale(scale.x, scale.y); }
/** Premultiplies this matrix by a shear matrix. * @param shear The shear vector. * @return This matrix for the purpose of chaining. */ public Affine2 preShear (Vector2 shear) { return preShear(shear.x, shear.y); }
/** Constructs a matrix from the given affine matrix. * * @param other The affine matrix to copy. This matrix will not be modified. */ public Affine2 (Affine2 other) { set(other); }
/** Inverts this matrix given that the determinant is != 0. * @return This matrix for the purpose of chaining operations. * @throws GdxRuntimeException if the matrix is singular (not invertible) */ public Affine2 inv () { float det = det(); if (det == 0) throw new GdxRuntimeException("Can't invert a singular affine matrix"); float invDet = 1.0f / det; float tmp00 = m11; float tmp01 = -m01; float tmp02 = m01 * m12 - m11 * m02; float tmp10 = -m10; float tmp11 = m00; float tmp12 = m10 * m02 - m00 * m12; m00 = invDet * tmp00; m01 = invDet * tmp01; m02 = invDet * tmp02; m10 = invDet * tmp10; m11 = invDet * tmp11; m12 = invDet * tmp12; return this; }
/** Sets the transform matrix to be used by this Batch. Even if this is called inside a {@link #begin()}/{@link #end()} block, * the current batch is <em>not</em> flushed to the GPU. Instead, for every subsequent draw() the vertices will be transformed * on the CPU to match the original batch matrix. This adjustment must be performed until the matrices are realigned by * restoring the original matrix, or by calling {@link #flushAndSyncTransformMatrix()}. */ @Override public void setTransformMatrix (Matrix4 transform) { Matrix4 realMatrix = super.getTransformMatrix(); if (checkEqual(realMatrix, transform)) { adjustNeeded = false; } else { if (isDrawing()) { virtualMatrix.setAsAffine(transform); adjustNeeded = true; // adjust = inverse(real) x virtual // real x adjust x vertex = virtual x vertex if (haveIdentityRealMatrix) { adjustAffine.set(transform); } else { tmpAffine.set(transform); adjustAffine.set(realMatrix).inv().mul(tmpAffine); } } else { realMatrix.setAsAffine(transform); haveIdentityRealMatrix = checkIdt(realMatrix); } } }
/** Sets this matrix to a concatenation of translation, rotation and scale. It is a more efficient form for: * <code>idt().translate(trn).rotate(degrees).scale(scale)</code> * @param trn The translation vector. * @param degrees The angle in degrees. * @param scale The scale vector. * @return This matrix for the purpose of chaining operations. */ public Affine2 setToTrnRotScl (Vector2 trn, float degrees, Vector2 scale) { return setToTrnRotScl(trn.x, trn.y, degrees, scale.x, scale.y); }
/** Sets this matrix to a scaling matrix. * @param scale The scale vector. * @return This matrix for the purpose of chaining operations. */ public Affine2 setToScaling (Vector2 scale) { return setToScaling(scale.x, scale.y); }
/** Premultiplies this matrix by a translation matrix. * @param trn The translation vector. * @return This matrix for the purpose of chaining. */ public Affine2 preTranslate (Vector2 trn) { return preTranslate(trn.x, trn.y); }
/** Postmultiplies this matrix with a scale matrix. * @param scale The scale vector. * @return This matrix for the purpose of chaining. */ public Affine2 scale (Vector2 scale) { return scale(scale.x, scale.y); }
/** Premultiplies this matrix with a scale matrix. * @param scale The scale vector. * @return This matrix for the purpose of chaining. */ public Affine2 preScale (Vector2 scale) { return preScale(scale.x, scale.y); }