## unity – How to calculate a direction that will turn a parent so that a locally-rotated child is faced in a given direction?

Imagine an old sailing ship with cannons: they’re pointed out at 90° angles from the ship’s hull, so in order to fire them, you have to turn the whole ship so that the cannons are pointed at the target. I’m hoping to figure how to calculate “where to point the ship so that the cannons are aimed correctly,” but for any possible cannon-angle (not just 90°) in 3D space.

I’m an experienced dev but a total 3D newb, so this vector stuff is wrinkling my brain. This is what I have right now, calculated from the child’s (cannon’s) gameObject:

`Vector3 directionToAim = (transform.localRotation * (transform.position - target.position)).normalized;`

It’s working for side-facing weapons (like the sailing ship), but for forward-facing weapons it turns the parent in the opposite direction, directly away from the target. If I invert it to (target.position – transform.position) the forward-facing weapons work fine, but the side-facing weapons face the opposite way.

Also, my vague understanding is that it’s better to use Quaternions for this, rather than Vector3, is that right? Can someone help me grasp how these parent/child rotations can be calculated?

Posted on

## google bigquery – Calculate the total on events with two time conditions

I have a table in BigQuery that looks something like this:

``````    schema = (
bigquery.SchemaField('timestamp', 'TIMESTAMP', mode='REQUIRED', description='Data point timestamp'),
bigquery.SchemaField('event_id', 'STRING', description='EventID'),
(...)
)
``````

The table has a fairly large dataset, and I’m trying to find write an efficient query that returns the number of events that happened in the last 24 hours but also within the last N days. That is, two different records with different conditions but the same `event_id`. I don’t care so much about the actual `event_id`, but rather the distribution.

Ideally, the query would return something like this:

``````7_days: 20
30_days: 15
60_days: 7
``````

If it’s impossible to do this in pure SQL, I also have Pandas available at my disposal.

Posted on

## java – How to calculate FOV angle range based on facing and mouse aim position

The title is not that clear so I’ll explain. In my top down shooter game the player can be faced in any of the 8 directions using the keyboard. Then depending on the direction he will have a 90 degree field of view to turn based on the angle selected by mouse position. Obviously the mouse can go anywhere so it has to be restricted within -45 and 45 of the faced angle.

The eight direction angles:

``````    135 90 45
180             0
225 270 305
``````

So if he’s facing to the left at 180 his range will be -45 and 45 of that ie 135 to 225. So simply it’s `mouseAngle - facingAngle` which works fine except if he’s facing at around 0, 45 or 305 degrees where the mouse angle passes zero causing issues in calculation.

As it is I’m using this formula.

``````private void setAngleFOV() {
if (facing == 0 || facing == 360)
facing = mouseAngle < 360 && mouseAngle >= 180 ? 360 : 0;
angleFOV = mouseAngle - facing;
angleFOV = Math.max(Math.min(angleFOV, 45), -45);
}
``````

It can either be 0 or 360 for either `facing` or `mouseAngle`, whichever works best but I haven’t found an ideal solution. The above formula works for facing right at angle 0, but not with diagonal angles 45 and 305.

It could maybe work with lots of `if` statements and such, but I feel there must be a better way?

Posted on

## dnd 5e – How to calculate movement within an area

Related: How does the UA Tunnel Fighter fighting style’s reaction attack interact with the Sentinel feat’s speed-reduction effect?

The Tunnel Fighter feat from UA has the following wording:

As a bonus action, you can enter a defensive stance that lasts until the start of your next turn. While in your defensive stance, you can make opportunity attacks without using your reaction, and you can use your reaction to make a melee attack against a creature that moves more than 5 feet while within your reach.

What exactly is meant by moving “more” than 5 feet?

Would either of the two following paths be considered moving “more than 5 feet”, assuming the player was holding a weapon with reach?

• Path 1: Enter range, move 5 feet, exit range
• Path 2: Enter range, move another 5 feet within range
• Path 3: Enter range, immediately exit range
``````┌───┬───┬───┰───┬───┐
│MAX-RANGE->┃   │   │
├───┼───┼───╂───┼───┤
│   │   │ .------P_1│
├───┼───┼─|─╂───┼───┤
│ME │   │ `-->F │   │
├───┼───┼───╂───┼───┤
│   │ F <--------P_2│
├───┼───┼───╂───┼───┤
│   │   │ .------P_3│
┝━━━┿━━━┿━|━╃───┼───┤
│   │   │ F │   │   │
└───┴───┴───┴───┴───┘
``````

PURELY looking at the diagram and adding up the lengths of the lines, you could calculate the sum of lines WITHIN my range as being:

• Path 1: `2.5 + 5 + 2.5 = 10`
• Path 2: `2.5 + 5 = 7.5`
• Path 3: `2.5 + 2.5 = 5`

But I don’t know if this is strictly correct. I suspect that partial values may only be calculated before, or after, movement.

If movement is calculated before the step is taken, then stepping into my reach won’t count as movement within my reach, but stepping out will count as 5 feet. This makes Path 2 safe to travel.

If movement is calculated after the step is taken, then stepping into my reach will count as moving 5 feet within my reach, but stepping out won’t count as any. This makes Path 2 dangerous.

If movement is only calculated when it is entirely (start+finish) within reach, then all paths are safe.

There’s a lot of different ways to spin this. Is there any official ruling on how to calculate movement within an area?

## code quality – How to calculate the similarity of files with noise?

There are three files of different lengths that need to be compared for similarity.
It is supposed to use the cosine distance formula.
But these files have a lot in common, which makes the comparison noisy.
If I throw out the general part, then how do I compare these files for similarity?

The content of the files is the name of the object and its suspicious value:

``````  file1              file2              file3

name1 1.0          name1 1.0          name2 0.9
name2 1.0          name2 1.0          name4 0.7
name3 0.8          name3 0.6          name5 0.2
name4 0.6          name5 0.2          name12 0.1
name5 0.5          name8 0.1             ...
...              name9 0.05
...
``````

How can I compare them?

Thank you for your attention to my question, have a nice day =]

Posted on

## probability or statistics – Calculate combined standard deviation

If I have a data that I fit with `NonlinearModelfit` that fits a data based on two fitting parameters, `c1` and `c2`.

When I used `nlm("ParameterTable") // Quiet` I get the following table: If I have an equation such as:

`eq = (2.303*((70 + 273.15)^2)*(c1/c2))/1000`

Is there any code (as opposed to doing it manually) I can use to calculate the value of `eq` with the combined standard deviation based on the standard deviations of `c1` and `c2` from the table?

Thank you!

Posted on

## algorithms – Augmenting AVL tree to calculate sum of subtree

Suggest a way to augment an AVL tree to support a $$O(log n)$$ implementation of the function
`calculateSum(key)`, which receives a key of a node and returns the sum of its subtree.

I implemented it this way:

``````sumSubtree(node):
if node != null:
return sumSubtree(node.left) + sumSubtree(node.right) + node.key
return 0

calculateSum(key):
node = Search(key) // assuming I have a search function
return sumSubtree(node)
``````

which solves it in $$O(log n)$$.

But I read it is possible to maintain the sum during insertion and deletion. And augment an AVL tree this way.

Which solution would be better? Mine, or the other method? Does it matter?

Posted on

## physics – How to calculate the probability current?

In physics, we often have to calculate the probability current of a function. Given a function $$Psi(x,t)$$, the probability current $$j(x,t)$$ is defined as $$frac{i hbar}{2m}left(Psifrac{partialPsi^*}{partial x}-Psi^*frac{partialPsi}{partial x}right)$$ How could I calculate $$j(x,t)$$ using Mathematica?

Posted on

## list manipulation – Calculate Radius of Curvature

I have a list of coordinates that make a near-parabolic graph, which I managed to fit and calculate the curvature. However, the curvature only outputs a equation. Is there any way to calculate the radius of the curvature so it gives a numeric value? Any help is appreciated.

My code is as follows:

``````list = {{54.5, 120.5}, {65.25, 143.15}, {65.61, 143.02}, {76.18,
157.28}, {89.89, 176.76}, {98.08, 184.87}, {98.33,
184.44}, {118.5, 197.5}, {140.77, 205.74}, {150.96,
207.06}, {163.11, 206.07}, {169.09, 204.77}, {177.07,
202.8}, {181.14, 201.13}, {182.41, 200.47}, {194.65,
192.49}, {201.47, 188.95}, {209.02, 182.56}, {209.21,
182.86}, {222.11, 172.77}, {230.93, 161.53}, {230.96,
161.51}, {245.51, 141.75}, {257.03, 124.18}, {257.23,
124.53}, {261.71, 114.96}, {261.72, 114.91}, {267.16,
104.41}, {267.29, 104.07}, {280.45, 72.62}, {280.63,
72.34}, {283.35, 63.57}, {286.5, 48.5}};

fit = NonlinearModelFit(list, a*(x + c)^2 + b, {a, b, c}, x);
curvature = Simplify(ArcCurvature({t, fit(t)}, t))
``````

The fitted equation along with the list of coordinates graphed out: Posted on

## physics – How to calculate tangents in beam data to fit given trajectory

I’m currently trying to create a trajectory effect (in UE 4.25.1) similar to one from the last of us: I have created a beam with niagara, which lets me determine the starting point of the beam, endpoint and the tangents for both of them: I am happy with the look of it, but I don’t understand the math behind the tangent values. My goal is to make the line overlap the actual trajectory of the projectile (arrow), which is calculated in a BluePrint.

Most examples I found online either just use the Debug-Line from the `Predict Projectile Path by Trace Channel`, which looks rather bad or they spawn some kind of mesh every x interval on the trajectory line, which is not the kind of indicator I want to create.

Right now, the arrow is being shot from a starting point (bow) with a given velocity and a direction (camera angle). Unreal then calculates the drop of the arrow according to gravity and velocity.

So, the starting position of the beam would be the same as the starting position of the arrow and the angle/rotation would be equal, as well. I can also determine the endpoint for the beam (endposition of the arrow).

However I don’t understand how I could calculate the required tangents for the beam to represent the trajectory of the arrow (dropping)? I am new to UE and math is not my strongest point to say the least. Is this even possible with the way I set things up?

Posted on