It may be a noob question but here it is...
I'm using Visual Studio with the XNA Framework (3.1) and I'm only going to draw 2D sprites for my game, here is the Draw Method in the main class:
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
background.Draw(this.spriteBatch);
player1.Draw(this.spriteBatch);
player2.Draw(this.spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
The problem is that the player2 is overlapping player1 (because he is drawn after), and I'd like the lowest one of the 2 players (by their position) to be drawn last (to simulate depth).
Thanks in advance! (and sorry for my Engrish, I'm French...)
There is an overload of the SpriteBatch.Draw method which offers a 'layerDepth' parameter (from 0 for front, to 1 for back). You can set an individual sprite's layerDepth and use spriteSortMode.BackToFront in the SpriteBatch.Begin method.
Ok thanks to Steve I did it.
I just changed my player SpriteBatch.Draw method to SpriteBatch.Draw(X,X,(...),layerDepth)
with layerDepth getting updated with the Y position of the players.
Had to use spriteSortMode.FrontToBack: everything was reversed because of the origin being in the upper-left corner.
Thanks!
Steve's Answer is probably easiest , but sometimes Immediate drawing is favorable.
An alternative is sort your players and objects in a list based on their Y axis.
Related
I want to determinate the intersection's Rect / Polygon of 2 overlapping SkNodes in Sprite-kit.
The method :
SKNode.intersectsNode(<#T##SKNode#>)
returns only true or false if there is an intersection, but i want to know also the intersection's area.
Can somebody help me? (I want to determinate the green triangle)
Googling "area of intersection of two rectangles" shows this has been addressed multiple times on SO, like here, and here.
As far as Sprite Kit specific: you are going to have to implement some logic (to test for intersection, containment, etc...) and math (to calculate the area of the intersection,) AFAIK there is nothing built into SK to do it for you.
I am making a basic app that pushes shapes across the screen and detects collision with Sprite Kit. My first attempt was using moveTo on the nodes. The issue I had was with collision, the objects would rotate around each other instead of bounce.
Therefore I found I need to use applyForce OR applyImpulse.
In this situation I have a circle for example that is position off screen at its start of life. We then determine a target exit point, and want to 'flick'/'push' the node in that direction.
I cannot figure out how to applyImpulse towards the target end position I have as a CGPoint. I need to get this to a CGVector but I am not sure what needs to be done. I had a look around and found some Ray tuts but they just show applyForce or moveTo. I am not sure how to calculate this.
I found a site that explains 2D physics well.
http://www.rodedev.com/tutorials/gamephysics/
With this I worked out what the angle needed to be and have a speed that I can control and it works well.
You can move an object by changing manually the x and y position so you can reach your end point. In the update function you change yourObject.position.x and yourObject.position.y if I have understood correctly your question. If not please be more explicit. Hope that helps.
Recently I have a new project that uses Box2D as physics engine. And I am having some trouble with the body's position.
I want to draw images on the sprite, so I just use body.GetPosition as image's position. But I found that body.GetPosition returns the same value as body.GetWorldCenter, and I just wondering that I did something wrong.
I use box2d flash 2.1a in my project. And I currently subtract this position with half-width and half-height to walk around. Also, can you tell me if there is some way to solve my problem or draw the image in the correct positon in Box2D.
Thank you.
update
so sorry to post before read manual.
after RTM I got that body has two points,first is origin point and the second is center of mass ,so a regular shape's world center is always the same as it's origin point. sorry to post is.
Have you set the image in the userdata for the body? Your getting the body's position but you need to have a function that runs each world tick that can set the images position to the new location. Box2d is just numbers defining a simulation. Those numbers are constantly updating so the image needs to have position set each time there is a game tick. Hope this helps
i'm having trouble with drawing 3D-Models in the right order in XNA.
Here are two images that describe the problem:
Pic1: http://imgur.com/wGPIk&L5AY1l
Pic2: http://imgur.com/wGPIkl&L5AY1
The ball is moving downwards and as soon as it intersects the terrain, the perspective is changing so that it looks like as if the ball is behind the terrain.
If i change the order of drawing, it will look like the ball is on top of the terrain all the time..
Can someone please explain how to solve this problem?
Thanks in advance
The problem is the SpriteBatch call that draws your FPS counter in the corner.
SpriteBatch changes the state of the graphics device. Whenever you're done drawing your 2D objects (in this case, your frame rate counter in the upper-left corner), you need to restore the device state.
After calling SpriteBatch.End(), add this code:
device.DepthStencilState = DepthStencilState.Default;
where device is a reference to the current GraphicsDevice that you're drawing with.
This looks like a ZBuffer problem. Have you checked that your Zbuffer is enabled and working? Try what David Lively said. if that works, check that you don't draw your 3D models Inside the Begin and End calls of the SpriteBatch class.
Begin readies the device for drawing 2D objects, if you try now to call 3D drawing this can't work. End makes sure that everything, set for 2D, is unset and reverted to the states before the Begin call.
Okay im working on making a tic tac toe game for one of my game development courses using XNA 4.0 I need to place sprites or some other objects so the game can check if the mouse is being clicked in the correct spots. I am going to use transparent sprites as a kind of button. How do I code them to go to these specific x,y coordinates. The game board is drawn on the background, I have all the coordinates to where to place these sprites. I am just stuck on putting the sprites in the correct positions.
SpriteBatch.Draw has a position parameter. Pass in an appropriately-valued Vector2.
Well if you check the Draw method you will find a parameter for the position.
Check this code sample
spriteBatch.Begin();
Vector2 pos = new Vector2(10, 10);
spriteBatch.Draw(SpriteTexture, pos, Color.White);
spriteBatch.End();
This is how you draw a sprite, with SpriteTexture as the image, on the position x10, y10 with the color White to modulate the texture.
You can also find more informations here.
Keep in mind that there are many overloaded methods to the Draw method. One you can even pass in rotation information and the like. So .Draw(...) has a lot of functionality you can use beyond just placing a sprite.