sharpdx / SharpDX

SharpDX GitHub Repository
http://sharpdx.org
MIT License
1.69k stars 641 forks source link

Sharpdx Camera not moving #993

Open MazeThePlayer opened 6 years ago

MazeThePlayer commented 6 years ago

I am trying to move the camera along with the player using this code: Camera.Position = (Vector2)gameobject.transform.Position; The camera main class is this: public Vector2 Position = new Vector2();

    float Rotation = 0;

    public float Zoom = 1;

    public int ViewportWidth;
    public int ViewportHeight;

    public Vector2 ViewportCenter { get { return new Vector2(ViewportWidth * 0.5f, ViewportHeight * 0.5f); } }

    public Matrix3x2 GetTransform3x2()
    {
        return Translation;
    }

    private Matrix3x2 Translation
    {
        get
        {
            return Matrix3x2.Translation(-Position.X, -Position.Y) *
                   Matrix3x2.Rotation(Rotation) *
                   Matrix3x2.Scaling(Zoom) *
                   Matrix3x2.Translation(ViewportCenter); ;
        }
    }

    public void MoveCamera(Vector2 position)
    {
        Vector2 newPosition = Position + position;
        Position = newPosition;
    }

    public Camera2D(int viewportWidth, int viewportHeight)
    {
        ViewportWidth = viewportWidth;
        ViewportHeight = viewportHeight;
    }

And this is the code i use to render everything:

        public void DrawGameobjects(Gameobject[] gameObjects)
        {
            foreach (Gameobject Gameobject in gameObjects)
            {
                if (Gameobject.Enabeld == true)
                {
                    foreach (object Component in Gameobject.Components)
                    {
                        //render 2D
                        if (Component is Sprite)
                        {
                            RawMatrix3x2 CurrentTransform = renderTarget.Transform;
                            Sprite NewSprite = Component as Sprite;
                        if (NewSprite.spriteMode == SpriteMode.Multiple)
                        {
                            int SliceIndex = NewSprite.CurrentSlice;
                            Bitmap Texture = Game.bitmapManager.GetTexture(NewSprite.texture.TexturePath);
                            RawRectangleF PositionRectangle = new Rectangle(new Point(0, 0), NewSprite.spriteSlice[SliceIndex].sliceSize).ToRawRectangleF();
                            float Transparency = NewSprite.Transparency;
                            RawRectangleF ImageRectangle = new Rectangle(NewSprite.spriteSlice[SliceIndex].slicePosition, NewSprite.spriteSlice[SliceIndex].sliceSize).ToRawRectangleF();
                            Vector2 NewScale = (Vector2)Gameobject.transform.Scale;
                            Matrix3x2 Translation = Matrix3x2.Translation((Vector2)Gameobject.transform.Position);
                            Matrix3x2 Rotation = Matrix3x2.Rotation(Gameobject.transform.Rotation.X);
                            Matrix3x2 Scale = Matrix3x2.Scaling(new Vector2(Gameobject.transform.Scale.X * NewSprite.PixelPerUnit, Gameobject.transform.Scale.Y * NewSprite.PixelPerUnit));
                            renderTarget.Transform = (Translation * Rotation * Scale).ToRawMatrix3x2();
                            renderTarget.DrawBitmap(Texture, PositionRectangle, Transparency, BitmapInterpolationMode.NearestNeighbor, ImageRectangle);
                            renderTarget.Transform = CurrentTransform;
                        }
                        else
                        {
                            Bitmap Texture = Game.bitmapManager.GetTexture(NewSprite.texture.TexturePath);
                            RawRectangleF PositionRectangle = new Rectangle(new Point(0, 0), new Size(NewSprite.texture.TextureSize * Zoom)).ToRawRectangleF();
                            float Transparency = NewSprite.Transparency;
                            RawRectangleF ImageRectangle = new Rectangle(new Point(0, 0), new Size(NewSprite.texture.TextureSize * Zoom)).ToRawRectangleF();
                            Vector2 NewScale = (Vector2)Gameobject.transform.Scale;
                            Matrix3x2 Translation = Matrix3x2.Translation((Vector2)Gameobject.transform.Position);
                            Matrix3x2 Rotation = Matrix3x2.Rotation(Gameobject.transform.Rotation.X);
                            Matrix3x2 Scale = Matrix3x2.Scaling((Vector2)Gameobject.transform.Scale);
                            renderTarget.Transform = (Translation * Rotation * Scale).ToRawMatrix3x2();
                            renderTarget.DrawBitmap(Texture, PositionRectangle, Transparency, BitmapInterpolationMode.NearestNeighbor, ImageRectangle);
                            renderTarget.Transform = CurrentTransform;
                        }

                    }

                    //Render Physics2D
                    if (Component is Physics2D)
                    {
                        Physics2D Physic = Component as Physics2D;
                        SolidColorBrush SolidBrush = new SolidColorBrush(renderTarget, new RawColor4(0.5f,1,1,1));
                        renderTarget.DrawLine(Physic.Points[0].ToRawVector2(), Physic.Points[1].ToRawVector2(), SolidBrush);
                        SolidBrush = new SolidColorBrush(renderTarget, new RawColor4(1, 0.5f, 1, 1));
                        renderTarget.DrawLine(Physic.Points[1].ToRawVector2(), Physic.Points[3].ToRawVector2(), SolidBrush);
                        SolidBrush = new SolidColorBrush(renderTarget, new RawColor4(1, 1, 0.5f, 1));
                        renderTarget.DrawLine(Physic.Points[3].ToRawVector2(), Physic.Points[2].ToRawVector2(), SolidBrush);
                        SolidBrush = new SolidColorBrush(renderTarget, new RawColor4(1, 1, 1, 0.5f));
                        renderTarget.DrawLine(Physic.Points[2].ToRawVector2(), Physic.Points[0].ToRawVector2(), SolidBrush);
                    }

                    if (Component is Tilemap)
                    {
                        foreach (Tile tile in (Component as Tilemap).ToRenderTiles)
                        {
                            Bitmap Texture = Game.bitmapManager.GetTexture(tile.TileSprite.texture.TexturePath);
                            RawRectangleF PositionRectangle = new Rectangle(new Point(0, 0), tile.Slice.sliceSize).ToRawRectangleF();
                            float Transparency = tile.TileSprite.Transparency;
                            RawRectangleF ImageRectangle = new Rectangle(tile.Slice.slicePosition, tile.Slice.sliceSize).ToRawRectangleF();
                            Vector2 NewScale = (Vector2)Gameobject.transform.Scale;
                            Matrix3x2 Translation = Matrix3x2.Translation(new Vector2(tile.Position.X, tile.Position.Y));
                            Matrix3x2 Rotation = Matrix3x2.Rotation(Gameobject.transform.Rotation.X);
                            Matrix3x2 Scale = Matrix3x2.Scaling(new Vector2(Gameobject.transform.Scale.X * (Component as Tilemap).PixelPerUnit, Gameobject.transform.Scale.Y * (Component as Tilemap).PixelPerUnit));
                            renderTarget.Transform = (Translation * Rotation * Scale).ToRawMatrix3x2();
                            renderTarget.DrawBitmap(Texture, PositionRectangle, Transparency, BitmapInterpolationMode.NearestNeighbor, ImageRectangle);
                        }
                    }
                }
            }
        }
        renderTarget.Transform = Game.Camera.GetTransform3x2().ToRawMatrix3x2();
    }`

For some reason putting renderTarget.Transform = Game.Camera.GetTransform3x2().ToRawMatrix3x2(); in the end doesn't seams to be enough to move the camera. Does anyone knows why?

h1cks commented 6 years ago

HI, you would get a better response if you raise this question on https://gamedev.stackexchange.com/ or stackoverflow.com. Feel free to raise this issue there.

MazeThePlayer commented 6 years ago

@h1cks i tried before coming here no one answered