So I'm new to XNA and am attempting to draw multiple sprites to the screen in the easiest way. I want each sprite to be incremented in the X axis so that I am left with multiple sprites drawn across the screen.
I have defined:
Vector2 pos;
In the LoadContent function I have:
pos = new Vector2(0, 0);
and in Draw I have:
spriteBatch.Draw (circle, pos, Color.White); //Draws sprite to screen in correct position
spriteBatch.Draw(circle, (pos.X += 1), Color.White); //causes error and doesnt draw
Hopefully I've explained this well enough and you can see what I'm trying to do, the compiler doesn't agree with (pos.X += 50) (me trying to increment the X position by 50).
I know I could go about this a longer way and create a new Vector2 for each draw but that would create multiple lines of what I think surely is unnecessary code and there must be a quick way like this to go about doing it?
The method signature of Draw expects the second parameter to be a Vector2, right?
If so, then (pos.X += 1) isn't a Vector2. It's a statement which increments the X parameter of the pos Vector2 variable, but the statement doesn't return an instance of a Vector2 object.
Edit: Code is as follows:
public void DrawSprites()
{
// setup circle here
// setup spritebatch here
// setup initial pos here
// setup MAX_ITERATIONS here
var INCREMENT_VALUE = 50;
for (var i = 0; i < MAX_ITERATIONS; i++) {
var iteratedPos = pos + new Vector2((INCREMENT_VALUE * i), 0); // per Nikola's comment
spriteBatch.Draw(circle, iteratedPos, 0), Color.White);
}
}
You need to make a list of the draw rectangles of your textures.
//implement Texture2D (called "image") and SpriteBatch (called "spriteBatch")
List<Rectangle> rectangles = new List<Rectangle>();
const int ITERATIONS = 10; //or whatever you want the iterations to be
const int INCREMENT_VALUE = 50; //again, whatever you want it to be
for (int i = 0; i < ITERATIONS; i++)
{
for (int j = 0; j < rectangles.Count; j++)
{
rectangles[j].X += INCREMENT_VALUE * i;
spriteBatch.Draw(image, rectangles[j], Color.White);
}
}
Like said earlier, you need a list of Rectangles that have all the image rectangles in them. Hope I helped, and good luck!
Related
I want to find the 2D bounding box (BB) of the visible part of an Game-object in Unity3D.**
Thanks to having no reputation i cant post images, so here are links to Imgur:
Starting Point: Image
Showing the initial scene camera. For example i want to get the BB of the wheels of the passing car.
Goal: Image
Showing the quite small BB i want to get in an efficient way. The method used is very costly and i want to avoid it. Its works on a segmented version of the input picture, looking similar to this: Image.
Current Approach:
Image
This uses the bounds of all GameObjects (GO) tagged as wheels and calls a function similar like this:
Rect GetBoundingBox(GameObject go, Camera camera, float margin = 0f)
{
Rect rect = new Rect(-1f, -1f, -1f, -1f);
Vector3[] v; // Vertices
// Object is behind camera
if (camera.WorldToScreenPoint(go.GetComponentInChildren<Renderer>().bounds.center).z < 0)
return rect;
// get first mesh filter
v = go.GetComponentInChildren<MeshFilter>().mesh.vertices;
// maybe the game object hast no mesh yet... and is empty
if (v.Length < 1 || v == null)
return rect;
for (int i = 0; i < v.Length; i++)
{
//World space
v[i] = go.transform.TransformPoint(v[i]);
//GUI space
v[i] = camera.WorldToScreenPoint(v[i]);
v[i].y = Screen.height - v[i].y;
}
Vector3 min = v[0];
Vector3 max = v[0];
for (int i = 1; i < v.Length; i++)
{
min = Vector3.Min(min, v[i]);
max = Vector3.Max(max, v[i]);
}
//Construct a rect of the min and max positions and apply some margin
rect = Rect.MinMaxRect(min.x, min.y, max.x, max.y);
// apply optional margin
rect.xMin -= margin;
rect.xMax += margin;
rect.yMin -= margin;
rect.yMax += margin;
return rect;
}
Two problems appear.
Its getting all BB even if the GO is not visible by the camera.
Even if its visible, the boundaries are as large as the GO it self, not as the visible part.
I am very happy about every hint in a good direction!!
For interested people the background is the development of an application to generate/simulate labeled training data for an deep learning project.
So I am working on a side scrolling platformer. I changed some code to allow for scrolling backgrounds, which is working perfectly; however now when my levels advance the background stays the same.
This is the class for Layer
class Layer
{
public Texture2D[] Textures { get; private set; }
public float ScrollRate { get; private set; }
public Layer(ContentManager content, string basePath, float scrollRate)
{
// Assumes each layer only has 3 segments.
Textures = new Texture2D[3];
for (int i = 0; i < 3; ++i)
Textures[i] = content.Load<Texture2D>(basePath + "_" + i);
ScrollRate = scrollRate;
}
public void Draw(SpriteBatch spriteBatch, float cameraPosition)
{
// Assume each segment is the same width.
int segmentWidth = Textures[0].Width;
// Calculate which segments to draw and how much to offset them.
float x = cameraPosition * ScrollRate;
int leftSegment = (int)Math.Floor(x / segmentWidth);
int rightSegment = leftSegment + 1;
x = (x / segmentWidth - leftSegment) * -segmentWidth;
spriteBatch.Draw(Textures[leftSegment % Textures.Length], new Vector2(x, 0.0f), Color.White);
spriteBatch.Draw(Textures[rightSegment % Textures.Length], new Vector2(x + segmentWidth, 0.0f), Color.White);
}
}
And this is level constructor in the loading part of class level
public Level(IServiceProvider serviceProvider, Stream fileStream, int levelIndex)
{
// Create a new content manager to load content used just by this level.
content = new ContentManager(serviceProvider, "Content");
timeRemaining = TimeSpan.FromMinutes(2.0);
LoadTiles(fileStream);
// Load background layer textures.
layers = new Layer[3];
layers[0] = new Layer(Content, "Backgrounds/Layer0", 0.2f);
layers[1] = new Layer(Content, "Backgrounds/Layer1", 0.5f);
layers[2] = new Layer(Content, "Backgrounds/Layer2", 0.8f);
// Load sounds.
exitReachedSound = Content.Load<SoundEffect>("Sounds/ExitReached");
}
It looked like this before I changed it to allow for scrolling
layers = new Texture2D[3];
for (int i = 0; i < layers.Length; ++i)
{
// Choose a random segment if each background layer for level variety.
int segmentIndex = random.Next(3);
layers[i] = Content.Load<Texture2D>("Backgrounds/Layer" + i + "_" + segmentIndex);
}
Thank you for your time.
Ok, I note a couple of things:
When you changed it to allow for scrolling, your layers went from one segment to 3.
The image for each single-segment layer was chosen at random (out of three), now each layer has all three segments in order, as loaded by your for loop.
You don't factor your level index in to the selection of your textures.
So, in the past, each level would give you a somewhat randomly selected choice of background, and now you get the same selection each time because you load segment 1, then 2, then 3 in your for loop.
Suggestions:
Re-introduce the randomness
Draw more backgrounds, and factor your level index in to the choice of backgrounds
I didn't quite get what you wanted here:
spriteBatch.Draw(Textures[leftSegment % Textures.Length], new Vector2(x, 0.0f), Color.White);
spriteBatch.Draw(Textures[rightSegment % Textures.Length], new Vector2(x + segmentWidth, 0.0f), Color.White);
More precisely, these two things:
leftSegment % Texture.Length
rightSegment % Textures.Length
Have you tried to draw inserting the Indexes manually? Like...
spriteBatch.Draw(Textures[0]...
spriteBatch.Draw(Textures[1]...
...
Because those leftSegment and rightSegment are dependent of the segmentWidth, right?
int segmentWidth = Textures[0].Width;
Is that width always going to be the same? If yes, there's no need to do this on the texture index on the draw method, because it'll always be the same indexes...
I didn't quite understand what you tried there(I'm sorry, I'm kinda dumb), but it just sounds... strange to me..
I don't know, maybe I'm just saying non-sense stuff...
I have some problems on the display of my model with texture.
Everything works perfectly, however, I use a loop to repeat the texture to represent a floor 20 X 20 on the screen. My texture is repeated correctly. But I do not understand why all my textures generate a flicker ...
I noticed that images are superimposed on each other. I'm sure I checked my loop is coded correctly.
see screenshot:
my code (loop function generation ground):
//Function draw - ground land
private void draw_groundLand(Vector3 position_model_origin)
{
//example generation mode 4x4 cubes
int[,,] MatriceWorldCube = new int[1,2,2];
MatriceWorldCube[0, 0, 0] = 1;
MatriceWorldCube[0, 0, 1] = 1;
MatriceWorldCube[0, 1, 0] = 2;
MatriceWorldCube[0, 1, 1] = 1;
int height = MatriceWorldCube.GetLength(0);
int width = MatriceWorldCube.GetLength(1);
int length = MatriceWorldCube.GetLength(2);
Vector3 pos_reference = position_model_origin;
for (int thickness = 0; thickness < height; thickness ++)
{
for (int column = 0; column < width; column ++)
{
for (int line = 0; line < length ; line ++)
{
// Copy any parent transforms.
Matrix[] transforms = new Matrix[model_ground_land1.Bones.Count];
model_ground_land1.CopyAbsoluteBoneTransformsTo(transforms);
// Draw the model. A model can have multiple meshes, so loop.
foreach (ModelMesh mesh in model_ground_land1.Meshes)
{
// This is where the mesh orientation is set, as well
// as our camera and projection.
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = transforms[mesh.ParentBone.Index] *
Matrix.CreateRotationY(cubeGroundLand1_modelRotation) * Matrix.CreateTranslation(position_model_origin);
effect.View = View;
effect.Projection = Projection;
}
// Draw the mesh, using the effects set above.
mesh.Draw();
}
position_model_origin.X = (float)(line +1);
}
position_model_origin.X = pos_reference.X;
position_model_origin.Z = (float)(column +1);
}
position_model_origin.Z = pos_reference.Z;
position_model_origin.Y = (float)(thickness+1);
}
position_model_origin.Y = pos_reference.Y;
position_model_origin = pos_reference;
}
Thank you in advance for your help. I lose patience (over a whole weekend ^ ^)
It's Z-fighting. Z-buffer precision falling with distance so far objects have more "flickers" GPU can't figure out which polygon is on top because tail of the z-buffer value isn't precise enough to distinguish 2 almost equal values.
You have 6 ways to fix it:
Move geometry or do not display part that is below.
Use something like polygon offset in OpenGL
Use CPU z-sorting instead of z-buffer.
Use only one object with 2 textures instead of 2 objects + some shaders (i don't know what exactly you are trying to achieve)
Use larger z-buffer.
Move clip planes closer to each other it will increase precision.
This method that draws my tiles seems to be quite slow, Im not sure exactly whats wrong, it belive my culling method isnt working and is drawing stuff offscreen, but im not completeley sure. Here it is:
// Calculate the visible range of tiles.
int left = (int)Math.Floor(cameraPosition.X / 16);
int right = left + spriteBatch.GraphicsDevice.Viewport.Width / 16;
right = Math.Min(right, Width) + 1; // Width -1 originally - didn't look good as tiles drawn on screen
if (right > tiles.GetUpperBound(0))
right = tiles.GetUpperBound(0) + 1; // adding 1 to get the last right tile drawn
int top = (int)Math.Floor(cameraPosition.Y / 16);
int bottom = left + spriteBatch.GraphicsDevice.Viewport.Height/ 16;
bottom = Math.Min(bottom, Height) + 1; // Height -1 originally - didn't look good as tiles drawn on screen
if (bottom > tiles.GetUpperBound(1))
bottom = tiles.GetUpperBound(1) + 1; // adding 1 to get the last bottom tile drawn
// For each tile position
for (int y = top; y < bottom; ++y)
{
for (int x = left; x < right; ++x)
{
// If there is a visible tile in that position, draw it
if (tiles[x, y].BlockType.Name != "Blank")
{
Texture2D texture = tileContent["DirtBlock_" + getTileSetType(tiles,x,y)];
spriteBatch.Draw(texture, new Vector2(x * 16, y * 16), Color.White);
if (isMinimap)
spriteBatch.Draw(pixel, new Vector2(30+x, 30+y), Color.White);
}
}
}
GetTileSetTypes is a function to get what tiles are around it, for different textures, like DirtBlock_North, DirtBlock_Center, etc.
Tile content is just a class with my block textures.
Try changing SpriteBatch.Begin to defered and combining all of the tiles onto one texture.
See this GameDev question for info about why deferred is most likely the fastest option for you.
Also realize that every time you draw a new texture you have to take the old one out of the GPU and put the new one in. This process is called texture swapping and usually isn't an issue but you are swapping textures twice per tile which is likely to impact performance noticeably.
This can be fixed by combining multiple sprites onto one texture and using the source rectangle argument. This allows you to draw multiple sprites without a texture swap. There are a few OSS libraries for this. Sprite Sheet Packer is my personal favorite.
Unfortunantly without the project and a profiler I'm just guessing; however, these are the two biggest gotchas for rendering tilemaps I know of. I can't really see anything wrong from here. Below is the code I use to draw my tile maps and as you see its very similar to yours.
If all else fails I would suggest using a profiler to figure out which bits are running slowly.
//Init the holder
_holder = new Rectangle(0, 0, TileWidth, TileHeight);
//Figure out the min and max tile indices to draw
var minX = Math.Max((int)Math.Floor((float)worldArea.Left / TileWidth), 0);
var maxX = Math.Min((int)Math.Ceiling((float)worldArea.Right / TileWidth), Width);
var minY = Math.Max((int)Math.Floor((float)worldArea.Top / TileHeight), 0);
var maxY = Math.Min((int)Math.Ceiling((float)worldArea.Bottom / TileHeight), Height);
for (var y = minY; y < maxY; y++) {
for (var x = minX; x < maxX; x++) {
_holder.X = x * TileWidth;
_holder.Y = y * TileHeight;
var t = tileLayer[y * Width + x];
spriteBatch.Draw(
t.Texture,
_holder,
t.SourceRectangle,
Color.White,
0,
Vector2.Zero,
t.SpriteEffects,
0);
}
}
Okay, I have a 2d Tile Map editor I'm working on in xna c#.
In the Draw method I loop through (with a 'for' loop) my 2 dimensional array of tiles so that
my map updates and draws all the tiles every frame.
My question is, how do you draw only the tiles that are seen on screen.
Also is there a better way to draw the tile map (Rather than updating every frame).
In the platformer demo I played around with the visible tiles were calculated and then only those tiles were drawn. I believe you will have to include them in the draw method to be drawn in each time.
Here is a snippet (this only had left to right scrolling so no vertical range was calculated). This kept track of the camera position to calculate it.
Edit:: Added the second method shows how it updated camera position based on the player position stored in a player object.
private void DrawTiles(SpriteBatch spriteBatch)
{
// Calculate the visible range of tiles.
int left = (int)Math.Floor(cameraPosition / Tile.Width);
int right = left + spriteBatch.GraphicsDevice.Viewport.Width / Tile.Width;
right = Math.Min(right, Width - 1);
// For each tile position
for (int y = 0; y < Height; ++y)
{
for (int x = left; x <= right; ++x)
{
// If there is a visible tile in that position
Texture2D texture = tiles[x, y].Texture;
if (texture != null)
{
// Draw it in screen space.
Vector2 position = new Vector2(x, y) * Tile.Size;
spriteBatch.Draw(texture, position, Color.White);
}
}
}
}`
private void ScrollCamera(Viewport viewport)
{
const float ViewMargin = 0.35f;
// Calculate the edges of the screen.
float marginWidth = viewport.Width * ViewMargin;
float marginLeft = cameraPosition + marginWidth;
float marginRight = cameraPosition + viewport.Width - marginWidth;
// Calculate how far to scroll when the player is near the edges of the screen.
float cameraMovement = 0.0f;
if (Player.Position.X < marginLeft)
cameraMovement = Player.Position.X - marginLeft;
else if (Player.Position.X > marginRight)
cameraMovement = Player.Position.X - marginRight;
// Update the camera position, but prevent scrolling off the ends of the level.
float maxCameraPosition = Tile.Width * Width - viewport.Width;
cameraPosition = MathHelper.Clamp(cameraPosition + cameraMovement, 0.0f, maxCameraPosition);
}
int MapSizeX = 20;
int MapSizeY = 20;
int LeftCornerX = 0; //the position of the Tile in the 2Darray that is going
int LeftCornerY = 0; //to be drawn in the left corner of the screen.
int ScreenSizeX = 10;
int ScreenSizeY = 10;
public Tiles[,] tiles = new Tile[MapSizeX, MapSizeY]; //list of all Tiles
//then you can draw it like this....
int counterX = 0; //represents the position on screen
int counterY = 0;
// y and x inside the for loops represents the position in tiles
for(int y = LeftCornerY; y < MapSizeY < y++)
{
for(int x = LeftCornerX; y < MapSizeX < x++)
{
if(counterX < ScreenSizeX && counterY < ScreenSizeY)
{
tiles[x, y].draw(tiles[counterX , counterY]);
}
counterX ++;
//when you do like this you draw the tiles you want
//at the position you want. In the draw method you just
// drawn the tile you want at the position of the tile you
// send as in parameter to the draw method.
}
counterY++;
counterX = 0;
}
then you just have to increase the LeftCorner variables to draw another part of the map