Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 650
uncle_scrooge Avatar asked Oct 20 '18 09:10

uncle_scrooge


2 Answers

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)

like image 76
uncle_scrooge Avatar answered Nov 14 '22 14:11

uncle_scrooge


I don't understand your question per say, but from what I gathered, you want the camera to follow the target's position, and you also want independent screen resolutions?

Well, for the independent screen resolution, simply create a screen resolution handler that renders the scene to a RenderTarget2D as defined by your sizes. Then draw that to the screen.

For the camera movement. Try adjusting the camera's position to follow the target's position with an offset and slerp interpolation to prevent stuttering and smooth action.

void Update(float gameTime) {
        Vector3 camTransform = Camera.Position + cameraTarget;
        Vector3 newCameraPosition = Vector3.Slerp(cameraPosition, camTransform, 0.2f);
        Camera.Position = newCameraPosition;
}

For your intersection problem try something along this

private bool intersects(rectangle1, rectangle2) {
    return rectangle1.x >= rectangle2.x &&
           rectangle1.y >= rectangle2.y &&
           rectangle1.y <= rectangle2.y + rectangle2.h &&
           rectangle1.x <= rectangle2.x + rectangle2.w;
}

private void checkIntersections(gameObjects[]) {
    foreach (var obj in gameobjects) {
        if (intersects(obj.rectangle, camera.rectangle){
             handleIntersections(camera, obj);
        }
    }
}
like image 39
Linas Paz Avatar answered Nov 14 '22 14:11

Linas Paz