How do I make dynamic 2D collisions faster in unity? - c#

Currently I'm working on a 2D game with destructible terrain in unity. It works great! Except for one thing.... collision generation... Since the terrain is destructible, I have to generate collisions on the go. I tried and it's awful performance. From 1000 fps from editing the terrain to 1 fps when I enable collisions generation. This is a huge issue and I know it's possible within unity because this guy :
https://forum.unity.com/threads/wip-nimbatus.221798/
created it with collisions as well. I tried contacting him but no response yet! Any of you guys have any ides on what I can do? Thanks!

I found a rather annoying but working solution to the issue. So basically what was wrong was I was setting points as vertices. The guy I messaged told me that at the time when he made his game, the 2D engine wasn't a thing in unity so he was forced to use regular 3D components. I didn't want to do this because it would possibly limit what I can do with 2D packages since I am not using the 2D engine. But in the end I decided that unless I could find a extremely fast edge finding algorithm that supported concave meshes WITH holes, I would need to just use the regular Unity3D MeshCollider component. I'm not sure why I was having an issue with the Polygon collider as I thought it would work the same as the MeshCollider but in 2D. I think the issue was I was setting the path of the Polygon Collider as each vertice. This is wrong because I think I would need to set the edge vertices only. (My mesh does not share vertices so I'm not sure how to find the edges) Of course I could re write it to share vertices but I think I will be find with the 3D components (For now at least)

Related

How to create multiplayer / shared AR game with Vuforia?

I would like to create a shared AR game on Android phones, where I would like to:
spawn a cube for each player on an ImageTarget
allow them to control the position of their cube
allow them to see the movements of all players' cubes
I'm using Vuforia as my AR library and PUN 2 as my networking library. I have no issue synchronizing the positions and rotations of all cubes. However, the cubes do not stay on the ImageTarget properly and "jump" around. On the other hand, if I place my two phones very close together and point them at the ImageTarget at roughly the same angle, the cubes do not jump as much.
This leads me to think that the 2 instances of ARCamera fail to realize that they are pointing at the same ImageTarget from 2 different angles, and instead think that the ImageTarget exists in 2 different orientations at the same time.
Is there any way for me to tell Vuforia that I'm using multiple instances of ARCamera pointing at the same ImageTarget? (Or if my hypothesis is completely wrong, how do I actually make a multiplayer AR game?)
Thanks so much in advance!
p.s. I know the Vuforia forums are a better place to ask this question but unfortunately that forum is not particularly active, so I'm trying my luck here.
I've solved the issue by going to the ARCamera gameobject, then in the Vuforia Behavior component, I changed the World Center Mode from DEVICE to FIRST_TARGET. This allows multiple instances of ARCameras to be in different positions.
More info on World Center Mode can be found here.

Unity - possible to have bending grass? Mesh?

Ok here is what Im trying to achieve - when user's feet collide with grass mesh, the mesh is secured to the ground at the base but the tops move away, then bounce back when foot is no longer there:
This example, and everything I've found however, are using billboarding/2d as grass. I want 3d, like these:
https://assetstore.unity.com/packages/3d/vegetation/plants/grass-toon-76674
Where some sort of collision is occurring, or Ive tried experimenting with unity's trees. Trying to find the most CPU/GPU effective (for Oculus app or mobile) way to do this.
Is this possible - to have interactive, 3d not 2d grass?
The only thing I can think of is to model each blade of grass as several bars connected by universal joints (I'm not sure what kind they would be in unity, but that is the mechanisms term).
Each joint could have a torque proportional to its angle, so the more you bend it, the harder it tries to flick back. Then its just a matter of adding forces to the blade structures as you bump into them.
This is more of a engineering solution than a programming one, and would require a little bit of math, so its probably not ideal. but it should work.

Silverlight Collision Detection when controls are in different canvasses/layers

For our game we have to create collision detection.
The problem is that the collided objects are in different canvasses/layers, which makes collision detection by pointlocation inpossible.
Does anyone have an idea how to solve this?
It's hard to give a great answer without some more information, but if all your layers are the same size then you can just roll your own collision detection. All you need to know is the locations and sizes of two things to be collision detected. Then you just test to see if one rectangle intersects with the other rectangle.
There is also a function that might be useful to use called TranslatePoint. This translates from one UIElements coordinates to another. So if you had a ball bouncing around in a smaller area of the screen with it's own local coordinate system, you could get the ball's coordinates relative to the entire screen with this function.
Can I suggest you try using the Farseer physics engine just to save yourself some pain?
http://farseerphysics.codeplex.com/
It's very good and is in use in some WP7 games already.
http://www.farseergames.com/
There's also some Blend behaviors and helpers to make using it even easier:
http://physicshelper.codeplex.com/

Tile based collision in XNA

I have drawn tiles in my XNA game and loaded my character. My character, however, doesn't move- the map does, which gives it the illusion of movement. Now I am wondering how to actually test against them for collision. I mean, where does the collision code go and how do I make all tiles represent 'one big thing'?
There's a tutorial on pixel based collision detection on XNA Creator's club. You'll need to figure out what objects you want to do collision detection on. I guess you want the character to move across the tiled background, so you don't want to check for collision between your character and the background. Instead you should make any obstacle s sprites and do collision detection on those.
You might have a look at Nick Gravelyn's Tile Engine Tutorials, it goes through the whole process of creating a tile engine. There's a link here to see all the tutorials on YouTube.
You could have a look at the Platformer Starter Kit, it shows how to organize tiles in a map and check for collisions.
Rectangles have a intersects method. If your player is centered and you know the coordinates, loop through the other texture2ds and check for an intersection before scrolling the map.

XNA C# 2D Platformer Game

I'm trying to make (figure out how to..) a 2d platformer in XNA.
I know how to create a tile grid and how to perform collision detection.
I perform collision detection on the 9 bounding tiles of the player, but I'd like to know if there's a better way to perform collision detection.
I've read Braid doesn't use tiles but pieced images or something, how do you perform collision detection on those? and is that better than using tiles?
Assuming you don't mind using a third party tool to do the majority of the work for you, you may be better off using something like Box2D for C#.
http://www.box2d.org/
http://code.google.com/p/box2dx/
This library will give you collision detection and physics capabilities.
If you're going for a retro style then it's certainly fine to use tiles! However, if you want something more "modern", then you can go with a more conventional physics engine. Farseer Physics is a great engine, and several people have used it to make platformers. It's based on Box2D and similar engines, but offers a simpler API and several unique features (like texture to geometry) and i myself can testify to it's niceness having played around with it a bit.
You probably need a bit more than collision detection for a game like Braid. I would assume that you also need a physics engine. I would recommend that you take a look at Farseer Physics engine, which is 2D engine that works very well with XNA. It supports a number of different techniques for collision detection.
Have you checked the examples in the collision serie on the XNA creators club, in particular the pixel-collision sample?

Categories