-
|
|
Camera and rectangle issues with a platformer game
|
You may recognize the "character" model from the xna 3d starter tutorial, I haven't gotten very far yet so I'm just using it as a placeholder for now. The game is pretty much 2D, but uses a 3D model for the character, rectangles are used for collision since the terrain is all sprites (I honestly have no idea if a model with oriented bounding boxes can collide with rectangle-based terrain at all, but I digress). I'm new to cameras and 3D in general so matrices and whatnot is still quite confusing, so I've pretty much copy-pasted the camera and model-drawing code for now, and not surprisingly they're giving me some trouble. Drawing the character model at any other Z-coordinate than 0 makes it overly complicated to make the hitbox rectangles stick to the character, so I'd prefer to draw it at 0 if at all possible, however, the current copy-pasted camera isn't exactly agreeable. This is the camera code. | cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView( | | MathHelper.ToRadians(45.0f), | | graphics.GraphicsDevice.Viewport.AspectRatio, | | 1.0f, | | 10000.0f); | | | The 45° float gives me trouble because the further the character goes away from the middle of the screen, the more angled the character (in this case a ufo, as mentioned earlier) becomes. (Example 1, Example 2). Changing the 45° float to 1° fixes this issue, but forces me to scale the model to a ridiculously low scale (about 0.0001f, if I recall correctly) and also makes the model look quite bad, as the resolution of it is pretty low.As you can see on the screenshots, there's a little box in the upper left corner, that's my rectangle hitbox. As you can also see, it is unfortunately nowhere near the model itself. The main reason for this is (again, I may or may not be right, as I'm pretty new to 3D) that the position of the rectangle is an integer while the position of the model is a Vector3 (that is, floats).This is how the position of the hitbox is decided... | characterhitboxposition = new Rectangle((int)Character.position.X, (int)Character.position.Y, 82, 72); |
...and this is how it is updated. | characterhitboxposition.X = (int)Character.position.X; | | characterhitboxposition.Y = (int)Character.position.Y; |
When the degree float related to the camera that I mentioned earlier is set to 1° the hitbox barely moves at all when I move the model, since the model is so tiny and has to move very slowly to even stay on screen for more than half a second. If the float is 45° it moves noticably faster, but still nowhere near as fast as the model, itself, and well...it's still in the corner.Thanks in advance if anyone knows how to fix these issues.
|
|
-
-
- (295)
-
premium membership
-
Posts
145
|
Re: Camera and rectangle issues with a platformer game
|
It looks like you want an orthographic view, not a perspective view. Use the Matrix.CeateOthographic instead of Matrix.CreatePerspectiveFieldOfView (look it up on msdn). The object will always be the same size regardless of it's distance to the camera.
|
|
-
|
|
Re: Camera and rectangle issues with a platformer game
|
Thanks a lot, this solved the angled model issue perfectly! :) I'm afraid I have some new problems though. The model treats the middle of the screen as 0 for both the X and Y coordinates, while the hitbox uses actual screen coordinates, treating the upper left corner as 0,0, is there any way I can make the model follow the same coordinate system as the rectangle does? Also, for some reason, the hitbox moves up if the model moves down and vice versa, I really have no idea why it does that.
Edit: I temporarily fixed the hitbox issue by multiplying its Y-coordinate with -1 after placing it at the position of the model, but this solution shouldn't be necessary if I've understood things correctly. This thread seems to have what I need, but I'm not quite sure where to put the code (as mentioned above, I haven't coded the camera myself, so it is still rather confusing).
|
|
-
-
- (295)
-
premium membership
-
Posts
145
|
Re: Camera and rectangle issues with a platformer game
|
Your hitbox is in a different coordinate system than your model. Your hitboxes are in screen space, while your model is in world space. I would suggest transforming your object into screen space as I'm guessing that's where most of the 2d stuff is in.
Screen space's origin (0,0) is at the top left corner of your screen. World space's origin is at the center of your screen. That is why you see the difference.
As your y value increases in screen space, the further down you are on the screen. The opposite is true for world space.
I would translate your model by this : new Vector2(screenWidth/2, screenHeight/2) in addition to the translation that is based on your position.
Switching the sign of your objects y position should fix the problem the problem with the hitbox and model going in the opposite direction.
Looks like you did that already. If you don't like the idea of doing that then you need to switch the sign of your up vector of the camera. Change it from (0,1,0) to (0,-1,0). If you do this you will have to rotate the object so it doesn't look upside down.
|
|
-
|
|
Re: Camera and rectangle issues with a platformer game
|
I followed the link that I mentioned above and managed to integrate it into my code, so I've partially got things working now, the hitbox moves nicely and the model is at the same coordinates as the hitbox (according to Console.WriteLine(Character.position.ToString());, anyway). Unfortunately the model isn't visible, so something's still wrong for sure.
|
|
-
-
- (295)
-
premium membership
-
Posts
145
|
Re: Camera and rectangle issues with a platformer game
|
Just because your model has the same position as the hitbox doesn't mean that they are the same relative position to the camera. Your model is going through some world space and camera space transformations before it get's rendered the screen. Your sprites do not go through the same transformations because they are already in "2D" screen space. That's why you can't assume that the same position mean's the same position in screen space.
My advice is first forget about the hitbox. Put the model at 0,0 and make sure it's located where you expect. If it's not then you probably have to translate by constant amount. Once you get the working, start moving the object. Does it move in the way you expect? If it's moving down when you expect it to move up, you either have to change a sign on the model's transformations or the camera's transformations.
Once you get that far, add the hitbox. Does it line up with the model?
Another thing that helps is printing the position of the model and hitbox on the screen as you move it. You will get a better understanding why things are off by looking how the position of each object changes as it moves.
|
|
-
|
|
Re: Camera and rectangle issues with a platformer game
|
After a few days of work I've got things working pretty well, the model lines up with the hitbox and follows it perfectly, and I'm currently working on the physics. Collision worked fine until recently, for some (to me) unexplicable reason there's a box that won't collide with my hitbox at all, except for the first instance of aforementioned box. This box is the one that shows up as the number 6 on the grid. There's also another thing, you can jump against the 5-block and then jump again without touching the ground, which is unintended. This doesn't work with the normal walls, so it is probably related the upper side of the 5-block, and possibly to the fact that the corners of all the blocks are empty and doesn't have rectangles. I'm not really sure how to solve the corner thing, so if have any idea, I'd love to hear it, thanks in advance.
The 5-blocks seem to suffer from the same issue as the 6-blocks, none of them other than the first one drawn collides at all.
|
|
|