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);
ba.Draw(spriteBatch, Color.White);

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);

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.Request all to help,I am stuck here from long time…

Anybody here …please help…

I dont understand what you mean by this. I think you misunderstand how transformations work. If you want to center the first ball, all balls need to be transformed with your camera transform. If you use different transforms the other balls will move differently from your centered ball.

Thanks for replying…I have already achieved centering the ball with the help of camera transform.
Problem is there are other objects which are not in camera,i want to interesect this ball with the other objects which are not camera…Let me give you example-

In each tap I am moving the ball pos.X+=5 pos.Y+=5(kind of flappy bird,in my case i am moving x -axis also).
So If i am not using camera ,it is obvious that if moving x axis,the ball will go out of the screen,So by th ehelp of camera code above,I able to put the ball in centre even though it is moving along x-axis.So now in real world the ball will be in 700(x-axis),but one brick is there which is steady not moving ,also not in camera (in separate begin ,end draw) is in 500 x axis,I want to interesect this ball with brick.

In a simple words-I want to clone this game-

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

I hope you got ,what I want to acheive.


You’re posted link is broken.

I think i understand what the problem is…

You are trying to move game objects in you’re world but at the same time place the camera on one of them so that it appear in the center of the screen ?

I wrote out a working example in a new topic as the example i gave is under the assumption i understand what you are trying to do and as such… it has less to do with screen resolution as it does with transforming and centering on a game sprite.

You will need to add a Texture2D of your own to test it.

You can move the sprite with the WASD keys
The other sprites will move opposite to the centered one.

Thanks all for helping,but i found my answer-
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)

1 Like