Unity 3d ondular terrain generation - c#

I am making a 3d game where i have to do some terrain generation to have an infinite and random level.
The terrain has to be ondular with ups and downs, like this:
https://imgur.com/a/prTuRl0
i used perlin noise to generate a terrain, with multiple planes in a queue, updating them as the player goes forward(i have the Z position of the last plane, so when i update when i peek from the queue the first in line, dequeue it and enqueue it again with new vertices, through mathf.PerlinNoise(0, zPosition))
the reason why i have a 0 in the first parameter is so that the planes are uniform in the x axis
i'd like to have a gameplay similar to dunes but in 3d, where the player controls a ball, clicking while grounded gives it speed, clicking while on air brings it down, and he can get score and streaks by smoothly going through downhills or die if he crashes against an uphill
problem:
https://imgur.com/a/sdZp1qQ
perlin noise isn't always up and down as i need it to be, sometimes it has those curves in the middle that make the ball jump and have weird not desired movement... appreciate any help on this subject.

I fixed this problem by using the Normal Distribution Function to generate the waves, by messing with the parameters, i can add width or height to my wave
Using 5 planes per "wave" (each wave is a gameobject array of 5 planes), and simply going through the vertexes in the x axis, changing all their heights to the same value calculated in the function, incrementing x and doing it again

Related

Get Y value from a mesh or mesh vertices without raycast

I been stuck at this specific problem and have not been able to rack my head around it.
Easiest way to describe my problem is I have a 100 by 100 mesh that is randomly generated along the Y value points. I THEN spawn in trees rocks grass ect depending on the room value. The problem I have is I have been unable to successfully get the Y value of the plane at the specific location.
I have tried raycast and return the value unfortunately it is too slow as my world is 100% randomly generated in chunks and tends to mess up. I also tried attaching the objects to a plane then set that plane equal to to the mesh but unity combine meshes is not suitable for the amount of objects I have on the plane.
I was wondering if I could somehow compare mesh vertices with the game object x and z to return a Y.
Update*
enter image description here
And the search continues.

Does Unity move object by the magnitude(length) of vector value?

Most of you may know that moving object diagonally is faster than moving object horizontally or vertically. You can watch a short youtube tutorial here. If an object is set at (0,0) then using this code.transformation.postition += new Vector3(x, y, 0f).Assume moving object on 2D plane. When looking the object position at 2D grid(cartesian plane), for value x=1, y=0, it move one position right, for value x=0, y=1, it move one position up and for value x=1, y=1 it move one right and one up. When look at the following image, the line drawing diagonally is obviously longer than others. I read about vector, the magnitude of vector, Pythagorean theorem. Assume an object move one unit every frame. Then after 1 frame passed all objects will be moved 1 unit exactly. But when calculate in magnitude[length] of vector the units are different. I mean 1 in X or Y and around 1.4 in diagonal. Aren't object moving from point to point? Does Unity move object in vector's magnitude value and set the point on X and Y plane? How do object actually move?
Unity is a frame-based engine. So Unity calculates the positions where objects are in a frame, and when it comes to calculating the next frame, the positions are calculated there as well. One could assume that the object has moved between these frames and conclude that, for example, a projectile must have hit a thin object between the two frames.
If you want to move an object consistently in any direction, first clamp the direction with normalized and then add a custom magnitude.

xna collision without max speed

I'm making a platform game and I've looked at different ways to add collisions in a tile based game without rotation. But everything I look at assumes that in no case will one of the colliding objects be going fast enough to pass the other before collision is detected. I've tried using Box2D and Farseer but they were over complicated and ran quite slowly when making lots of tiles. I also tried my own method using 2D convex hulls but that ran too slowly as well. So is there a way to to detect collisions without a maximum speed or letting items pass each other that isn't over complicated and would work with lots of tiles? any help is much appreciated
Depend on objects and their movement and whole gameplay. if you are shooting bullet to some obstacle, you can calculate if bullet will intersect with obstacle before bullet hit it. you have positions, you can calculate distance between them. so you can check collision and distance.
other way, as #cwohlman suggested. you can have 2 collision boxes, one big "assumed" collision area and one excact that cover objects. so if object triggers assumed collision, you know that there i a chance that bullet will hit object and you do some detailed calculations if button will hit object or will pass above or under it. for determinating if object is in assumed collision area you can use circle collsion.
private bool DetectCircularCollision(Sprite a, Sprite b)
{
float radius = Math.Sqrt(a.Width / 2 * a.Height / 2 + b.Width / 2 * b.Height / 2);
float distance = Vector2.Distance(a.Position, b.Position);
if (distance < radius) return true;
return false;
}
so far i didn't encounter this problem, as all flying objects should be visible to players eye. if you move object 50px or 100px per frame player will not notice this or will be confused what is this.
You will probably need to combine few collision methods to achieve what you need. Advice, do not use pixel collsion as it's completly unnecessary and it's resuorce killer. Instead, cover objects with few bounding boxes or circles.
example where blue circle is assumed collision area, and red circles are exact collision area.
You might try a dual algorithm approach, using a simple algorithm first to deturmine if the objects 'might' have collided and if if so, use a complex algorithm to see if they did collide.

How to determine if an object is between two other objects in XNA

I have a screen filled with circles. One circle is the player, one is the enemy, and the others are obstacles. I want the enemy to be able to calculate if there is an obstacle in the path of a straight line from the player to the enemy, so it can adjust accordingly (it is taking place in space, so it is a straight line for jumping from asteroid to asteroid).
Right now, I just turn a random direction when the AI is stuck on an asteroid.
A simple solution could be find the Vector2 between player and enemy (using Vector2.Subtract), and then divide it into smaller vectors or reduce it to a delta that you choose. This can be done normalizing the distance vector and multipling it by a fixed number.
Then simply add that delta to the enemy position and check if the new position collides with an objects (maybe using Rectangle.Contains method), and do this until you reach the player.

XNA Making a 3D object move with the mouse

I'm making a small game in XNA. I have a camera up in the air 20 pixels on the y axis. Below it I have a grid of tiles that are 100x100. Right now what I'm trying to do is have a 3D object move with the mouse along the X and Z axis of the grid. I'm using viewport.unproject to convert the 2D screen coordinates to 3D ones, but whatever I try it doesn't seem to be quite right. At the moment I have this:
Vector3 V1 = graphicsDevice.Viewport.Unproject(new Vector3(mouse.X, mouse.Y, 0f), camera.Projection, camera.View, camera.World);
If I use this then it moves, but only by a tiny amount. I've tried replacing the Z axis with a 1 and but then it moves a drastic amount (I understand why, just not really sure how to fix it).
I've tried various other methods such as having 2 vectors, 1 with a 0 Z and 1 with a 1 on the Z and then subtracting them/normalizing them but that wasn't it either.
The closest I got was multiplying the result by the amount it's zoomed, but it wasn't perfect and was slightly offsetting and would go crazy whenever I scrolled the screen so I figured that was the wrong approach too.
Any help would be greatly appreciated, thanks.

Categories

Resources