Search code examples
c#xnamonogamexna-4.0

Need help on monogame screen resolution and intersection


Currently in my game i want trying to move my object towards both x axis and y axis.As I also wanted to put it into center ,I have put a camera.Here is my Camera code-

 public class Camera
{
    public Matrix transform;
    public Viewport view;
    public Vector2 origin;
    Vector2 baseScreenSize = new Vector2(1136, 720);
    float horScaling ;
    float verScaling ;
    Vector3 screenScalingFactor ;
    public Camera(Viewport newView)
    {
        view = newView;
        horScaling = view.Width / baseScreenSize.X;
        verScaling = view.Height / baseScreenSize.Y;
        screenScalingFactor = new Vector3(horScaling, verScaling, 1);
    }

    public void update(GameTime gt, ball pl)
    {
        origin = new Vector2(pl.Position.X + (pl.ballRectangle.Width / 2) - 400, 0);
        transform = Matrix.CreateScale(1,1,0) *
            Matrix.CreateTranslation(new Vector3(-origin.X, -origin.Y, 0));
    }

}

and in Game1.cs file as usual in begin statement i am putting this-

 spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null, cm.transform*globalTransformation);
                ba.Draw(spriteBatch, Color.White);
                spriteBatch.End();

Here ba is the object of ball,its just have moving x and y functionalities.

In a separate begin,end statement ,I am drawing rest all of the objects-

 spriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, null, globalTransformation);

                spriteBatch.Draw(mainMenu, new Vector2(0, 0), Color.White);
                spriteBatch.Draw(mainMenu1, new Vector2(450, 100), Color.White);
                spriteBatch.End();

Here Have applied globaltransformation to acheive independent screen resolution(similar codes like in Camera.cs).

Rest of the objects are working as expected,But intersections of camera object and rest of the objects is not working as expected. I guess this is due to resolution independency is not applied to Camera object(I am not sure).I have tried lot of codes after searching internet,but none of them is working as expected. In a simple words-I want to clone this game- https://play.google.com/store/apps/details?id=com.BitDimensions.BlockyJump

If you see main player is moving along x and y axis,but due to camera its in constant position,but the obstacles are not in camera,How to acheive the intersection between obejct which is in camera draw and objects which are not in camera in this case Request all to help,I am stuck here from long time...


Solution

  • Never thought this will be this much of easy...Searched all over internet,in most of the codes they were saying we need to inverse the camera transform. But this is not the case.As from beginning I was saying my problem is intersection between camera object and non camera object,here is the answer-

    First of all we need to find the positions of camera object to form a world space rectangle

    Vector2 hj = Vector2.Transform(ba.Position, cm.transform);
          Rectangle leftRectangleT1 =
                       new Rectangle((int)hj.X, (int)hj.Y, ba.tex.Width, ba.tex.Height);
    

    Here ba is the camera object,we need to transform it to camera transform like above codes.

    To get transform of ba in case pixel intersection,here is the codes-

    Matrix ballTransform = Matrix.CreateTranslation(new Vector3(hj.X, hj.Y, 0.0f));
    

    Thats it you have ball rectangle which is camera object to intersect with real world objects(non camera objects)