Sprites flicker in XNA - xna

I have player sprites and drawing them statically is no problem. But on updating and rendering them in the game loop they flicker for some reason. What could possibly cause this? They don't flicker all the time, but just now and then you see a sprite flicker.

it's due to the use of float, do this before draw:
Vector2 FixedPosition = new Vector2((int)SpritePosition.X, (int)SpritePosition.Y);
SpriteBacth.Draw(Texture, FixedPosition,....);

Related

iOS SpriteKit strong collision issues

I wrote few simple SpriteKit games and always had problems with strong collisions behaviour. Even now with my latest game in development the same issue appears quite randomly.
Game-play area is inside edgeLoopFromRect physics body
There are some objects (with circle physics body) bouncing around
But sometimes (this happens very rarely) when a strong collision (edge <- body1 <- body2) happens directly near the edgeLoopFromRect physics body, sometimes colliding-object (body1) jumps out. I am totally confused how to debug this kind of behaviour.
EDIT: I have recorded a video of what exactly happens:
Physics engine collapses
For the demo I have made impulse strength (the biggest blue ball) on purpose 10-times stronger, so the effect can be seen sooner. Look what happens after 10-15 seconds. Physics totally collapses. This can be seen by background totally going off and balls fly into the unknown.
Note: everything I do in this demo is applying impulse on the blue ball.
You need to enable usesPreciseCollisionDetection. This will force SpriteKit to calculate the physics positions of the entire movement, which will now prevent it from popping out. Code for this would look like this in Objective-C:
self.body1.physicsBody.usesPreciseCollisionDetection = YES;
And in Swift:
body1.physicsBody?.usesPreciseCollisionDetection = true
This will have a performance impact, but will prevent your glitch from happening. You will probably have to do this for all of the circular physics bodies that you have moving around.

didBeginContact not called on fast moving sprite

I have a game where a user is dragging around my main sprite. The main sprite collides with other sprites just fine except when the user drags the main sprite very quickly. Sometimes when the main sprite is moving quickly, the physics bodies just pass right through each other and the two sprites suddenly overlap. I have a breakpoint set that logs the hit count at didBeginContact, and it is not hit.
Is there a limit to how fast a sprite can move and still be covered by didBeginContact? Am I allowing the user to move the sprite faster than the game cycle can handle the collisions?
Again, when the sprite is moving at slow speeds, the physics are working perfectly.
Remember, these things are all calculated frame by frame. You're probably moving the sprite so fast that its ending up on the other side of the screen in too few frames to count as a collision. If someone is spastically moving their finger around it might not catch it. You could put some kind of speed limit on the sprite or something.
try to set physic body with usesPreciseCollisionDetection = YES

Lag using SKAction

I've finished my game which uses the method + (SKAction *)moveToY:(CGFloat)y duration:(NSTimeInterval)sec to move sprites and when they collide with another sprite they disappear but when one of them collides with another object and disappears the other sprites that are falling down make some lag and I don't know if I could use another method to move sprites without lag.
PS: The sprites move a little bit fast(from y:0to y:568 in 0.5 seconds), is that maybe the problem?

Drawing a 2D HUD messes up rendering of my 3D models?

I'm using XNA 3.1
I have recently created a 2D Heads Up Display (HUD) by using Components.Add(myComponent) to my game. The HUD looks fine, showing a 2D map, crosshairs and framerate counter. The thing is, whenever the HUD is on screen the 3D objects in the game no longer draw correctly.
Something further from my player might get drawn after something closer, the models sometimes lose definition when I walk past them. When I remove the HUD all is drawn normally.
Are their any known issues regarding this that I should be aware of? How should I draw a 2D HUD over my 3D game area?
This is what it looks like without a GameComponent:
And here's how it looks with a GameComponent (in this case it's just some text offscreen in the upper left corner that shows framerate), notice how the tree in the back is appearing in front of the tree closer to the camera:
You have to enable the depth buffer:
// XNA 3.1
GraphicsDevice.RenderState.DepthBufferEnable = true;
GraphicsDevice.RenderState.DepthBufferWriteEnable = true;
// XNA 4.0
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
SpriteBatch.Begin alters the state of the graphics pipeline:
SpriteBatch render states for XNA 3.1
SpriteBatch render states for XNA 4.0
In both versions depth buffering is disabled, that's what causes the issue.
Again, I cannot stress this enough:
Always make sure that ALL render states are correctly set before drawing any geometry.
BlendState
DepthStencilState
RasterizerState
Viewports
RenderTargets
Shaders
SamplerStates
Textures
Constants
Educate yourself on the purpose of each state and each stage in the rendering pipeline. If in doubt, try resetting everything to default.

Draw order of 3D-Models in XNA

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.

Resources