Issue implementing proper collision in XNA Game Studio 4.0 - c#

I am building a game in XNA 4.0, where a player moves about a 2 dimensional (vertical perspective) map consisting of blocks. My issue is creating proper collision between the payer and the blocks (basic game physics.) The player moves more than 1px per frame, so .Intersects() just isn't enough, I need physical contact collision that can function in a gravity environment. The current version I currently have is a piece of garbage and only works occasionally.
Basically, all that the collision system needs to do is stop gravity when the player lands on a block, and provide some decent physics when the player hits blocks (movement in that direction ceases). The idea behind my current solution is to move the next Position around until it finds a clear spot, but it doesn't work well. I have an idea why, just have no idea how to do it properly.
I know there must be a better way to do this. What would be the best method of making this kind of collision work properly?
Thanks

This does the job perfectly, you just need to sort through a few syntax errors. http://go.colorize.net/xna/2d_collision_response_xna/

Related

How to make realistic shot in Unity

I'm just wondering how to make shots in my game more realistic. I mean there are two main ways in implementing shots. First: throw a small projectile and let it detect collisions. Second: use raycast. But in real world (and apparently I noticed this in PUBG) bullets fly really quick but not immediately. Here is why you should aim near your target if it's far away from you to hit it. Because if you aim right where it is it will move and your bullet will miss the target.
I'm just curious if any of you, guys have a nice solution to this problem. Also I wish to find a way to use raycast not every frame. In such things like detecting if you will actually hit the wall when shooting. If you have any good ideas how to implement spread and recoil for different types of weapons I will be happy to read them.
Blue point is bullet point of frame. and in very frame send a new raycast to check cross any human. like red line in this picture.
and bullet path use unity built-in Physics system to do it.

BuildNavMesh is too slow

NavMeshSurface.BuildNavMesh();
I want to do "Bake" of NavMesh dynamically. My game is like Minecraft.
I move the MOB around the world. Player can place blocks. So, if I do BuildNavMesh() I will be able to "Bake" NavMesh with new blocks.
Blocks is placed by the player. And every time player puts a block, I do BuildNavMesh(). Players can jump on them. Player can also destroy those blocks.
As I mentioned earlier, I do not know what to do other than that.
I can only write the above code.
NavMeshSurface.BuildNavMesh();
The best result I want is to use NavMesh to dynamically change the MOB's range of motion.
My navmesh was also building extremely slow. 2 things that really helped were:
play around with the tile size on the NavMeshSurface component, the smaller the faster it will build but it has some tradeoffs.
remove unnecessary layers from you NavMeshSurface component. And this was really the biggest issue. I had the default layer and ui layers in there which caused a big lag, removing them almost instantly rebuilds the navmesh in runtime!

Unity3D Changing Ball Direction When Hitting a Cube

I'm working on a simple Paddle Game project nowadays. In the begining, everything's looking good. But, when I complete my level design and publish my game, I see gameobjects in my scene are moving so slow. I think, I pass over the Unity3D's physics limit. If I try some Math instead of Unity3D's Colider, I can finish my first project. (I tried to use Separating Axis Theorem, but I can't handle x and y coordinates on Unity3D.)
I need your help. Thanks a lot for your time. (And If I can handle this problem, I will share my project on the internet, for beginner people like me.)
In my project, I achieved this simulation with using BoxColider, but because of Unity3D physics limit, I don't want to use colider in my project.
You can't not detect collision. I'd recommend using Unity's colliders for that, since they're really not bad. The first thing I notice is that in your first simulation, you have 4 box colliders, when instead you should just have one. Use the OnCollisionEnter event (on the spheres) to reflect them off your box.

Controlling Player Movement After Per Pixel Collision

I have implemented a full per pixel collision system that accounts for rotation, and its very accurate. It returns a simple bool on collision.
However I am not sure how to handle the collision from the player movement point of view.
E.g. In the picture above, if the player is holding up on the left stick, he should be stopped, but if he is holding up + right diagonal on the left stick, he should slide northeast alongside the side of the red square almost naturally.
How do I go about this, to make the player’s momentum stop, but still give control for the player to move in direction not blocked by a collision.
I could do this kind of thing with simple untransformed rectangles, but going into per-pixel texture collision has made my brain explode today so I’m hoping you guys can help. Any advice would be massively appreciated.
It is possible. For that you need to implement physics engine with your pixel based collision if you are willing to give natural effect in your game.
For that either you write your own game engine or use engine that is already there. For 2D game I highly recommended Farseer Physics Engine. It is out there for a long time. Now in stable condition and it surely wonderful.
It is developed using XNA only from scratch, and also performance wise it is far good.
Have a look at this. I hope I was able to give you answer. Please let me know if more details needed.

XNA collission detection vs. game speed

I have been coding for some years in C# and have now decided to try out the XNA framework to make games.
It went great until I started to implement collision handling in my very simple game.
I can understand how to do collision detection using BoundingBoxes and BoundingSpheres, but after looking at the refresh rate in my game, it quickly became a concern of mine if the two colliding objects were never detected as colliding.
Let me try to explain with an example:
- If a character is shooting with a gun at another character.
- The bullet is heading straight for the other character.
- The bullet gets rendered just before the character.
- Because of the bullets high velocity it now gets rendered on the other side of the character.
In this scenario the bullet and the character never collides, because they are never rendered in their colliding state.
So how do you make sure to detect a collision in this scenario?
For very fast-moving objects, the regular approach fails in the scenario you described. What you need to check for is whether the bullet has collided with any item in the interval between the two consecutive game ticks. This is called continuous collision detection (this is a rather related SO post).
You can do this by basically casting a ray between the middle of the bullet's binding box in the current position and the middle of the box from the old position, and checking if that collides with any other blocks/spheres. This is a rather fast solution and if your bullets are small enough, it should work fine.
For more precision, you can cast four different so-called collision rays from each corner of the current bullet box, to their corresponding positions from the previous game tick. Note that in the rare event of high-speed items smaller than bullets, this might also fail. In this case you would need a more advanced collision detection system. But this would just be a corner case.
If that extra precision is a must, a free 3D physics library, such as Bullet could represent a solution. It even has bindings for XNA!
I think your problem is in tunneling that matches your description.
I recommend you to use Box2D physics engine for XNA. It's fast, simple and will avoid your any problems with collisions in your game.
Check in this manual on 4th page continuous collision and check how Box2D deal with tunneling effect.

Categories

Resources