0
votes

I'm trying to get a 3D model that I've implemented into Monogame (Xna 4.0) code to constantly be in the bottom right of the screen, it's a gun - basic pistol model, there's nothing fancy - doesn't even have textures! But I can't figure it out, I've tried some maths to get it to stay there, but the problem is that the X and Z offset I set is hard-coded so when the player rotates the camera, while the pistol rotates to face the new LookAt, the actual model doesn't move position so it goes out of sight very quickly.

It's been a long road for a pretty pointless 3D game for an A-Level (Highschool equivalent I suppose) course in computer science which I'm making in Monogame Xna to rake in those complexity marks, but I've got to the last stretch, I need to get my fbx model to sit like the player is holding it in the bottom right of the screen. Right now, if it sits in the centre of the screen, there's not much notice - however, if I increase my X or Z offset to position it where I want, when you begin to rotate your camera it falls behind the camera or goes in a massive circle around the camera which breaks immersion (despite how silly it sounds it really bugs me).

After the simple way didn't work, I tried attaching it to the BoundingBox that the current camera is using: Still left a gap, tried a BoundingSphere to see if I could get it to move around the sphere's edge and follow the player that way: No luck. Yet.

tl;dr I've tried attaching gun to BoundingBox, BoundingSphere and regular Vector Positioning to no success.

                    if (pausedGame == false)
                {
                    camera.UpdateCamera(gameTime);
                    if (carryingWeapon)
                    {
                        weapons[weaponToCarry].SetRotationMatrix(Matrix.CreateRotationX(camera.GetCameraRotation().X), Matrix.CreateRotationY(camera.GetCameraRotation().Y));
                        weapons[weaponToCarry].SetPosition(camera.weaponSlot.Center + camera.GetLookAtOffSet());
                        //Debug.WriteLine(weapons[weaponToCarry].Position);
                        //Debug.WriteLine(camera.Position);
                    }
                    CheckCollision();
                    AIMovement();
                    UpdateVisitedNodes(updatedNodes);
                }

that was my update for it, simple setting position and rotation of model. The 'weaponslot.Centre' is due to the fact that right now I left on using BoundingSphere so that piece of code is still in there.

            else if (_state == ItemState.Dynamic)
        {               
            foreach (var mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.PreferPerPixelLighting = true;
                    effect.World = Matrix.CreateScale(SCALE, SCALE, SCALE) * rotationMatrix * 
                        Matrix.CreateTranslation(position);
                    effect.View = view;
                    effect.Projection = projection;
                }
                mesh.Draw();
            }
        }

this is my draw for the actual item, it's nothing fancy.

Like you can imagine, I want it to be locked in the bottom right of the frustum and moves it's position according to where the player is looking themselves. Right now the positioning of the item is really messed up - any conceptual help is welcome! Thanks.

1
I don't have any experience in 3D, but in 2D you have separtate coordinate systems. One for the game world and one for the game window which is 0,0 at the top left corner and 1920x1080 bottom right (on 1080p resolution). When drawing your game you represet you UI and all other stationary stuff in your window coordinates. An other way is to recalculate the position for your stationary items from the current users view.Pavel Slesinger
@Pavel Slesinger thanks for the response. It's more to do with how I should go about recalculating the position from the current user view , I can't figure out how to work out the new position from the View / Projection matrix or rotation amountvK 3 1 RON

1 Answers

0
votes

What I would do is parenting the gun to the camera (it's done that way in engines like unity as well).

You would need to do that by yourself in mono (I think) - the basic principle is to first transform the gun relative to the cam and multiply with the cameras World-Matrix afterwards - this also means to have the guns position relative to the camera

gun.worldmatrix = Matrix.CreateTranslation(gun.position) * camera.world;

You now just move and rotate the camera and the gun keeps in relative position to the camera. Remember: The guns position/(rotation) has to be relative to the camera in that case.

The important thing is just the order in which you multiply the matrices (haven't tried it, hopefully I got it right above)