Need help on monogame screen resolution and intersection
Asked Answered
G

2

12

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

Grub answered 20/10, 2018 at 9:43 Comment(0)
G
5

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)

Grub answered 16/12, 2018 at 3:34 Comment(0)
H
1

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);
        }
    }
}
Hysteric answered 22/10, 2018 at 6:44 Comment(11)
Thanks for reply,Crrently I am able to achieve to follow the target,but the thing is intersections are not working..Grub
Hey, not a problem.What kind of intersections are you looking to perform? Raycast, bounding box, etc... Alternatively you could stick with a rectangle and test each intersection with each object via a quad tree.Hysteric
normal interections between camera object(in this case ba.draw) and out of camera objects where rsolution indenpendency is already appliedGrub
What you want to achieve is simple intersection calculations. Create a screen rectangle (proportionate to the resolution you've chosen) and create rectangles for each of your objects, and then in a separate class dubbed IntersectionHandler, test all your intersections there. This is the most easiest way, but use a QuadTree (2D) or Octtree (3D) for intersections of any kind, against the screen camera, it saves performance and is easy to implementHysteric
As I mentioned in my question,all of the intersections works as expected ,only thing not working is intersection between camera object and other objectsGrub
So then whats the exact problem your facing with the camera and object intersections? Experiencing stuttering, overlapping?Hysteric
There is an object in camera,you can see from codes,that is ba.draw,I am asking about intersection between that camera object(ba),with the objects which are not in cameraGrub
For starters, why is your camera controlling the objects? From what it sounds like the camera is storing each object? This is far from a good way to do it. Use the components collection to store your objects in your scene, not your camera, that will manage them. Externally, create an object which contains intersection properties, ie. rectangle. and have the camera test those intersections with a method that allows you to pick and choose what should be tested and what shouldn't. Its pretty simple. The code is above with checkIntersection(gameobjects[])Hysteric
This is the first time i am using camera(using XNA from last 6 years),as in the first line of question i have mentioned that i want to move the object both along x -axis and y-axis.If I move the object along x-axis without camera,then it will be out of the screen.So as i wanted to put the object in center even though it is moving along x-axis,I had to use the camera,if you have solution to achieve this(move the object both along x-axis and y-axis,but object should be in center),then tell me right away i will mark this as answer,even though that is not related to my question asked in this forumGrub
If you want to object in the center, create a view matrix that will have the camera follow the object. That way your camera doesn't directly move (As it shouldn't be). The camera is just a viewing field. No direct movement should be made on a camera. Therefore, Matrix.CreateLookat(transformedCameraPosition, ballobjectPosition, Vector3.Up) this would be your view, with the transformedCameraPosition = ballobjectPosition + new Vector3(0, 0, viewDistanceFromBall)Hysteric
Alternatively, you could use the SpriteBatch draw method that takes a rectangle as the source rectangle I think... And simply cut the rectangle in half and set those values to your camera's position. Either its the source rectangle, or destination rectangle. I think that may be a fix. But not 100% sure on that as I compute with more 3D/2.5D programming.Hysteric

© 2022 - 2024 — McMap. All rights reserved.