last day (15 days later) » 

18:40
0
Q: Reduce dependecy of physics formula to one variable

TechTreeDevI'm still stuck with the artillery script in my project. In the scene, I can toggle to type of the cannon from ordinary cannon to howitzer and mortars and back by changing the string of the type. For each type there's a distinct script that's called in the update on fire button press (just for ...

It's not clear to me what this code is meant to do. Are you trying to find the angle at which to fire to hit a particular target? There are existing answers that may be useful to you for this purpose.
@DMGregory that looks great but I have tried this also but had problems with that. (Can you tell me how the curves are drawn in Unity)
Also, I need to implement a max range and I have no idea how to do this with your formula.
It sounds like you may want to edit this question to narrow-in on what you still need answered, or possibly create a new post if you have a second question. Quick replies: arcs in my gif are drawn with Unity's Gizmos, suited for visual debugging; max range is implicit in the formula via the speed parameter (beyond a certain range, projectile can't reach the target before falling), but you can easily add a distance check and early-out if you want additional constraints.
@DMGregory edited my question using your formula. Works halfways except for the described bug
You might want to use ForceMode.VelocityChange when supplying a target velocity in m/s rather than a force in newtons.
18:40
@DMGregory Hm, sorry, I can't follow. Would you mind to point out which specific part of the code should be changed as I'm not familar with these methods
In the last line: AddForce(vel, ForceMode.VelocityChange)
Without this, the velocity is interpreted as a force to apply (in newtons) and generally this will be 50x too weak for a unit mass.
thanks did not knoew of the usage as parameter, now the projectile flies way faster but still straight 90 degree upwards
Exactly 90 degrees up will only occur if deltaX is 0. What's your current deltaX?
dX: -0.6012058 dY: 0.27
should I use abs()?
18:51
okay
float deltaX = (targetPos.x - exitPos.x);
is the code for the calculation
Using the numbers you've given, I get an output velocity of (-0.03521, 83.69442), does this agree with your output?
Vel: (0.0, 195.4, 0.0)
hm
Whoops, had an error in the spreadsheet I was using. Should be (-0.29873, 9.99554)
Found what might be the cause - some missing parentheses.
hm, where?
can not find it
The (g*g) at the end of the T_Max formula.
We want to multiply by two over g squared. ;)
The operator precedence without the parentheses will instead multiply by two, and the gs cancel out.
19:08
Vel: (-0.1, 20.0, 0.0)
That's roughly double what the math is giving me. What are your T_Max and T_Min debugs giving you?
Min: 0.03306193 Max: 4.067976
whats your t_max formula?
float T_max = Mathf.Sqrt((b + discRoot) * 2 / (g * g));
Vel: (-0.1, 20.0, 0.0)
matches 100% the used formula
Bizarre. What are your b and discroot values?
19:19
B: 397.354 discroot: 397.3015
This is still for dX=-0.6012 dY=0.27 vMax=10 and g=9.8?
b = vMax * vMax - g * dY, so for dY >= 0 we'd expect b to be less than vMax squared (ie. 100). Getting nearly 400 there is strange.
restarted the whole thing, now i get different values
dX: -0.6012058 dY: 0.27
B: 97.354 discroot: 97.13953
Min: 0.06683129 Max: 2.012526
Vel: (-0.3, 10.0, 0.0)
That now looks correct up to rounding.
still upwards but slower
Keep in mind that using T_Max will tend to give taller trajectories. If you want a more sideways trajectory you can use T_Min, or any value in-between the two extremes.
19:28
using t min causes the projectile going left
and if I use t max it should theoretically still land on top of the enemy
It should indeed. We can confirm this using the equations of motion under constant gravity:
Px(t) = p0x + vx*t
Py(t) = p0y + vy*t - g * t*t/2
Plug in your velocity and you can confirm that these arcs pass through the given point (dx, dy) at T_max (or whatever time value you choose to use)
If your scene gravity is set to something other than (0, -9.8, 0) then you could get different results. Have you double-checked this?
sorry unity keeps crashing
and here's the scenario my maths teachers warned me
scene gravity can be altered where?
19:50
Edit -> Project Settings -> Physics
-9.81 seems good
what's p0 in your case?
That corresponds to exitPos
okay
besides this, is there another way to check this?
I run a quick for loop, incrementing a t value, drawing the arc using the equations of motion above and the Gizmos.DrawLine method. That way I can preview the expected arc.
dou you have occasionally the code for such a display? usually I don't work with Unity as I have a relly bad understanding of maths and physics
20:11
I don't have one handy, but I'll whip it up...
Sorry for annoying you, I feel really stupid at the moment.
20:27
would the velocity affect the hit position?
bc currently an increasing makes the projectile go further
With a higher max speed you can reach targets further away.
but if e.g the target ist 10 units away would 10 or 30 or 50 vMax result in a hit directly near to the ground on the given pistion
a bit afk trying to reach my friends in berlin as there was a suspected terrorist attack
If the point is in range then the formula will give you a trajectory that hits the point exactly. If it is out of range then it will not give you any trajectory at all.
Here's a quick implementation of the formula, along with a gizmo line and a test projectile to verify that it gives the correct result mathematically and in game physics: pastebin.com/KSzfufhs
But it will hit if its in range regardless of the speed?
thanks for the script, i'll try it after I'm done
"If the point is in range then the formula will give you a trajectory that hits the point exactly" yes.
21:09
Now the range is okay, but it still keeps shoting in the wrong direction. Gues I try it tomorrow
21:42
just fo interest, is there a need for the z value in the velocity

  last day (15 days later) »