This week has been rather hectic for my stuff. We finally got the blocks fixed, and the wall collision sort of works on both sides now. However, the camera collision still isn't completely finished. It works, but only if "up" to the player is Vector3.Up, and here's why:
The way the collision basically works is I set up a Ray that points from the player's position toward the camera. I find all intersections with objects along that ray for infinity, then I set up a temporary position vector that sits on the collision point (the one that my code decides is the appropriate one to use, but that's a different story). I then check the distance from the player to that position against the camera's constant offset. If the offset is larger, that means the camera IS actually clipping through the plane and needs to be fixed.
The problem is, I don't want to just set the camera's position to be the temporary position, because if the player is completely against a wall facing away from it, the camera would then be inside the player. My fix for this is that I modify the temporary position's y value to be the camera's actual y value, then set the position to be the new temporary position. This will ensure that the camera stays at the same height. But this only works when "up" is Vector3.Up, because I'm modifying just the y value of the vector, when I should be modifying some component based on player.world.Up. Since player.world.Up is a unit vector, multiplying it by a position vector singles out that "component" of the position, but I'm unable to modify that component.
What I'm wanting to do looks like this when written out:
tempPos * player.world.Up = Position * player.world.Up
Although, this is an illegal statement (obviously) and algebraically it doesn't work either.
So, to fix this predicament, I got rid of the Position member of the Camera class, and replaced it with a World Matrix. We still have a position within the matrix, World.Translation. But this way, it is easier to translate multiple times since the Matrix multiplication is handled for me, and I don't have to worry about modifying certain components of a Vector3.
After that, I added another list to my code that keeps track of collisions. So now I have I list that keeps track of the distances away from the player, a list that keeps track of what kind of object that is colliding, and now a list that stores the actual Plane that is being collided (so I have access to its normal and other useful things).
So when I get to the part where I need to fix the camera's position, I make another Ray that points from the camera (specifically, where the camera would be if it didn't care about collision) in the direction of the Plane's normal vector. This way, I should be able to calculate the distance from the camera to that plane and translate the camera's position the calculated amount along that vector so that it is no longer colliding. The problem is, the code isn't working and I can't seem to figure out why.
EDIT: I think I figured out the problem. I think the spring camera was breaking it. I changed Camera.Update() so that it takes a Character instead of the character's position vector and world matrix (which was redundant anyway), then after preferredPosition is calculated, I call Camera.Collision() (which takes the Character as a parameter) before calculating the actual position of the camera, and I changed the code inside of Collision() to modify preferredPosition instead of the actual World matrix. This seems to have fixed the issue. I'm not on campus so I can't commit, but I think the issue is solved.
No comments:
Post a Comment