- Home /
create and display curve based on physics?
I have a cannon that shoots a ball using the physics engine. Mass, ball speed, and forward angle are all variables that are used. I assume gravity is a constant.
Basically I want to create a visible curve that starts at my shooting position and creates an arc similar to a sin wave until it hits and object. Any fairly simple way to do this?
I am fairly new to unity so please keep that in mind for examples or reference. thanks.
By the way: Projectiles follow a parabola, not a sine wave :)
From what I have found so far tracer rounds would be possible. Predictive curvature would need to do the math using the angle, mass, speed and gravity. Which should be possible to estimate a point in 3d space using these variables and plotting vectors3 positions within a time range. I don't have enough info (or enough attention span) to do this though. Parabola might be correct, but I wasn't trying to be technical in my comments and I was just trying to get the reader a visual and sine waves were the first thing to pop in my head.
The link I gave has all the math you need done for you. Taking mass into consideration is inadvisable since it only becomes a factor in drag equations and I myself do not know which of the many varieties of formulas Unity actually uses. You might be able to find out which one is used, but having experienced trying to reverse solve them myself I can tell you it's not really worth the effort.
Answer by redteardrop · Sep 26, 2012 at 01:43 PM
apparently wikipedia and trying to go back to my high school math helped. There also appears to be an issue with addforce. I had to make sure I used ForceMode.Impulse to get an accurate initial velocity. If anyone else wants something like this, check the links below for the wiki math and here is a general rundown.
http://en.wikipedia.org/wiki/Range_of_a_projectile
http://en.wikipedia.org/wiki/Trajectory_of_a_projectile
Things you will need to know to predict the trajectory of a launched object using a single forward axis and no air resistance or wind.
1. initial velocity of item (since f=m*a you can get the velocity by dividing the force applied (use ForceMode.Impulse) by the rigidbody mass)
2. the angle of launch (can be put in a variable in an update function)
3. gravity (should be a constant 9.81m/s^2 (just use 9.81)
Here comes the tricky part. You should be able to google the formuly (I can't find the link right now). What needs to be done is use a linerenderer using enough points and plotting each one based on the y height at a given x. It's a good idea to use a transform variable to point to the world coordinates of your starting position to call in code.
var maxDist;this will get you the max distance on a level surface and you can use that minus the initial position of the start point to get a range and a distance for each x of the linerenderer. This won't work for me since I am shooting from the top of a cliff so I just chose a set interval for each of my x positions.
var velocity;
maxDist = ((velocity * velocity) * ((Mathf.Sin((-angle * Mathf.Deg2Rad) *2))))/9.81;//convert degree angle to radian to use in Sin correctly
To find Y us this code (I'm using a for loop to get each linerenderer position):
rayY[n] = (0) + (rayX[n] * (Mathf.Tan(-angle * Mathf.Deg2Rad))) - ( (g * (rayX[n]*rayX[n])) /(((Mathf.Cos(-angle * Mathf.Deg2Rad) * velocity) * (Mathf.Cos(-angle * Mathf.Deg2Rad) * velocity)) *2));The (0) at the beginning is the initial position of y, mine is kept at 0 here since I have the script attached to a child object and the math is local space so it'll move around the world fine. It should even rotate left or right on the cannon and still work properly this way (haven't tried it) rayX[n] is the x position.
Anyway, hope this helps someone else too. I'll see if I can put it in the asset store at some point just to easily add to a project.
Answer by redteardrop · Aug 05, 2012 at 08:08 PM
ITs a start, but from what I can tell, if I do it this way it'll look like a whip when flicked. It'll show the position of the last position on previous projectiles when the cannon could be at a lower position creating a distorted effect. Any way around that or to be preemtive and only show what the curve would do based on the current position of that cannon angle.
yeah, I don't know why I didn't think of it, Angry birds is pretty much a great example of what I'd like to do.
Your answer
![](https://koobas.hobune.stream/wayback/20220613075224im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Producing a 2D Bounce Effect For a Simple Game 0 Answers
2D 360 degress platformer example needed 0 Answers
Punching a crate 0 Answers
Two Bone IK constraint with more than 2 bones on my arm. 0 Answers
HOW DO I USE RAYCASTHIT 2D???? 1 Answer