MonoGame: Some VertexColorPosition dissappear while drawing user primitives(DrawUserPrimitives) - xna-4.0

I am a complete beginner in XNA/MonoGame developing. I started my own project using MonoGame with XAML for WinRT, hopefully that it will reach Windows App Store one day. I encountered a serious issue, see the video. I used wireframes so the missing vertices can be easily seen. Only the explosions created by user input are flawless. All of them use the same logic.
I am doing this game with ball collision, pretty simple, indeed. In certain conditions these balls explode and they start to expand following some rules. When the explosion is initiated by user input with the same type explosion, the following explosion do not appear well at all. Some of the vertices of the primitives disappear and they appear as some strange shapes but not circles at all. I tried disabling CullMode(setting it to None), DepthBuffer(setting to false), StencilEnable(setting to false). None of this helped. All of these primitives are in the same z-plane(z = 0). Does anyone have any suggestions? Your help is highly appreciated, thank you a lot. Below you can find the code which gives more details into the situation.
During the update I go through all the objects consecutively, do the necessary updates, and in the same order I call for each of them:
this.graphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, circleVertices, 0, primitiveCount);
This is the BasicEffect that I apply:
basicEffect.Projection = Matrix.CreateOrthographicOffCenter
(0, graphics.GraphicsDevice.Viewport.Width, // left, right
graphics.GraphicsDevice.Viewport.Height, 0, // bottom, top
0, 1); // near, far plane

This will be hard to answer without seeing more code. It appears from the video that there must be an issue when you are generating the circleVertices for an explosion that starts from user input. Would it be possible to post your code somewhere?

Related

Isometric tilemap in unity - sorting issues

ok I've been searching everywhere and cannot understand this, hopefully someone here can help!
I am building a game in unity with an isometric tilemap, and apparently I can't grasp the whole sorting issue, cause nothing I do works as I wish...
to start this is the scene: basic scene with player
the whole basic scene is in one tilemap (I also have a map for collision, floor and upper level but those aren't relevant to the problem in hand - at least I don't think so...), which is set to individual and the graphic settings are 0,1,1 (I also tried changing those with no success)
when the player stands close to a tile it sorts incorrectly incorrect sorting, obviously this is solved if I change to chunk mode but then everything else gets messed up chunk mode, tables sort wrong and also this happens for some reason - the player sorts behind some of the tiles player sorts behind tile
any help would be appreciated
omer

Ive been working on a "mario like" game engine in p5.js, im having some serious trouble with collision avoidance

By collision avoidance, i mean stopping the player from walking through something. Like in mario, he can't just walk through the blocks. I technically succeeded in making this, but it's very very bad. The player often gets stuck on the block once the player hits it, and i cant figure out how to fix it. I put all the code together on an online p5.js editor, Here
In the code i linked, I'm trying to get the player to not go through any of the terrain constructions i make, the one i have currently set up is a red square named 'block1'
Stack Overflow isn't really designed for general "how do I do this" type questions. It's for specific "I tried X, expected Y, but got Z instead" type questions. But I'll try to help in a general sense.
You need to take a step back and break your problem down into smaller steps and then take those steps on one at a time. Can you get a simple program working that just shows two hard-coded rectangles that change color if they're colliding? Get that working perfectly before moving on.
Shameless self-promotion: I wrote a tutorial on collision detection available here. That's for regular Processing, but everything should be basically the same for P5.js. Generally you probably want to use grid-based collision detection to figure out which cell your player is in, and then use rectangle-rectangle collision detection to actually check whether the player has hit a block.
If you're having trouble, please debug your code and try to narrow your problem down to a MCVE and ask a specific technical question. Good luck.

Unity 3D Several objects not showing on the screen everytime the level is loaded

I have a simple 2D game and I have a strange problem whenever a level is loaded several of the objects that include platforms and enemies won't display on screen and neither do the 2D background but all the objects are there, I can see it in the hierarchy and they are functioning as they should like killing the player and such. They just don't show up on the screen. I was finally able to show them while pausing the player during gameplay and set the z position of the camera to -11 and above but every time the level reloads like after the death of player, the same problem happens.
I even tested it on the mobile device but the same problem occurs furthermore I can't even interact with any of the UI buttons on the level, even though I check that all the code for them is appropriate.
Please help.
EDIT:
Okay after reading, your replies, I thought may be this will help. O always get these errors every time I load up this project in unity but they disappear once the game is running.
As you can see the platforms and enemies does appear on the camera frustum but when I start the game they disappear.
Okay, I finally found the problem. After lot of head scratching and testing I found that the objects that weren't appearing on screen has their z-position set to -10 which is the same of that of camera's z-position.
The problem was I was using this code to set the position of objects in the Awake method:
transform.position = camera.main.ViewportToWorldPoint(new Vector3(x, y, 0));
What's actually happening is all the x, y, and z co-ordinates were setting related to the camera's position. So setting the z-position to 10 fixed it for me.
Thanks everyone for helping me.
It's probably because near clipping of your cam is to high; Set it to 0.1 and try it again.

2D Collision Resolution

I am currently working on a user interface, in which it is possible, to add objects by clicking on the screen. I don't want any overlapping objects. While it is easy to detect, whether a collision between 2 objects occurred, i am still struggling with resolving these conflicts.
Currently, i am resolving the conflicts locally by moving the intruding object away from the collision. This, however, may lead to new collisions, which are resolved in the same manner. Unfortunately, there is no guarantee, that this process will ever stop.
Are there any standard problems this relates to or algorithms to use? Or any efficient solutions which are not prone to endless recursion?
Now that I thought of it more I think you just move all the objects that lie in the direction of the move that you are already doing. Doing it this way you basically solve the problem of possible endless recursion.
Since you should know the size of your objects to move and the direction of a smart movement this should solve the problem
"grid snap" is avoiding the problem but not a solution to the problem.
As Sarah pointed out you can solve this by moving your objects a random distance into one direction and then check again if there is a collision, then move the colliding object. However, this can lead to exponential growth of the problem.
Instead you can try to implement a light physics engine where your objects "bounce" against each other and using friction come to a halt after some time. Try to google for continuous collision avoidance.
The easiest way I can think of how you can resolve your problems is by implementing a "grid snap" behavior.
Basically, there are only predefined areas in your frame where the user can add UI elements---the grid cells. When a user drops a UI element on your frame, you should detect where in the grid does it fall mainly (you can choose your own behavior in case it falls equally across two or more grid cells). This way, you need not detect collision between two UI elements at all.
Edit
Well, I certainly did not anticipate the cases you outlined in your comment. If the sizes of your objects will vary by that much, I admit that "grid snap" may not be applicable to your situation---you might end up with a lot of empty space. I was thinking of something along the lines of Visual Basic when I composed my answer (VB, as far as I remember does implement some sort of grid snap behavior).
A minor point though: while you may have your user's best interests at heart by letting them have an exact control of the positioning of UI elements, think of how your users will interface with your program. Positioning things exactly on the screen using a mouse can be punishment and it may just backfire on you. The same, however, cannot be said if you are guaranteed that your users will always use a touchscreen device.

Texture2D.Bounds.Intersect, but the Bounds never move? - XNA, .Net 4.0

I am still shiny new to XNA, so please forgive any stupid question and statements in this post (The added issue is that I am using Visual Studio 2010 with .Net 4.0 which also means very few examples exist out on the web - well, none that I could find easily):
I have two 2D objects in a "game" that I am using to learn more about XNA. I need to figure out when these two objects intersect.
I noticed that the Texture2D objects has a property named "Bounds" which in turn has a method named "Intersects" which takes a Rectangle (the other Texture2D.Bounds) as an argument.
However when you run the code, the objects always intersect even if they are on separate sides of the screen. When I step into the code, I noticed that for the Texture2D Bounds I get 4 parameters back when you mouse over the Bounds and the X, and Y coordinates always read "X = 0, Y = 0" for both objects (hence they always intersect).
The thing that confuses me is the fact that the Bounds property is on the Texture rather than on the Position (or Vector2) of the objects. I eventually created a little helper method that takes in the objects and there positions and then calculate whether they intersect, but I'm sure there must be a better way.
any suggestions, pointers would be much appreciated.
Gineer
The Bounds property was added to the Texture2D class to simplify working with Viewports. More here.
You shouldn't think of the texture as being the object itself, it's merely what holds the data that gets drawn to the screen, whether it's used for a Sprite or RenderTarget. The position of objects or sprites and how position/moving is handled is entirely up to you, so you have to track and handle this yourself. That includes the position of any bounds.
The 2D Rectangle Collision tutorial is a good start, as you've already found :)
I found the XNA Creator Club tutorials based on another post to stackoverflow by Ben S. The Collision Series 1: 2D Rectangle Collision tutorial explains it all.
It seems you have to create new rectangles, based on the original rectangles moving around in the game every time you try to run the intersection method, which then will contain the updated X and Y coordinates.
I am still not quite sure why the original object rectangles position can not just be kept up to date, but if this is the way it should work, that's good enough for me... for now. ;-)

Resources