Creating A Racing Line For an AI Path - swift

I am currently creating a top down racing game ( or trying! ) using SpriteKit. I am creating tracks procedurally with catmullrom splines created from points. I have access to three sets of points, the inside of the track, the outside of the track and the centre line of the track.
I am using GKAgents, GKGoals, and GKBehaviours from GameplayKit for the AI cars, and I currently have a few cars following a GKPath. The points I have provided to GKPath are the centre line shown in yellow in the image.
My maths skills are not all that! So my question is given that I know the inner, outer and centre points, how can I modify my centre line points to create a more realistic racing line for the AI cars to follow?
If you need a clearer idea of what is going on, you can see the central points in a video: https://www.youtube.com/watch?v=yhwNL0iCBuU

Related

Unity - Tilemap Contains - how to read if Tilemap contains a tile on a specific coordinate in world?

I'm a complete noob in gamedev, but I've watched a number of videos on generating a 2D array to setup Grid-based combat (pathing, obstacles etc), and I don't find the programmable approach intuitive or visually friendly.
Is it possible to setup such level with obstacles using multiple tilemaps?
1st tilemap would include the whole level zone (I named it "General Tilemap"):
2nd tilemap would only contain tiles that would be marked as collision when being read (I named it "Collision Tilemap") and player wouldn't be able to move to them:
My logic would be to read the adjacent tiles around the player, and if:
A tile exists on the General tilemap, but not on the Collision tilemap, player can click it and move there.
A tile exists on both tilemaps, it is marked as collision, it cannot be clicked.
A tile doesn't exist, it is out of boundaries, it cannot be clicked.
Could you please let me know if this is a valid approach (for smaller levels at least, I won't be making anything large so scalability is not an issue), or have I gone completely off course and there's a superior way to do this properly?
I'm currently stuck at the very first step - reading whether the tile on a coordinate (next to player) is existing or null for both tilemaps. Doing my best to figure it out though.
Thanks!
Managed to check if tilemap contains a tile on xy coordinates in Start function, by finding the relevant Tilemap and using hasTile to read it it has value or not. This returns a boolean.
Tilemap generalTilemap = GameObject.Find("General Tilemap").GetComponent<Tilemap>();
hasGTile = generalTilemap.HasTile(playerTileCoord);
Still not sure if this approach will work for me long-term, especially when I get to the pathfinding algorithm, but we'll see!

I want to check if a line between two vector points falls into a particular area or a set of vector coordinates?

Note : I am coding in Unity, using C# script. I cannot use trigger hit detection using Raycasting because there are a lot of trigger colliders in between the target and source which detect touches and the sort. So the ray hits the other triggers before even reaching it's target, which is not desirable.
What I want to accomplish basically is return a boolean value if a vector line crosses or intersects a particular set of vector coordinates or an area.
For example: Detecting a laser entering into a fog in between it's path when shooting at it's target. The fog is a trigger collider based game object.
Edit: Another example would be to check if a line crosses a 2D box area in a 2D graph. Keep in mind, I cannot use collision detection or Raycast hit here.
There is no need for code, just explain the concept of how it could be accomplished. Though a code snippet is also welcome. Thankyou!
[...] So the ray hits the other triggers before even reaching it's target, which is not desirable.
What about putting that on Layers? You can specify LayerMasks for Raycasts.
You could consider the edges of the 2D box as lines i.e 4 lines and check for intersection of a line with these 4 edges using line to line intersection technique. If the line however is small enough to fit inside the box, and you want to treat it as a valid intersection, then check if the two points of the line is inside the bounds of the box.

How Does Unity Assign Pivot Point Location on Script Generated Meshes

I have tried to find any information on how the Unity assigns pivot points to object but all I keep finding is threads on how to move pivot points and that it can't be done. I am creating a 2D game with a background that is randomly created with meshes that are wrapped in empty GameObjects. These objects are organically shaped but they have a property that returns a rectangle that bounds the object so that they can be placed in a way that they are not overlapping. The trouble is that the algorithm assumes that the pivot point is going to be the center of the object. What I would like to know is how does Unity decide where the pivot point will be set to so that I can predict how much I will need to move my mesh inside the parent object so that the pivot point will be in the center of the bounding rectangle.
Possible fix:
Try create the meshes during runtime and see if it always places the pivot points at a certain corner or at least relatively speaking the same location.
If it does that you would know where the pivot point is and could take it into account in your code, if you also know the size of the mesh you spawn.
So I think most general and correct answer that I can come up with is that unity assigns the pivot point to the center of the GameObject that you apply the Mesh to. The local coordinates of the vertices of the mesh depending on how you create them mighht place your mesh so that its logical center is not the same as the that of the empty GameObject that it is attached to. What I did to fix the issue was to make a vector from local point (0,0,0) to the center of bounding rectangle and translate the vertices I use to make my mesh by that vector inverted. It wasn't perfect but by far close enough to ensure that I won't have any overlapping meshes.

Unity3d Dungeon generation

I,m creating a dungeon generator in unity3d. I have a pile of prefabs(rooms, corridors, junctions). Randomly a piece is chosen for starting piece and then depending on the connecting rules next pieces are chosen. Also the dungeon can be multi layered. Like 1 room above the other etc. Problem is room collisions. On the same layer 2 rooms can be placed on top of each other and this is a problem.
My first idea was to use arrays to model the layers and then find best suiting locations for stairs up and down. But the problem with this is that not all angles are 90degrees. I have a 3 way intersection in the shape of Y. So everything that comes after this is angled. And its not easily represented on array.
Second idea was the use of collisions and backtracing. I place the tile and then if no collision I can move forward. But if collides I try to find a better suiting piece or remove the previous piece and but something new there. But I can't figure out a good method to do this.
Anyone have any suggestions or ideas on how to generate multilayered dungeons with non 90 degree angles?

Open GL - ES 2.0 : Touch detection

Hi Guys I am doing some work on iOS and the work requires use of OpenGL es. So now I have a bunch of squares, cubes and triangles on the screen. Some of these geometries might overlap. Any ideas/ approaches for touch detection?
Regards
To follow up on the answer already given, squares, cubes and triangles are convex shapes so you can perform ray-object intersection quite easily, even directly from the geometry rather than from the mathematical description of the perfect object.
You're going to need to be able to calculate the distance of a point from the plane and the intersection of a ray with the plane. As a simple test you can implement yourself very quickly, for each polygon on the convex shape work out the intersection between the ray and the plane. Then check whether that point is behind all the planes defined by polygons that share an edge with the one you just tested. If so then the hit is on the surface of the object — though you should be careful about coplanar adjoining polygons and rounding errors.
Once you've found a collision you can easily get the length of the ray to the point of collision. The object with the shortest distance is the one that's in front.
If that's fast enough then great, otherwise you'll probably want to look into partitioning the world or breaking objects down to their silhouettes. Convex objects are really simple — consider all the edges that run between one polygon and the next. If only exactly one of those polygons is front facing then the edge is part of the silhouette. All the silhouettes edges together can be projected to a convex 2d shape on the view plane. You can then test touches by performing a 2d point-in-polygon from that.
A further common alternative that eliminates most of the maths is picking. You'd render the scene to an invisible buffer with each object appearing as a solid blob in a suitably unique colour. To test for touch, you'd just do a glReadPixels and inspect the colour.
For the purposes of glu on the iPhone, you can grab SGI's implementation (as used by MESA). I've used its tessellator in a shipping, production project before.
I had that problem in the past. What I have used is an implementation of glu unproject that you can find on google (it uses the inverse of the model view projection matrix and the viewport size). This allows you to map the 2D screen coordinates to a 3D vector into the world. Then, you can use this vector to intersect with your objects and see which one intersects (or comes really close to doing so).
I do hope there are better ways of doing this, so I look forward to other answers as well!
Once you get the inverse-modelview and cast your ray (vector), you still need to know if the ray intersects your geometry. One approach would be to grab the depth (z in view coordinate system) of the object's center and extend (stretch) your vector just that far. Then see if the vector's "head" ends within the volume of your object or not (you need the objects center and e.g. Its radius, if it's a sphere)