public override CCAffineTransform NodeToParentTransform() { b2Vec2 pos = m_pBody.Position; float x = pos.x * Box2DTestLayer.PTM_RATIO; float y = pos.y * Box2DTestLayer.PTM_RATIO; if (IgnoreAnchorPointForPosition) { x += m_tAnchorPointInPoints.X; y += m_tAnchorPointInPoints.Y; } // Make matrix float radians = m_pBody.Angle; var c = (float)Math.Cos(radians); var s = (float)Math.Sin(radians); if (!m_tAnchorPointInPoints.Equals(CCPoint.Zero)) { x += c * -m_tAnchorPointInPoints.X + -s * -m_tAnchorPointInPoints.Y; y += s * -m_tAnchorPointInPoints.X + c * -m_tAnchorPointInPoints.Y; } // Rot, Translate Matrix m_tTransform = new CCAffineTransform(c, s, -s, c, x, y); return m_tTransform; }
public static CCPoint Transform(CCPoint point, CCAffineTransform t) { return new CCPoint( t.a * point.X + t.c * point.Y + t.tx, t.b * point.X + t.d * point.Y + t.ty ); }
public static CCSize Transform(CCSize size, CCAffineTransform t) { var s = new CCSize(); s.Width = (float) ((double) t.a * size.Width + (double) t.c * size.Height); s.Height = (float) ((double) t.b * size.Width + (double) t.d * size.Height); return s; }
internal static void CGAffineToGL(CCAffineTransform t, ref float[] m) { // | m[0] m[4] m[8] m[12] | | m11 m21 m31 m41 | | a c 0 tx | // | m[1] m[5] m[9] m[13] | | m12 m22 m32 m42 | | b d 0 ty | // | m[2] m[6] m[10] m[14] | <=> | m13 m23 m33 m43 | <=> | 0 0 1 0 | // | m[3] m[7] m[11] m[15] | | m14 m24 m34 m44 | | 0 0 0 1 | m[2] = m[3] = m[6] = m[7] = m[8] = m[9] = m[11] = m[14] = 0.0f; m[10] = m[15] = 1.0f; m[0] = t.a; m[4] = t.c; m[12] = t.tx; m[1] = t.b; m[5] = t.d; m[13] = t.ty; }
public static CCAffineTransform Rotate(CCAffineTransform t, float anAngle) { var fSin = (float) Math.Sin(anAngle); var fCos = (float) Math.Cos(anAngle); return new CCAffineTransform(t.a * fCos + t.c * fSin, t.b * fCos + t.d * fSin, t.c * fCos - t.a * fSin, t.d * fCos - t.b * fSin, t.tx, t.ty); }
public static CCRect CCRectApplyAffineTransform(CCRect rect, CCAffineTransform anAffineTransform) { float top = CCRect.CCRectGetMinY(rect); float left = CCRect.CCRectGetMinX(rect); float right = CCRect.CCRectGetMaxX(rect); float bottom = CCRect.CCRectGetMaxY(rect); CCPoint topLeft = CCPointApplyAffineTransform(new CCPoint(left, top), anAffineTransform); CCPoint topRight = CCPointApplyAffineTransform(new CCPoint(right, top), anAffineTransform); CCPoint bottomLeft = CCPointApplyAffineTransform(new CCPoint(left, bottom), anAffineTransform); CCPoint bottomRight = CCPointApplyAffineTransform(new CCPoint(right, bottom), anAffineTransform); float minX = Math.Min(Math.Min(topLeft.X, topRight.X), Math.Min(bottomLeft.X, bottomRight.X)); float maxX = Math.Max(Math.Max(topLeft.X, topRight.X), Math.Max(bottomLeft.X, bottomRight.X)); float minY = Math.Min(Math.Min(topLeft.Y, topRight.Y), Math.Min(bottomLeft.Y, bottomRight.Y)); float maxY = Math.Max(Math.Max(topLeft.Y, topRight.Y), Math.Max(bottomLeft.Y, bottomRight.Y)); return new CCRect(minX, minY, (maxX - minX), (maxY - minY)); }
public static CCRect Transform(CCRect rect, CCAffineTransform anAffineTransform) { float top = rect.MinY; float left = rect.MinX; float right = rect.MaxX; float bottom = rect.MaxY; CCPoint topLeft = Transform(new CCPoint(left, top), anAffineTransform); CCPoint topRight = Transform(new CCPoint(right, top), anAffineTransform); CCPoint bottomLeft = Transform(new CCPoint(left, bottom), anAffineTransform); CCPoint bottomRight = Transform(new CCPoint(right, bottom), anAffineTransform); float minX = Math.Min(Math.Min(topLeft.X, topRight.X), Math.Min(bottomLeft.X, bottomRight.X)); float maxX = Math.Max(Math.Max(topLeft.X, topRight.X), Math.Max(bottomLeft.X, bottomRight.X)); float minY = Math.Min(Math.Min(topLeft.Y, topRight.Y), Math.Min(bottomLeft.Y, bottomRight.Y)); float maxY = Math.Max(Math.Max(topLeft.Y, topRight.Y), Math.Max(bottomLeft.Y, bottomRight.Y)); return(new CCRect(minX, minY, (maxX - minX), (maxY - minY))); }
public void Concat(ref CCAffineTransform m) { float t_a = a; float t_b = b; float t_c = c; float t_d = d; float t_tx = tx; float t_ty = ty; float m_a = m.a; float m_b = m.b; float m_c = m.c; float m_d = m.d; a = m_a * t_a + m_c * t_b; b = m_b * t_a + m_d * t_b; c = m_a * t_c + m_c * t_d; d = m_b * t_c + m_d * t_d; tx = m_a * t_tx + m_c * t_ty + m.tx; ty = m_b * t_tx + m_d * t_ty + m.ty; }
public CCBoundingBoxI Transform(CCAffineTransform matrix) { var top = MinY; var left = MinX; var right = MaxX; var bottom = MaxY; var topLeft = new CCPointI(left, top); var topRight = new CCPointI(right, top); var bottomLeft = new CCPointI(left, bottom); var bottomRight = new CCPointI(right, bottom); matrix.Transform(ref topLeft.X, ref topLeft.Y); matrix.Transform(ref topRight.Y, ref topRight.Y); matrix.Transform(ref bottomLeft.X, ref bottomLeft.Y); matrix.Transform(ref bottomRight.X, ref bottomRight.Y); int minX = Math.Min(Math.Min(topLeft.X, topRight.X), Math.Min(bottomLeft.X, bottomRight.X)); int maxX = Math.Max(Math.Max(topLeft.X, topRight.X), Math.Max(bottomLeft.X, bottomRight.X)); int minY = Math.Min(Math.Min(topLeft.Y, topRight.Y), Math.Min(bottomLeft.Y, bottomRight.Y)); int maxY = Math.Max(Math.Max(topLeft.Y, topRight.Y), Math.Max(bottomLeft.Y, bottomRight.Y)); return(new CCBoundingBoxI(minX, minY, maxX, maxY)); }
public static CCRect Transform(CCRect rect, CCAffineTransform anAffineTransform) { return anAffineTransform.Transform(rect); }
/// <summary> /// Return true if `t1' and `t2' are equal, false otherwise. /// </summary> public static bool Equal(CCAffineTransform t1, CCAffineTransform t2) { return (t1.a == t2.a && t1.b == t2.b && t1.c == t2.c && t1.d == t2.d && t1.tx == t2.tx && t1.ty == t2.ty); }
public void SetLerp(CCAffineTransform m1, CCAffineTransform m2, float t) { a = MathHelper.Lerp(m1.a, m2.a, t); b = MathHelper.Lerp(m1.b, m2.b, t); c = MathHelper.Lerp(m1.c, m2.c, t); d = MathHelper.Lerp(m1.d, m2.d, t); tx = MathHelper.Lerp(m1.tx, m2.tx, t); ty = MathHelper.Lerp(m1.ty, m2.ty, t); }
public CCAffineTransform ParentToNodeTransform() { if (m_bIsInverseDirty) { m_tInverse = CCAffineTransform.CCAffineTransformInvert(NodeToParentTransform()); m_bIsInverseDirty = false; } return m_tInverse; }
public void Concat(CCAffineTransform m) { Concat(ref m); }
public static CCAffineTransform Translate(CCAffineTransform t, float tx, float ty) { return(new CCAffineTransform(t.a, t.b, t.c, t.d, t.tx + t.a * tx + t.c * ty, t.ty + t.b * tx + t.d * ty)); }
public CCNode() { m_fScaleX = 1.0f; m_fScaleY = 1.0f; m_bIsVisible = true; m_nTag = kCCNodeTagInvalid; m_tTransform = CCAffineTransform.Identity; m_bIsInverseDirty = true; // set default scheduler and actionManager CCDirector director = CCDirector.SharedDirector; m_pActionManager = director.ActionManager; m_pScheduler = director.Scheduler; }
internal static void GLToCGAffine(float[] m, CCAffineTransform t) { t.a = m[0]; t.c = m[4]; t.tx = m[12]; t.b = m[1]; t.d = m[5]; t.ty = m[13]; }
public virtual CCAffineTransform NodeToParentTransform() { if (m_bTransformDirty) { // Translate values float x = m_obPosition.X; float y = m_obPosition.Y; if (m_bIgnoreAnchorPointForPosition) { x += m_obAnchorPointInPoints.X; y += m_obAnchorPointInPoints.Y; } // Rotation values // Change rotation code to handle X and Y // If we skew with the exact same value for both x and y then we're simply just rotating float cx = 1, sx = 0, cy = 1, sy = 0; if (m_fRotationX != 0 || m_fRotationY != 0) { float radiansX = -CCMacros.CCDegreesToRadians(m_fRotationX); float radiansY = -CCMacros.CCDegreesToRadians(m_fRotationY); cx = (float)Math.Cos(radiansX); sx = (float)Math.Sin(radiansX); cy = (float)Math.Cos(radiansY); sy = (float)Math.Sin(radiansY); } bool needsSkewMatrix = (m_fSkewX != 0f || m_fSkewY != 0f); // optimization: // inline anchor point calculation if skew is not needed if (!needsSkewMatrix && !m_obAnchorPointInPoints.Equals(CCPoint.Zero)) { x += cy * -m_obAnchorPointInPoints.X * m_fScaleX + -sx * -m_obAnchorPointInPoints.Y * m_fScaleY; y += sy * -m_obAnchorPointInPoints.X * m_fScaleX + cx * -m_obAnchorPointInPoints.Y * m_fScaleY; } // Build Transform Matrix // Adjusted transform calculation for rotational skew m_sTransform.a = cy * m_fScaleX; m_sTransform.b = sy * m_fScaleX; m_sTransform.c = -sx * m_fScaleY; m_sTransform.d = cx * m_fScaleY; m_sTransform.tx = x; m_sTransform.ty = y; // XXX: Try to inline skew // If skew is needed, apply skew and then anchor point if (needsSkewMatrix) { var skewMatrix = new CCAffineTransform( 1.0f, (float) Math.Tan(CCMacros.CCDegreesToRadians(m_fSkewY)), (float) Math.Tan(CCMacros.CCDegreesToRadians(m_fSkewX)), 1.0f, 0.0f, 0.0f); m_sTransform = CCAffineTransform.Concat(skewMatrix, m_sTransform); // adjust anchor point if (!m_obAnchorPointInPoints.Equals(CCPoint.Zero)) { m_sTransform = CCAffineTransform.Translate(m_sTransform, -m_obAnchorPointInPoints.X, -m_obAnchorPointInPoints.Y); } } if (m_bAdditionalTransformDirty) { m_sTransform.Concat(ref m_sAdditionalTransform); m_bAdditionalTransformDirty = false; } m_bTransformDirty = false; } return m_sTransform; }
internal static Matrix CGAffineToMatrix(CCAffineTransform t) { var m = new float[16]; CGAffineToGL(t, ref m); return CGAffineToMatrix(m); }
/// <summary> /// Concatenate `t2' to `t1' and return the result: /// t' = t1 * t2 */s /// </summary> /// <param name="t1"></param> /// <param name="t2"></param> /// <returns></returns> public static CCAffineTransform CCAffineTransformConcat(CCAffineTransform t1, CCAffineTransform t2) { return(new CCAffineTransform(t1.a * t2.a + t1.b * t2.c, t1.a * t2.b + t1.b * t2.d, //a,b t1.c * t2.a + t1.d * t2.c, t1.c * t2.b + t1.d * t2.d, //c,d t1.tx * t2.a + t1.ty * t2.c + t2.tx, //tx t1.tx * t2.b + t1.ty * t2.d + t2.ty)); //ty }
private CCRect GetViewRect() { var rect = new CCRect(0, 0, _viewSize.Width, _viewSize.Height); return(CCAffineTransform.Transform(rect, NodeToWorldTransform())); }
public static CCAffineTransform Scale(CCAffineTransform t, float sx, float sy) { return(new CCAffineTransform(t.a * sx, t.b * sx, t.c * sy, t.d * sy, t.tx, t.ty)); }
public static CCRect Transform(CCRect rect, CCAffineTransform anAffineTransform) { return(anAffineTransform.Transform(rect)); }
public virtual CCAffineTransform NodeToParentTransform() { if (m_bIsTransformDirty) { // Translate values float x = m_tPosition.X; float y = m_tPosition.Y; if (m_bIgnoreAnchorPointForPosition) { x += m_tAnchorPointInPoints.X; y += m_tAnchorPointInPoints.Y; } // Rotation values float c = 1, s = 0; if (m_fRotation != 0.0f) { float radians = -CCMacros.CCDegreesToRadians(m_fRotation); c = (float)Math.Cos(radians); s = (float)Math.Sin(radians); } bool needsSkewMatrix = (m_fSkewX != 0f || m_fSkewY != 0f); // optimization: // inline anchor point calculation if skew is not needed if (!needsSkewMatrix && !m_tAnchorPointInPoints.Equals(CCPoint.Zero)) { x += c * -m_tAnchorPointInPoints.X * m_fScaleX + -s * -m_tAnchorPointInPoints.Y * m_fScaleY; y += s * -m_tAnchorPointInPoints.X * m_fScaleX + c * -m_tAnchorPointInPoints.Y * m_fScaleY; } // Build Transform Matrix //m_tTransform = new CCAffineTransform( // c * m_fScaleX, s * m_fScaleX, // -s * m_fScaleY, c * m_fScaleY, // x, y); // Build Transform Matrix m_tTransform.a = c * m_fScaleX; m_tTransform.b = s * m_fScaleX; m_tTransform.c = -s * m_fScaleY; m_tTransform.d = c * m_fScaleY; m_tTransform.tx = x; m_tTransform.ty = y; // XXX: Try to inline skew // If skew is needed, apply skew and then anchor point if (needsSkewMatrix) { var skewMatrix = new CCAffineTransform( 1.0f, (float)Math.Tan(CCMacros.CCDegreesToRadians(m_fSkewY)), (float)Math.Tan(CCMacros.CCDegreesToRadians(m_fSkewX)), 1.0f, 0.0f, 0.0f); m_tTransform = CCAffineTransform.Concat(skewMatrix, m_tTransform); // adjust anchor point if (!m_tAnchorPointInPoints.Equals(CCPoint.Zero)) { m_tTransform = CCAffineTransform.Translate(m_tTransform, -m_tAnchorPointInPoints.X, -m_tAnchorPointInPoints.Y); } } m_bIsTransformDirty = false; } return(m_tTransform); }
public static CCAffineTransform Translate(CCAffineTransform t, float tx, float ty) { return new CCAffineTransform(t.a, t.b, t.c, t.d, t.tx + t.a * tx + t.c * ty, t.ty + t.b * tx + t.d * ty); }
public override void UpdateTransform() { Debug.Assert(m_pobBatchNode != null, "updateTransform is only valid when CCSprite is being rendered using an CCSpriteBatchNode"); // recaculate matrix only if it is dirty if (Dirty) { // If it is not visible, or one of its ancestors is not visible, then do nothing: if (!m_bVisible || (m_pParent != null && m_pParent != m_pobBatchNode && ((CCSprite)m_pParent).m_bShouldBeHidden)) { m_sQuad.BottomRight.Vertices = m_sQuad.TopLeft.Vertices = m_sQuad.TopRight.Vertices = m_sQuad.BottomLeft.Vertices = new CCVertex3F(0, 0, 0); m_bShouldBeHidden = true; } else { m_bShouldBeHidden = false; if (m_pParent == null || m_pParent == m_pobBatchNode) { m_transformToBatch = NodeToParentTransform(); } else { Debug.Assert((m_pParent as CCSprite) != null, "Logic error in CCSprite. Parent must be a CCSprite"); m_transformToBatch = CCAffineTransform.Concat(NodeToParentTransform(), ((CCSprite)m_pParent). m_transformToBatch); } // // calculate the Quad based on the Affine Matrix // CCSize size = m_obRect.Size; float x1 = m_obOffsetPosition.X; float y1 = m_obOffsetPosition.Y; float x2 = x1 + size.Width; float y2 = y1 + size.Height; float x = m_transformToBatch.tx; float y = m_transformToBatch.ty; float cr = m_transformToBatch.a; float sr = m_transformToBatch.b; float cr2 = m_transformToBatch.d; float sr2 = -m_transformToBatch.c; float ax = x1 * cr - y1 * sr2 + x; float ay = x1 * sr + y1 * cr2 + y; float bx = x2 * cr - y1 * sr2 + x; float by = x2 * sr + y1 * cr2 + y; float cx = x2 * cr - y2 * sr2 + x; float cy = x2 * sr + y2 * cr2 + y; float dx = x1 * cr - y2 * sr2 + x; float dy = x1 * sr + y2 * cr2 + y; m_sQuad.BottomLeft.Vertices = new CCVertex3F(ax, ay, m_fVertexZ); m_sQuad.BottomRight.Vertices = new CCVertex3F(bx, by, m_fVertexZ); m_sQuad.TopLeft.Vertices = new CCVertex3F(dx, dy, m_fVertexZ); m_sQuad.TopRight.Vertices = new CCVertex3F(cx, cy, m_fVertexZ); } m_pobTextureAtlas.UpdateQuad(ref m_sQuad, m_uAtlasIndex); m_bRecursiveDirty = false; m_bDirty = false; } // recursively iterate over children if (m_bHasChildren) { CCNode[] elements = m_pChildren.Elements; if (m_pobBatchNode != null) { for (int i = 0, count = m_pChildren.count; i < count; i++) { ((CCSprite)elements[i]).UpdateTransform(); } } else { for (int i = 0, count = m_pChildren.count; i < count; i++) { var sprite = elements[i] as CCSprite; if (sprite != null) { sprite.UpdateTransform(); } } } } }
//protected Matrix m_tCCNodeTransform; // | m[0] m[4] m[8] m[12] | | m11 m21 m31 m41 | | a c 0 tx | // | m[1] m[5] m[9] m[13] | | m12 m22 m32 m42 | | b d 0 ty | // | m[2] m[6] m[10] m[14] | <=> | m13 m23 m33 m43 | <=> | 0 0 1 0 | // | m[3] m[7] m[11] m[15] | | m14 m24 m34 m44 | | 0 0 0 1 | public static void MultMatrix(CCAffineTransform transform, float z) { MultMatrix(ref transform, z); }
public bool Equals(ref CCAffineTransform t) { return(a == t.a && b == t.b && c == t.c && d == t.d && tx == t.tx && ty == t.ty); }
public virtual CCAffineTransform NodeToParentTransform() { if (m_bIsTransformDirty) { // Translate values float x = m_tPosition.X; float y = m_tPosition.Y; if (m_bIgnoreAnchorPointForPosition) { x += m_tAnchorPointInPoints.X; y += m_tAnchorPointInPoints.Y; } // Rotation values float c = 1, s = 0; if (m_fRotation != 0.0f) { float radians = -CCMacros.CCDegreesToRadians(m_fRotation); c = (float) Math.Cos(radians); s = (float) Math.Sin(radians); } bool needsSkewMatrix = (m_fSkewX != 0f || m_fSkewY != 0f); // optimization: // inline anchor point calculation if skew is not needed if (!needsSkewMatrix && !m_tAnchorPointInPoints.Equals(CCPoint.Zero)) { x += c * -m_tAnchorPointInPoints.X * m_fScaleX + -s * -m_tAnchorPointInPoints.Y * m_fScaleY; y += s * -m_tAnchorPointInPoints.X * m_fScaleX + c * -m_tAnchorPointInPoints.Y * m_fScaleY; } // Build Transform Matrix //m_tTransform = new CCAffineTransform( // c * m_fScaleX, s * m_fScaleX, // -s * m_fScaleY, c * m_fScaleY, // x, y); // Build Transform Matrix m_tTransform.a = c * m_fScaleX; m_tTransform.b = s * m_fScaleX; m_tTransform.c = -s * m_fScaleY; m_tTransform.d = c * m_fScaleY; m_tTransform.tx = x; m_tTransform.ty = y; // XXX: Try to inline skew // If skew is needed, apply skew and then anchor point if (needsSkewMatrix) { var skewMatrix = new CCAffineTransform( 1.0f, (float) Math.Tan(CCMacros.CCDegreesToRadians(m_fSkewY)), (float) Math.Tan(CCMacros.CCDegreesToRadians(m_fSkewX)), 1.0f, 0.0f, 0.0f); m_tTransform = CCAffineTransform.CCAffineTransformConcat(skewMatrix, m_tTransform); // adjust anchor point if (!m_tAnchorPointInPoints.Equals(CCPoint.Zero)) { m_tTransform = CCAffineTransform.CCAffineTransformTranslate(m_tTransform, -m_tAnchorPointInPoints.X, -m_tAnchorPointInPoints.Y); } } m_bIsTransformDirty = false; } return m_tTransform; }
public bool UpdateWithBatchNode(CCSpriteBatchNode batchnode, CCRect rect, bool rotated, CCRect capInsets) { var opacity = Opacity; var color = Color; // Release old sprites RemoveAllChildrenWithCleanup(true); _scale9Image = batchnode; _scale9Image.RemoveAllChildrenWithCleanup(true); m_capInsets = capInsets; // If there is no given rect if (rect.Equals(CCRect.Zero)) { // Get the texture size as original CCSize textureSize = _scale9Image.TextureAtlas.Texture.ContentSize; rect = new CCRect(0, 0, textureSize.Width, textureSize.Height); } // Set the given rect's size as original size m_spriteRect = rect; m_originalSize = rect.Size; m_preferredSize = m_originalSize; m_capInsetsInternal = capInsets; float h = rect.Size.Height; float w = rect.Size.Width; // If there is no specified center region if (m_capInsetsInternal.Equals(CCRect.Zero)) { m_capInsetsInternal = new CCRect(w / 3, h / 3, w / 3, h / 3); } float left_w = m_capInsetsInternal.Origin.X; float center_w = m_capInsetsInternal.Size.Width; float right_w = rect.Size.Width - (left_w + center_w); float top_h = m_capInsetsInternal.Origin.Y; float center_h = m_capInsetsInternal.Size.Height; float bottom_h = rect.Size.Height - (top_h + center_h); // calculate rects // ... top row float x = 0.0f; float y = 0.0f; // top left CCRect lefttopbounds = new CCRect(x, y, left_w, top_h); // top center x += left_w; CCRect centertopbounds = new CCRect(x, y, center_w, top_h); // top right x += center_w; CCRect righttopbounds = new CCRect(x, y, right_w, top_h); // ... center row x = 0.0f; y = 0.0f; y += top_h; // center left CCRect leftcenterbounds = new CCRect(x, y, left_w, center_h); // center center x += left_w; CCRect centerbounds = new CCRect(x, y, center_w, center_h); // center right x += center_w; CCRect rightcenterbounds = new CCRect(x, y, right_w, center_h); // ... bottom row x = 0.0f; y = 0.0f; y += top_h; y += center_h; // bottom left CCRect leftbottombounds = new CCRect(x, y, left_w, bottom_h); // bottom center x += left_w; CCRect centerbottombounds = new CCRect(x, y, center_w, bottom_h); // bottom right x += center_w; CCRect rightbottombounds = new CCRect(x, y, right_w, bottom_h); if (!rotated) { // CCLog("!rotated"); CCAffineTransform t = CCAffineTransform.Identity; t = CCAffineTransform.Translate(t, rect.Origin.X, rect.Origin.Y); centerbounds = CCAffineTransform.Transform(centerbounds, t); rightbottombounds = CCAffineTransform.Transform(rightbottombounds, t); leftbottombounds = CCAffineTransform.Transform(leftbottombounds, t); righttopbounds = CCAffineTransform.Transform(righttopbounds, t); lefttopbounds = CCAffineTransform.Transform(lefttopbounds, t); rightcenterbounds = CCAffineTransform.Transform(rightcenterbounds, t); leftcenterbounds = CCAffineTransform.Transform(leftcenterbounds, t); centerbottombounds = CCAffineTransform.Transform(centerbottombounds, t); centertopbounds = CCAffineTransform.Transform(centertopbounds, t); // Centre _centre = new CCSprite(); _centre.InitWithTexture(_scale9Image.Texture, centerbounds); _scale9Image.AddChild(_centre, 0, (int)Positions.Centre); // Top _top = new CCSprite(); _top.InitWithTexture(_scale9Image.Texture, centertopbounds); _scale9Image.AddChild(_top, 1, (int)Positions.Top); // Bottom _bottom = new CCSprite(); _bottom.InitWithTexture(_scale9Image.Texture, centerbottombounds); _scale9Image.AddChild(_bottom, 1, (int)Positions.Bottom); // Left _left = new CCSprite(); _left.InitWithTexture(_scale9Image.Texture, leftcenterbounds); _scale9Image.AddChild(_left, 1, (int)Positions.Left); // Right _right = new CCSprite(); _right.InitWithTexture(_scale9Image.Texture, rightcenterbounds); _scale9Image.AddChild(_right, 1, (int)Positions.Right); // Top left _topLeft = new CCSprite(); _topLeft.InitWithTexture(_scale9Image.Texture, lefttopbounds); _scale9Image.AddChild(_topLeft, 2, (int)Positions.TopLeft); // Top right _topRight = new CCSprite(); _topRight.InitWithTexture(_scale9Image.Texture, righttopbounds); _scale9Image.AddChild(_topRight, 2, (int)Positions.TopRight); // Bottom left _bottomLeft = new CCSprite(); _bottomLeft.InitWithTexture(_scale9Image.Texture, leftbottombounds); _scale9Image.AddChild(_bottomLeft, 2, (int)Positions.BottomLeft); // Bottom right _bottomRight = new CCSprite(); _bottomRight.InitWithTexture(_scale9Image.Texture, rightbottombounds); _scale9Image.AddChild(_bottomRight, 2, (int)Positions.BottomRight); } else { // set up transformation of coordinates // to handle the case where the sprite is stored rotated // in the spritesheet // CCLog("rotated"); CCAffineTransform t = CCAffineTransform.Identity; CCRect rotatedcenterbounds = centerbounds; CCRect rotatedrightbottombounds = rightbottombounds; CCRect rotatedleftbottombounds = leftbottombounds; CCRect rotatedrighttopbounds = righttopbounds; CCRect rotatedlefttopbounds = lefttopbounds; CCRect rotatedrightcenterbounds = rightcenterbounds; CCRect rotatedleftcenterbounds = leftcenterbounds; CCRect rotatedcenterbottombounds = centerbottombounds; CCRect rotatedcentertopbounds = centertopbounds; t = CCAffineTransform.Translate(t, rect.Size.Height + rect.Origin.X, rect.Origin.Y); t = CCAffineTransform.Rotate(t, 1.57079633f); centerbounds = CCAffineTransform.Transform(centerbounds, t); rightbottombounds = CCAffineTransform.Transform(rightbottombounds, t); leftbottombounds = CCAffineTransform.Transform(leftbottombounds, t); righttopbounds = CCAffineTransform.Transform(righttopbounds, t); lefttopbounds = CCAffineTransform.Transform(lefttopbounds, t); rightcenterbounds = CCAffineTransform.Transform(rightcenterbounds, t); leftcenterbounds = CCAffineTransform.Transform(leftcenterbounds, t); centerbottombounds = CCAffineTransform.Transform(centerbottombounds, t); centertopbounds = CCAffineTransform.Transform(centertopbounds, t); rotatedcenterbounds.Origin = centerbounds.Origin; rotatedrightbottombounds.Origin = rightbottombounds.Origin; rotatedleftbottombounds.Origin = leftbottombounds.Origin; rotatedrighttopbounds.Origin = righttopbounds.Origin; rotatedlefttopbounds.Origin = lefttopbounds.Origin; rotatedrightcenterbounds.Origin = rightcenterbounds.Origin; rotatedleftcenterbounds.Origin = leftcenterbounds.Origin; rotatedcenterbottombounds.Origin = centerbottombounds.Origin; rotatedcentertopbounds.Origin = centertopbounds.Origin; // Centre _centre = new CCSprite(); _centre.InitWithTexture(_scale9Image.Texture, rotatedcenterbounds, true); _scale9Image.AddChild(_centre, 0, (int)Positions.Centre); // Top _top = new CCSprite(); _top.InitWithTexture(_scale9Image.Texture, rotatedcentertopbounds, true); _scale9Image.AddChild(_top, 1, (int)Positions.Top); // Bottom _bottom = new CCSprite(); _bottom.InitWithTexture(_scale9Image.Texture, rotatedcenterbottombounds, true); _scale9Image.AddChild(_bottom, 1, (int)Positions.Bottom); // Left _left = new CCSprite(); _left.InitWithTexture(_scale9Image.Texture, rotatedleftcenterbounds, true); _scale9Image.AddChild(_left, 1, (int)Positions.Left); // Right _right = new CCSprite(); _right.InitWithTexture(_scale9Image.Texture, rotatedrightcenterbounds, true); _scale9Image.AddChild(_right, 1, (int)Positions.Right); // Top left _topLeft = new CCSprite(); _topLeft.InitWithTexture(_scale9Image.Texture, rotatedlefttopbounds, true); _scale9Image.AddChild(_topLeft, 2, (int)Positions.TopLeft); // Top right _topRight = new CCSprite(); _topRight.InitWithTexture(_scale9Image.Texture, rotatedrighttopbounds, true); _scale9Image.AddChild(_topRight, 2, (int)Positions.TopRight); // Bottom left _bottomLeft = new CCSprite(); _bottomLeft.InitWithTexture(_scale9Image.Texture, rotatedleftbottombounds, true); _scale9Image.AddChild(_bottomLeft, 2, (int)Positions.BottomLeft); // Bottom right _bottomRight = new CCSprite(); _bottomRight.InitWithTexture(_scale9Image.Texture, rotatedrightbottombounds, true); _scale9Image.AddChild(_bottomRight, 2, (int)Positions.BottomRight); } ContentSize = rect.Size; AddChild(_scale9Image); if (m_bSpritesGenerated) { // Restore color and opacity Opacity = opacity; Color = color; } m_bSpritesGenerated = true; return(true); }
public CCBoundingBoxI Transform(ref CCAffineTransform matrix) { var top = MinY; var left = MinX; var right = MaxX; var bottom = MaxY; var topLeft = new CCPointI(left, top); var topRight = new CCPointI(right, top); var bottomLeft = new CCPointI(left, bottom); var bottomRight = new CCPointI(right, bottom); matrix.Transform(ref topLeft.X, ref topLeft.Y); matrix.Transform(ref topRight.X, ref topRight.Y); matrix.Transform(ref bottomLeft.X, ref bottomLeft.Y); matrix.Transform(ref bottomRight.X, ref bottomRight.Y); int minX = Math.Min(Math.Min(topLeft.X, topRight.X), Math.Min(bottomLeft.X, bottomRight.X)); int maxX = Math.Max(Math.Max(topLeft.X, topRight.X), Math.Max(bottomLeft.X, bottomRight.X)); int minY = Math.Min(Math.Min(topLeft.Y, topRight.Y), Math.Min(bottomLeft.Y, bottomRight.Y)); int maxY = Math.Max(Math.Max(topLeft.Y, topRight.Y), Math.Max(bottomLeft.Y, bottomRight.Y)); return new CCBoundingBoxI(minX, minY, maxX, maxY); }
public static CCAffineTransform Scale(CCAffineTransform t, float sx, float sy) { return new CCAffineTransform(t.a * sx, t.b * sx, t.c * sy, t.d * sy, t.tx, t.ty); }
public static CCAffineTransform Invert(CCAffineTransform t) { float determinant = 1 / (t.a * t.d - t.b * t.c); return new CCAffineTransform(determinant * t.d, -determinant * t.b, -determinant * t.c, determinant * t.a, determinant * (t.c * t.ty - t.d * t.tx), determinant * (t.b * t.tx - t.a * t.ty)); }
public CCAffineTransform WorldToNodeTransform() { return(CCAffineTransform.Invert(NodeToWorldTransform())); }
public static void Lerp(ref CCAffineTransform m1, ref CCAffineTransform m2, float t, out CCAffineTransform res) { res.a = MathHelper.Lerp(m1.a, m2.a, t); res.b = MathHelper.Lerp(m1.b, m2.b, t); res.c = MathHelper.Lerp(m1.c, m2.c, t); res.d = MathHelper.Lerp(m1.d, m2.d, t); res.tx = MathHelper.Lerp(m1.tx, m2.tx, t); res.ty = MathHelper.Lerp(m1.ty, m2.ty, t); }
public CCPoint ConvertToNodeSpace(CCPoint worldPoint) { return(CCAffineTransform.Transform(worldPoint, WorldToNodeTransform())); }
public bool Equals(ref CCAffineTransform t) { return a == t.a && b == t.b && c == t.c && d == t.d && tx == t.tx && ty == t.ty; }
public CCPoint ConvertToWorldSpace(CCPoint nodePoint) { return(CCAffineTransform.Transform(nodePoint, NodeToWorldTransform())); }
public void UpdateTransform() { Debug.Assert(m_pobBatchNode != null, "updateTransform is only valid when CCSprite is being rendered using an CCSpriteBatchNode"); // recaculate matrix only if it is dirty if (Dirty) { // If it is not visible, or one of its ancestors is not visible, then do nothing: if (!m_bIsVisible || (m_pParent != null && m_pParent != m_pobBatchNode && ((CCSprite)m_pParent).m_bShouldBeHidden)) { m_sQuad.BottomRight.Vertices = m_sQuad.TopLeft.Vertices = m_sQuad.TopRight.Vertices = m_sQuad.BottomLeft.Vertices = new CCVertex3F(0, 0, 0); m_bShouldBeHidden = true; } else { m_bShouldBeHidden = false; if (m_pParent == null || m_pParent == m_pobBatchNode) { m_transformToBatch = NodeToParentTransform(); } else { Debug.Assert((m_pParent as CCSprite) != null, "Logic error in CCSprite. Parent must be a CCSprite"); m_transformToBatch = CCAffineTransform.CCAffineTransformConcat(NodeToParentTransform(), ((CCSprite)m_pParent). m_transformToBatch); } // // calculate the Quad based on the Affine Matrix // CCSize size = m_obRect.Size; float x1 = m_obOffsetPosition.X; float y1 = m_obOffsetPosition.Y; float x2 = x1 + size.Width; float y2 = y1 + size.Height; float x = m_transformToBatch.tx; float y = m_transformToBatch.ty; float cr = m_transformToBatch.a; float sr = m_transformToBatch.b; float cr2 = m_transformToBatch.d; float sr2 = -m_transformToBatch.c; float ax = x1 * cr - y1 * sr2 + x; float ay = x1 * sr + y1 * cr2 + y; float bx = x2 * cr - y1 * sr2 + x; float by = x2 * sr + y1 * cr2 + y; float cx = x2 * cr - y2 * sr2 + x; float cy = x2 * sr + y2 * cr2 + y; float dx = x1 * cr - y2 * sr2 + x; float dy = x1 * sr + y2 * cr2 + y; m_sQuad.BottomLeft.Vertices = new CCVertex3F(ax, ay, m_fVertexZ); m_sQuad.BottomRight.Vertices = new CCVertex3F(bx, by, m_fVertexZ); m_sQuad.TopLeft.Vertices = new CCVertex3F(dx, dy, m_fVertexZ); m_sQuad.TopRight.Vertices = new CCVertex3F(cx, cy, m_fVertexZ); } m_pobTextureAtlas.UpdateQuad(ref m_sQuad, m_uAtlasIndex); m_bRecursiveDirty = false; m_bDirty = false; } // recursively iterate over children if (m_bHasChildren) { CCNode[] elements = m_pChildren.Elements; if (m_pobBatchNode != null) { for (int i = 0, count = m_pChildren.count; i < count; i++) { ((CCSprite)elements[i]).UpdateTransform(); } } else { for (int i = 0, count = m_pChildren.count; i < count; i++) { var sprite = elements[i] as CCSprite; if (sprite != null) { sprite.UpdateTransform(); } } } } }
public virtual CCAffineTransform NodeToParentTransform() { if (m_bTransformDirty) { // Translate values float x = m_obPosition.X; float y = m_obPosition.Y; if (m_bIgnoreAnchorPointForPosition) { x += m_obAnchorPointInPoints.X; y += m_obAnchorPointInPoints.Y; } // Rotation values // Change rotation code to handle X and Y // If we skew with the exact same value for both x and y then we're simply just rotating float cx = 1, sx = 0, cy = 1, sy = 0; if (m_fRotationX != 0 || m_fRotationY != 0) { float radiansX = -CCMacros.CCDegreesToRadians(m_fRotationX); float radiansY = -CCMacros.CCDegreesToRadians(m_fRotationY); cx = (float)Math.Cos(radiansX); sx = (float)Math.Sin(radiansX); cy = (float)Math.Cos(radiansY); sy = (float)Math.Sin(radiansY); } bool needsSkewMatrix = (m_fSkewX != 0f || m_fSkewY != 0f); // optimization: // inline anchor point calculation if skew is not needed if (!needsSkewMatrix && !m_obAnchorPointInPoints.Equals(CCPoint.Zero)) { x += cy * -m_obAnchorPointInPoints.X * m_fScaleX + -sx * -m_obAnchorPointInPoints.Y * m_fScaleY; y += sy * -m_obAnchorPointInPoints.X * m_fScaleX + cx * -m_obAnchorPointInPoints.Y * m_fScaleY; } // Build Transform Matrix // Adjusted transform calculation for rotational skew m_sTransform.a = cy * m_fScaleX; m_sTransform.b = sy * m_fScaleX; m_sTransform.c = -sx * m_fScaleY; m_sTransform.d = cx * m_fScaleY; m_sTransform.tx = x; m_sTransform.ty = y; // XXX: Try to inline skew // If skew is needed, apply skew and then anchor point if (needsSkewMatrix) { var skewMatrix = new CCAffineTransform( 1.0f, (float)Math.Tan(CCMacros.CCDegreesToRadians(m_fSkewY)), (float)Math.Tan(CCMacros.CCDegreesToRadians(m_fSkewX)), 1.0f, 0.0f, 0.0f); m_sTransform = CCAffineTransform.Concat(skewMatrix, m_sTransform); // adjust anchor point if (!m_obAnchorPointInPoints.Equals(CCPoint.Zero)) { m_sTransform = CCAffineTransform.Translate(m_sTransform, -m_obAnchorPointInPoints.X, -m_obAnchorPointInPoints.Y); } } if (m_bAdditionalTransformDirty) { m_sTransform.Concat(ref m_sAdditionalTransform); m_bAdditionalTransformDirty = false; } m_bTransformDirty = false; } return(m_sTransform); }
/// <summary> /// Return true if `t1' and `t2' are equal, false otherwise. /// </summary> public static bool Equal(CCAffineTransform t1, CCAffineTransform t2) { return(t1.a == t2.a && t1.b == t2.b && t1.c == t2.c && t1.d == t2.d && t1.tx == t2.tx && t1.ty == t2.ty); }
/// <summary> /// Concatenate `t2' to `t1' and return the result: /// t' = t1 * t2 */s /// </summary> /// <param name="t1"></param> /// <param name="t2"></param> /// <returns></returns> public static CCAffineTransform Concat(CCAffineTransform t1, CCAffineTransform t2) { return new CCAffineTransform(t1.a * t2.a + t1.b * t2.c, t1.a * t2.b + t1.b * t2.d, //a,b t1.c * t2.a + t1.d * t2.c, t1.c * t2.b + t1.d * t2.d, //c,d t1.tx * t2.a + t1.ty * t2.c + t2.tx, //tx t1.tx * t2.b + t1.ty * t2.d + t2.ty); //ty }
public virtual bool CollidesWith(CCMaskedSprite target, out CCPoint pt) { pt = CCPoint.Zero; CCAffineTransform m1 = NodeToWorldTransform(); CCAffineTransform m2 = target.NodeToWorldTransform(); CCRect myBBInWorld = WorldBoundingBox; CCRect targetBBInWorld = target.WorldBoundingBox; if (!myBBInWorld.IntersectsRect(targetBBInWorld)) { return(false); } // Based upon http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2D/Putting_CD_into_practice.php Matrix mat1 = m1.XnaMatrix; Matrix mat2 = m2.XnaMatrix; Matrix mat1to2 = mat1 * Matrix.Invert(mat2); int width2 = (int)target.ContentSize.Width; int height2 = (int)target.ContentSize.Height; int width1 = (int)ContentSize.Width; int height1 = (int)ContentSize.Height; byte[] maskA = CollisionMask; byte[] maskB = target.CollisionMask; if (maskA == null || maskB == null) { return(false); } for (int x1 = 0; x1 < width1; x1++) { for (int y1 = 0; y1 < height1; y1++) { Vector2 pos1 = new Vector2(x1, y1); Vector2 pos2 = Vector2.Transform(pos1, mat1to2); int x2 = (int)pos2.X; int y2 = (int)pos2.Y; if ((x2 >= 0) && (x2 < width2)) { if ((y2 >= 0) && (y2 < height2)) { int iA = x1 + (height1 - y1) * width1; int iB = x2 + (height2 - y2) * width2; if (iA >= maskA.Length || iB >= maskB.Length) { continue; } byte ca = maskA[iA]; byte cb = maskB[iB]; if (maskA[iA] > 0) { if (maskB[iB] > 0) { Vector2 screenPos = Vector2.Transform(pos1, mat1); pt = new CCPoint(screenPos); return(true); } } } } } } return(false); }