Ok, I've been trying a lot of new things lately, and I've had a few stopping points. I decided to leave 3d because I figured I just didn't and couldn't understand the coding involved. I'm pretty good at math though, so I figured I would give it another shot.
I'm trying to learn 3d XNA in c#, I've recently worked out 2d and wish to move on. My problem is that with (in my opinion) the most basic of 3d shapes, a cube, I run into problems. After successfully exporting my cube from blender (after the 7th try >_>) and importing it into XNA, I can't get a texture to correctly show on the cube, so I downloaded a cube model from a sample source code file, and attempted to use that, and it's default texture, and I still have problems.
Basically, the code to draw the cube is:
foreach (ModelMesh mesh in model.Meshes)
{
GraphicsDevice.RasterizerState = RasterizerState.CullClockwise;
foreach (BasicEffect effect in mesh.Effects)
{
effect.TextureEnabled = true;
//effect.Texture = texture;
effect.World = world;
effect.View = view;
effect.Projection = projection;
effect.LightingEnabled = false;
}
mesh.Draw();
}
The RenderState and LightingEnabled are new, attempting to fix it myself, when I use my own texture, the texture looks like it's being stretched, and isn't showing the entire image on the cube, but all faces look the same, so it's not wrapping it.
Also, to see all the faces I rotate the cube like:
position -= new Vector3(0, 0.00f, 0.0100f);
angley += 0.01f;
anglez += 0.01f;
world = Matrix.CreateScale(1.5f) * Matrix.CreateRotationZ(anglez) * Matrix.CreateRotationY(angley) * Matrix.CreateTranslation(position);
The z change is so I could test another theory.
The default texture is a sandish texture, I'm not sure if it's stretching, because it's almost a solid color. But the box itself seems to be oddly represented (it looks as though I can see through the near faces, and I'm looking at the backs of the opposing ones)
I'm hoping someone can give me a hand, it just seems like it should be much simpler then it seems to be, to draw a simple textured cube, and most of the tutorials online are from older versions of XNA, so the code doesn't match up, and I get lost when trying to replace it with current code. (On tutorials that create a cube in code, rather then a model.)
Anyways, thanks for any answers.
EDIT 1:
Drawing this cube with CreateOrthographic makes it look correct (the first one uses perspective) but still no texture love :(
EDIT 2:
When I use my cube it's stretched, when I use the one from the source, it's a solid color.
EDIT 3:
I probably would have gotten an answer sooner had I mentioned I was displaying FPS using a font/spritebatch. When I was working out why it wouldn't work, and comparing it to a sample that DID work, I found it, Now, does anyone know how to make that work?
To myself, and anyone else encountering this problem >.<
Are you using:
SpriteBatch.begin()
to do anything in your code? If so, this is screwing up with the way your program renders in 3d, check out this link (for pre XNA 4.0):
http://blogs.msdn.com/b/shawnhar/archive/2006/11/13/spritebatch-and-renderstates.aspx
And this link if you using XNA 4.0:
http://blogs.msdn.com/b/shawnhar/archive/2010/06/18/spritebatch-and-renderstates-in-xna-game-studio-4-0.aspx
The line in particular that fixed this issue was:
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
Adding that before the foreach loop fixed this problem for me, but you may need to try the other lines in that article
To anyone else reading this, Good luck with your XNA dreams :D
Related
It's a 360 Video application on Unity 3D.
I want to place several objects around the camera (which has a fixed position), but I need this objects to have the same distance (same radius) from the Camera (which is the center). How can I do this? Either on Editor or by code.
I have been manually displacing objects around the camera, by dragging them by arrow tool. But it's as inaccurate as a pain to do. :)
Any light on this would help me a lot! Not only me, but anyone working with 360 videos in Unity.
Thank you all in advance!
To solve your problem, an easy solution would be to add a child "child_of_camera" to your camera and then add a child "child_of_child" to the "child_of_camera".
Now that you've done this, move the "child_of_child" away to however far from the camera you'd like it to be. After this, apply the random rotation you'd like to "child_of_camera".
Duplicate "child_of_camera" to however many objects you'd like on screen and then rotate them all to your preference.
Now, when you're moving around the camera, these children will follow along with the camera.
If you're looking so that the rotation of the camera doesn't affect the objects there are two ways you can handle this:
Place camera and "child_of_camera" (this name would now be misleading and should be renamed) under an empty GameObject and move "empty_GO" on the X,Y,Z axis instead of the camera.
or
Create a quick script to attach onto "child_of_camera" so that it always sets the "child_of_camera"s world space rotation to Vector3.zero.
As I stated in the comments, this solution is most likely not the optimal way to fix your problem but it is definitely a very simple solution that is easy to understand and implement. I hope it helps.
Me and my team are working on a game in xna right now. However we've run into a problem. It is an open space sim where players will be able to build their own ships (top down view) room by room. Each room has a texture2d, however since the game is top-down, we will need to be able to "turn the ship".
We're running into a problem rotating the collection of texture2ds that make up the ship. In our old code, we tried to rotate all the rooms around a central origin as the location of the bridge. As we had problems with this, we looked online and decided to try our hand at RenderTargets (draw the whole ship onto an RT, then we could just rotate the RT image when it's drawn).
Our problem is that the RT draws a big purple background and, since our ships aren't always perfectly filled in rectangles, there's always a purple background somewhere poking out.
The question is: What is the best way to rotate a collection of texture2ds around a single origin in xna as if they were a single image?
Your problem is almost word-for-word the exact same problem I had until recently.
I had a collection of compartments (for each of my spaceships) rotating around an origin, but this resulted in:
Large overhead for calculating and reposition each and every compartment
Small gaps between compartments, probably due to the inaccuracies of the float
I recommend you go back to the RenderTarget approach. To get rid of the purple background, use:
GraphicsDevice.SetRenderTarget(render);
GraphicsDevice.Clear(Color.Transparent);
Much less overhead and a lot prettier without the gaps between compartments.
All the best to you and your team, may the best spaceship game win ;]
Create a camera that rotates instead of rotating all textures / make a rendertarget.
It basically views the sprites, instead of tweaking everything else.
There are plenty of tutorials on the internet, but I'll show you something:
class Camera
{
public Matrix transform; // The transformed matrix
public void Update(float rotation)
{
// you can add the positions here, x y z. Play around with it.
// x should be half of the screen width,
// y the half of the screen height, creating a "orgin" where it's rotating.
transform = Matrix.CreateTranslation(0, 0, 0) * Matrix.CreateRotationZ(rotation);
}
}
Put this in your Draw method, it uses the camera's viewport:
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, camera.transform);
Also: I don't really know much about camera's. But this is a basic one.
You will have to find "center of mass" aka barycenter. it's averageing of all x and y coordinates. and you will have origin that you need.
this is code i found some times ago, but never tested... i guess it's also from stackoverflow.
Point bary = Point.Zero;
foreach (Point p in list)
{
bary.X += p.X;
bary.Y += p.Y;
}
bary.X = (bary.X + list.Count / 2) / list.Count;
bary.Y = (bary.Y + list.Count / 2) / list.Count;
return bary;
try this way
I'm making a zelda-like 2D Role-Playing-Game with XNA and wanted to implement a camera which follows the player today. Soo... I didn't really understand how it's working, but I found a nice code-example here: http://www.david-amador.com/2009/10/xna-camera-2d-with-zoom-and-rotation/
I implemented it and changed the spritebatch.Begin(); to what the guy in the thread said. Now it is working ... well. The collisions are alright and the player is centered (since I set the position of the camera to the position of the player once a frame) but: The textures don't get drawed the way they did before. So for example the shadow is drawn under the grass (=you can't see the shadows) the player is sometimes drawn over the NPCs and sometimes under them and the wall-tiles disappear and reappear like they want :(
Do I have to change the drawing-code or the order they get drawn?
Hope somebody know what I am doing wrong...
In your Spritebatch.Draw call you can specify the layer depth. When specifying the layer depth, you will need to use the proper Sprite Sort Mode when initializing your SpriteBatch.
At the moment i got a game project going where i will use the BasicEffect from XNA with defaultlighting on.
Simply put in class RenderLibManager
effect = new BasicEffect(device);
effect.EnableDefaultLighting = true;
This is working well on the normal 3D models, we got trees and a guy from the MSDN samples.
The problem is when i try to fetch the same basic effect from RenderLibManager to the world terrain file. I fetch it by doing
BasicEffect effect;
effect = RenderLibManager.effect;
And then i set the texture, cameraViewMatrix and cameraProjectionMatrix.
So when i start the game the terrain has a very dark blueish lighting. I don't really know why, but i was hoping someone could point me in the right direction.
Best regards,
Kerrai
EDIT
I actually found the error on my own. After a very long investigation i had forgotten to generate the normals of the terrain, or even set them in the vertex buffer.
Thank you all for looking into the answer at least
Ok, this is hard to explain without pictures. I made a very limited model of a glock in Blender with an extruded cube making up the base and a scaled cube making up the barrel.
In Blender, It looks fine:
However, after exporting the model to .fbx and loading it into the compiler, it comes out like this:
I don't know exactly what is going on. Everything on the model is properly UV Mapped and the coordinates are correct, It just seems the translation is.. off..
Here is my drawModel code:
private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
{
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = world;
effect.View = view;
effect.Projection = projection;
}
mesh.Draw();
}
}
Any pointers will be helpful!
EDIT: After Applying rotation, location, and scale, I was able to get them in the right position, but why does it look transparent?
Thank you for all the help!
As Nikola mentioned, in the later versions of Blender, they have proper XNA support.
Here's a screenshot of the settings I chose when exporting a model:
Why this happens:
Your Depth Buffer is not enabled, so when your GPU draws the barrel first, then the stock/handle, the handle is drawn on top of the barrel, even though some parts of it are supposed to be invisible because they would be hidden by the barrel.
How to fix:
Enable Depth Buffer as other answer suggests. And/Or order meshes in your model based on distance from camera, and draw tehm in order from the nearset to the farthest. This may not seem usefull now, but when you start working with transparent objects, you will see why this is a good thing to learn.
Enable depth buffer:
GraphicsDevice.DepthStencilState = DepthStencilStates.Default;
And to Draw a model in Xna you can follow this tutorial, that shows how the bone transforms have to be applied to get the meshes in right position.