• 12
• 27
• 9
• 9
• 20
• ### Similar Content

• By GytisDev
Hello,
me and few friends are developing simple city building game with unity for a school project, think something like Banished but much simpler. I was tasked to create the path-finding for the game so I mostly followed this tutorial series up to episode 5. Then we created simple working system for cutting trees. The problem is that the path-finding is working like 90% of the time, then it get stuck randomly then there's clearly a way to the objective (tree). I tried looking for some pattern when it happens but can't find anything. So basically I need any tips for how I should approach this problem.
Use this image to visualize the problem.
• By owenjr
Hi there!
I am trying to implement a basic AI for a Turrets game in SFML and C++ and I have some problems.
This AI follows some waypoints stablished in a Bezier Courve.
In first place, this path was followed only by one enemy. For this purpose, the enemy has to calculate his distance between his actual position
to the next waypoint he has to pick.
If the distance is less than a specific value we stablish, then, we get to the next point. This will repeat until the final destination is reached. (in the submitting code, forget about the var m_go)

Okay, our problem gets when we spawn several enemies and all have to follow the same path, because it produces a bad visual effect (everyone gets upside another).
In order to solve this visual problem, we have decided to use a repulsion vector. The calculus gets like this:

As you can see, we calculate the repulsion vector with the inverse of the distance between the enemy and his nearest neighbor.
Then, we get it applying this to the "theorical" direction, by adding it, and we get a resultant, which is the direction that
our enemy has to follow to not "collide" with it's neighbors. But, our issue comes here:

The enemys get sepparated in the middle of the curve and, as we spawn more enemys, the speed of all of them increases dramatically (including the enemies that don't calculate the repuslion vector).
1 - Is it usual that this sepparation occours in the middle of the trajectory?
2 - Is it there a way to control this direction without the speed getting affected?
3 - Is it there any alternative to this theory?

I submit the code below (There is a variable in Spanish [resultante] which it means resultant in English):

if (!m_pathCompleted) { if (m_currentWP == 14 && m_cambio == true) { m_currentWP = 0; m_path = m_pathA; m_cambio = false; } if (m_neighbors.size() > 1) { for (int i = 0; i < m_neighbors.size(); i++) { if (m_enemyId != m_neighbors[i]->GetId()) { float l_nvx = m_neighbors[i]->GetSprite().getPosition().x - m_enemySprite.getPosition().x; float l_nvy = m_neighbors[i]->GetSprite().getPosition().y - m_enemySprite.getPosition().y; float distance = std::sqrt(l_nvx * l_nvx + l_nvy * l_nvy); if (distance < MINIMUM_NEIGHBOR_DISTANCE) { l_nvx *= -1; l_nvy *= -1; float l_vx = m_path[m_currentWP].x - m_enemySprite.getPosition().x; float l_vy = m_path[m_currentWP].y - m_enemySprite.getPosition().y; float l_resultanteX = l_nvx + l_vx; float l_resultanteY = l_nvy + l_vy; float l_waypointDistance = std::sqrt(l_resultanteX * l_resultanteX + l_resultanteY * l_resultanteY); if (l_waypointDistance < MINIMUM_WAYPOINT_DISTANCE) { if (m_currentWP == m_path.size() - 1) { std::cout << "\n"; std::cout << "[GAME OVER]" << std::endl; m_go = false; m_pathCompleted = true; } else { m_currentWP++; } } if (l_waypointDistance > MINIMUM_WAYPOINT_DISTANCE) { l_resultanteX = l_resultanteX / l_waypointDistance; l_resultanteY = l_resultanteY / l_waypointDistance; m_enemySprite.move(ENEMY_SPEED * l_resultanteX * dt, ENEMY_SPEED * l_resultanteY * dt); } } else { float vx = m_path[m_currentWP].x - m_enemySprite.getPosition().x; float vy = m_path[m_currentWP].y - m_enemySprite.getPosition().y; float len = std::sqrt(vx * vx + vy * vy); if (len < MINIMUM_WAYPOINT_DISTANCE) { if (m_currentWP == m_path.size() - 1) { std::cout << "\n"; std::cout << "[GAME OVER]" << std::endl; m_go = false; m_pathCompleted = true; } else { m_currentWP++; } } if (len > MINIMUM_WAYPOINT_DISTANCE) { vx = vx / len; vy = vy / len; m_enemySprite.move(ENEMY_SPEED * vx * dt, ENEMY_SPEED * vy * dt); } } } } } else { float vx = m_path[m_currentWP].x - m_enemySprite.getPosition().x; float vy = m_path[m_currentWP].y - m_enemySprite.getPosition().y; float len = std::sqrt(vx * vx + vy * vy); if (len < MINIMUM_WAYPOINT_DISTANCE) { if (m_currentWP == m_path.size() - 1) { std::cout << "\n"; std::cout << "[GAME OVER]" << std::endl; m_go = false; m_pathCompleted = true; } else { m_currentWP++; } } if (len > MINIMUM_WAYPOINT_DISTANCE) { vx = vx / len; vy = vy / len; m_enemySprite.move(ENEMY_SPEED * vx * dt, ENEMY_SPEED * vy * dt); } } }
¡¡Thank you very much in advance!!
• By SinnedB
Hello,
I am not sure if I phrased the title properly. What I am trying to achieve is the following:
Winning chances:
Red card: 10%
Blue card: 20%
Green card: 15%
Nothing card: 10%
Now a player has the chances above to win those cards but how would that look like in code?

• I'm stuck trying to make a simple ray sphere intersection test. I'm using this tutorial as my guide and taking code from there. As of now, I'm pretty sure I have the ray sorted out correctly. The way I'm testing my ray is by using the direction of the ray as the position of a cube, just to make sure it's in front of me.
cube.transform.position.x = Camera.main.ray.origin.x + Camera.main.ray.direction.x * 4; cube.transform.position.y = Camera.main.ray.origin.y + Camera.main.ray.direction.y * 4; cube.transform.position.z = Camera.main.ray.origin.z + Camera.main.ray.direction.z * 4;
So if I rotate the camera, the cube follows. So it's looking good.

The problem occurs with the actual intersection algorithm. Here are the steps I'm taking, I'll be very brief:
1) I subtract the sphere center with the ray origin:
L.x = entity.rigidbody.collider.center.x - ray.origin.x; L.y = entity.rigidbody.collider.center.y - ray.origin.y; L.z = entity.rigidbody.collider.center.z - ray.origin.z; L.normalize(); 2) I get the dot product of L and the ray direction:
const b = Mathf.dot(L, ray.direction); 3) And also the dot product  of L with itself (I'm not sure if I'm doing this step right):
const c = Mathf.dot(L, L); 4) So now I can check if B is less than 0, which means it's behind the object. That's working very nicely.
L.x = entity.rigidbody.collider.center.x - ray.origin.x; L.y = entity.rigidbody.collider.center.y - ray.origin.y; L.z = entity.rigidbody.collider.center.z - ray.origin.z; const b = Mathf.dot(L, ray.direction); const c = Mathf.dot(L, L); if (b < 0) return false;
Problem starts here
5) I now do this:
let d2 = (c * c) - (b * b); 6) ...and check if d2 > (entity.radius * entity.radius) and if it's greater: stop there by returning false. But it always passes, unless I don't normalize L and d2 ends up being a larger number and then it return false:
const radius2 = entity.rigidbody.collider.radius * entity.rigidbody.collider.radius; if (d2 > radius2) return false; but again, since I'm normalizing, it NEVER stops in that step. Which worries me.
7) I then do this:
let t1c = Math.sqrt(radius2 - d2); ...but it always returns a number in the range of 0.98, 0.97, if I'm standing still. But if I strafe left and right, the number lowers. If I rotate the camera, it makes no difference. Only if I strafe.
So I'm clearly doing something wrong and stopped there. Hopefully I made sense
• By isu diss
I decided to implement light shafts using http://sirkan.iit.bme.hu/~szirmay/lightshaft_link.htm So far I've only managed to implement the shadow map. Can anyone help me to implement this in D3D11? (I mean steps, I can do the rest). I'm new to all these shadow maps and etc.

# Aiming turret (math question)

## Recommended Posts

I really can't figure out the angles needed to aim a certain turret rig I have. The skeleton looks like first image.

The red arrow is pointing to the joint that is used to aim (via skeletal controller) and the blue arrow is pointing to the joint that needs to be aimed ie this joints forward x vector needs to point to the target. When aimed the turret likes like the second image.

The red dot is the red joint being controlled and the blue dot is the blue joint being aimed. I just can't seem to calculate theta as a function of (x,y) the targets position.

So the first thing I tried is the math in third image.

The first line we get from the law of sines, the second line is just the definition of tangent, the third line is plugging the first equation into the second, and the fourth line is obtained from the general formula for a linear combination of sine and cosine (https://en.wikipedia.org/wiki/List_of_trigonometric_identities#Linear_combinations). The last line is theta as a function of (x,y) simplified which is my goal. But trying all this out in UE4 I get something that's obviously not correct (see last image). I have a feeling the math is wrong.

Math isn't really my wheelhouse and I'm surprised I even came up with anything even it appears wrong. Anyone able to tell me what stupid overcomplicated mistake I'm making in the math? I would really like an analytical solution and not just iteratively error correct it into aiming correctly. I would really appreciate a pair of eyes on this. Thanks!

Edited by instantmelanie
correction

##### Share on other sites

do you not have the value of h? You could just go arctan(y/(x+h)) = theta. Have you tried that already? I see what you did, nvm let me look further

do you know they bounds of x and y? You are using an arcsin to get to your last step and the domain of it may be causing you some issues. your sin^-1() in the last part of your equation will not work for values outside of the bounds of x and y with -1<x<1 and -pi/2<y<pi/2

Edited by abominableCodeman

##### Share on other sites

You shouldn't need the angles, if you can set the joint's matrix.

I'll label your matricies from the base to the barrel as: A (base), B(joint), C(barrel base), D(barrel end)

Firstly, you have a target to find, relative to your joint:

target_dir = normalize(target_pos - B.pos())

Secondly, your barrel is offset from the joint:

offset = D * inv(B)

Thirdly, you need to apply that offset to get the correct position, which means rotating your turret into the same plane as the target and applying the inverse of the offset to the target position.

offset_pos = target_pos * look_at(target_dir) * inv(offset)

Lastly, you have your corrected target position, so you can look at it relative to world space, and multiply by your turret's bind pose to get B in the correct spot.

B = look_at(normalize(offset_pos - joint_pos)) * joint_bind_matrix

Where "look_at" if your library doesn't define it is basically building a matrix from a guess of the "up" direction combined with your desired "at" direction:

look_at(dir) {
left = cross(up_guess, dir);
up = cross(dir, left);
return [at, up, left, zero_vec];
}

However, if you do actually need the angle, you can extract them from that final matrix's at vector.

Edited by KulSeran

##### Share on other sites

Having gone through what you are doing now, around turret aiming, you have identified much of the problem.  But you probably need to simplify.  There are a number of spaces you need to consider.  When I did this, you need to keep local and world space in mind.  You calculate your target vector in the world space, but then apply your rotations in local space (hope I can articulate this well enough, im not that good at explaining it!)

A) you obviously have a target vector, I like yourself want to calculate that from barrel of the gun.  This as you know is executing the matrix mutliplications from base to barrel, that is calculating barrels position.

Base/Origin matrix -> First attachment.

First attachment to second attachment

Second attachment to barrel.

You have the location and you then can calculate from that the direction vector to the target point.  That's the first bit done.

Now, im going to assume that your first attach point rotates in say the X/Z plane.  Therefore you need to map our target vector on to that plane.   That allows you to set the direction of the connection point on that plane by getting the angle between the reference direction of the model and direction on the X/Z plane.  This leaves only the Y component for your 2nd connection point, but you need to base the elevation off the distance in the XZ plane.

The key points, because you will probably be a little confused here.  I base all my angles to rotate based off the reference direction of the model in relation to the vector in a specific plane, ala YAW for XZ and Pitch in XY.

I have working example this in my game (https://www.facebook.com/InsaneSoftware.com.au/).  The Gun turrets follow you on both static and moving models.  My code model is a little more complicated as I assign rotation axis on each connection point and it takes account of say moving ships with moving rotating turrets with accurate barrel ejection points.  But you get the idea, if you need some help just PM me.  I will try and elaborate offline.   My models also consider optimal rotation direction, blind spot (gun arc limitations) etc.

Edit: I assume your barrel ejection point as a direction Vector, This is key to how I solved my rotation problems.

Edited by ErnieDingo