this is my first time posting so sorry for any mistakes
Hi, I’m trying to make a game where the sprites of the game can rotate around another. in the constructor if “aParent” is null then its parent should be itself. in draw I have the parents position as the rotation Origin, here’s the code i use
namespace Game1
{
public class Sprite
{
protected Texture2D myTexture;
public Vector2 myPosition;
public Vector2 myVelocity;
public Color Clolor = Color.White;
public float myRotation = 0;
public Sprite myParent;
public Rectangle Rectangle
{
get
{
return new Rectangle((int)myPosition.X - myTexture.Width/2, (int)myPosition.Y, myTexture.Width, myTexture.Height);
}
}
public Sprite(Texture2D aTexture , Vector2 aPostion, Sprite aParent = null, float aRotation = 0)
{
myTexture = aTexture;
myPosition = aPostion;
myRotation = aRotation;
if(aParent == null)
{
myParent = this;
}
else
{
myParent = aParent;
}
}
public virtual void Update(GameTime gametime, List<Sprite> someSprites, float someDeltaTime)
{ }
public virtual void Draw(SpriteBatch aSpriteBatch)
{
aSpriteBatch.Draw(myTexture, myPosition, null, Color.White, myRotation, myParent.myPosition, 1, SpriteEffects.None, 0);
}
#region Collision
//TODO fix top collision
protected bool IsTouchingLeft(Sprite sprite)
{
return this.Rectangle.Right + this.myVelocity.X > sprite.Rectangle.Left &&
this.Rectangle.Left < sprite.Rectangle.Left &&
this.Rectangle.Bottom > sprite.Rectangle.Top &&
this.Rectangle.Top < sprite.Rectangle.Bottom;
}
protected bool IsTouchingRight(Sprite sprite)
{
return this.Rectangle.Left + this.myVelocity.X < sprite.Rectangle.Right &&
this.Rectangle.Right > sprite.Rectangle.Right &&
this.Rectangle.Bottom > sprite.Rectangle.Top &&
this.Rectangle.Top < sprite.Rectangle.Bottom;
}
protected bool IsTouchingBottom(Sprite sprite)
{
return this.Rectangle.Bottom + this.myVelocity.Y > sprite.Rectangle.Top &&
this.Rectangle.Top < sprite.Rectangle.Top &&
this.Rectangle.Right > sprite.Rectangle.Left &&
this.Rectangle.Left < sprite.Rectangle.Right;
}
protected bool IsTouchingTop(Sprite sprite)
{
return this.Rectangle.Top + this.myVelocity.Y < sprite.Rectangle.Bottom &&
this.Rectangle.Bottom > sprite.Rectangle.Bottom &&
this.Rectangle.Right > sprite.Rectangle.Left &&
this.Rectangle.Left < sprite.Rectangle.Right;
}
#endregion
}
}
it draws out this : (there are supposed to be three platforms and a player)
and this is how its supposed to look like:
thank you in advance and sorry if its a bit unclear, just ask and ill try to explain better