XNA BasicEffect, strange result on terrain with texture - c#

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

Related

Objects having same distance/radius from the center (camera) in Unity 3D

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.

XNA 2D Game Camera doesnt work

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.

XNA Texturing issue

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

Hiding objects that obscure the player in a 3D scene

I'm designing a 3D game with a camera not entirely unlike that in The Sims and I want to prevent the player character from being hidden behind objects, including walls, pillars and other objects.
One easy way to handle the walls case is to have them face inwards and not have an other side, but that won't cover the other cases at all.
What I had planned is to somehow check for objects that are "in front" of the player, relative to the camera, and hide them - be it by alpha blending or not rendering at all.
One probably not so good idea I had in mind is to scan from the camera to the player in a straight line and see if you hit a non-hidden object, continuing until you reach the player. Unfortunately, I am an almost complete newbie on 3D programming.
Demonstration SVG illustration < that wall panel obscures the player, so it must be hidden. Another unrelated and pretty much already solved problem is removing all three wall panels on that side, which is irrelevant to this question and only caused by the mapping system I came up with.
What I had planned is to somehow check for objects that are "in front" of the player, relative to the camera, and hide them - be it by alpha blending or not rendering at all.
This is a good plan. You'll want to incorporate some kind of bounding volume onto the player, so the entire player (plus a little extra) is visible at all times. Then, simply run the intersection algorithm for each corner of the bounding volume.
Finding which object is at a given point on screen is called picking. Here's an XNA link for you which should direct you to an example. The idea is that you retrieve the 3D point in the game from the 2D point, and then can use standard collision detection methods to work out which object is occupying that space. Then you can elect to render that object differently.
One hack which might suffice if you have trouble with the picking approach is to render the character once as part of the scene, and then render it again at the end at half-alpha on top of everything. That way you can see the whole character and the wall, though you won't see through the wall as such.
One easy way, at least for prototyping, would be to always draw the player after you draw the rest of the scene. This would ensure that the player is rendered on top of anything else in the scene. Crude but effective.
Create a bounding volume from the camera to the extents of the player, determine what objects intersect that volume, and then render them in whatever alternate style you want?
There might be some ultra-clever way to do this, but this seems like the pretty straightforward version, and shouldn't be too much of a perf hit (you're probably doing collision every frame anyway....)
The simplest thing I can think of that should work relatively well is to model all obstacles by a plane perpendicular to your ground (assuming you have a ground.) Roughly assuming everything that is an obstacle is a wall with some height.
Model your player as a point somewhere, and model your camera as another point. The line in 3d that connects these two points lies in a plane that is particularly interesting to you, because if this plane intersects an "obstacle plane" below the height of the obstacle, that means that that obstacle is blocking your view of the player point.
I hope thats somewhat clear. To make this into an algorithm you'd have to implement a general method for determining where two planes intersect (to determine if the obstacle is tall enough to block view.)

C#: How can I tell which side a collision has occured on?

I think the title is rather self explanatory but just to clarify I am trying to figure out how to tell which side the collision has occured on.
For a bit more detail, I'm trying to make a maze-like game so I can't simply stop all movement upon a collision. Instead I need to be able to tell which side the collision has happened on so I can block that direction.
Any help is appreciated and if there is a better approach to this issue, I'm all for trying it out.
I hope this is enough details but if you need anymore, ask and I'll edit. Thanks in advance.
[edit]
#viggity - No, I'm not using any specific game engine and I would post the current "detection" code but it's a little, absurdly, robust.
#Streklin - I'm using the this.Paint event to draw onto the form itself as it was recommended I start by doing that to get better at drawing real time. I'm also using a location that's updated each time the timer ticks based on what I press (left, right, up, down). Yes the maze is tile based. Currently it only consists of 3 colors even. I'm not a very advanced programmer.
#Eric - Definately a one-d game. Again, I only have 3 colors, the lines are black, the background is white and the square (the user) is green. I'm using the DrawImage() with Bitmaps to draw onto the screen.
[edit psuedo-code summary]
foreach(Wall _wall in walls)
if(player.intersectsWith(_wall))
stop movement;
#JeffH - I'm not really sure what you're asking as that's pretty much all there is besides testing code that I was using to try and get it working. The only thing I left out was the if statement to check if it was the x axis or not so that x and y could move indepedently from each other. So instead of getting "stuck" because you touched the wall, you could slide against it. I didn't see the point in including that though since the problem occurs before that.
Assuming you're talking about a 3D game here.
The normal of the face you can see points towards you, so the dot product of your direction vector with the face normal will be negative. If it's positive then you are coming at the face from the back.
If it's zero you're travelling at right angles to the face.
| <---------- your direction of travel
|
|----------> <- face normal
|
| <- face
If you're not in 3D then you could store the direction the wall is facing (as a 2D vector) and do the same dot product with your 2D direction of movement.
Based on your edit you can only go one direction at a time? Or can you go in diagonal directions? If it's the later, ChrisF has provided you the answer in 3D and the corresponding information for 2D. If not, you should just have to stop travel in the direction of travel - since there are only four possibilities it should be easy enough to check them all for simple starter game.

Categories

Resources