- Home /
How can i decrease angle every in simple pendulum in my project?
Hi All,
please take a look of this question. http://answers.unity3d.com/questions/422829/how-can-a-decrease-angle-every-time-in-simple-pend.html
and answer me . I posted that question before and also i tried so many times but that is not done at. that is the final task for me then only my project is completed. Please help me.
Thanks To all.
Answer by Owen-Reynolds · Mar 25, 2013 at 02:53 PM
That's a big hunk of code, without a lot of explaining. Does it swing back and forth in a half circle, and you want to decrease that? Or do you want to change what clicking the mouse does (in that OnMouse* area)?
If you read it some, there are a lot of lines like if(x>90)
, if(z<270)
and such. Those are probably checking if you went past some angle. Read more what they do (what part of the code says "now start moving the other way"?) Then maybe just change some numbers and see what happens.
And, very common in games, sometimes what you wanted to do isn't fun, looks bad, has unsolvable problems. So you just drop it, but you learned and maybe it gave you an idea for something else.
Hi Owen Reynolds, Thanks for answering my question. In this project is not having lot of code.just one code for On GUI and one code for pendulum control. In this code my main problem is first i drag some angle so that angle is updated to updated method. v3T.Set (0.0f, 0.0f, $$anonymous$$athf.Lerp(startAngle, endAngle, f)); pivot.eulerAngles = v3T; fTimer += Time.deltaTime;
that is in updated method. please take a look. And give me correct answer. please help me. I have struck that problem from last week.
Thanks
Answer by robertbu · Mar 26, 2013 at 05:07 AM
I'm assuming you want the pendulum to run down due to friction? I can think of two ways of handling this issue. If you really want to only adjust the angles once per cycle, then detect when 'f' transitions across .5. That is keep a copy of f from the previous fame (or a boolean value) , and when f was less than 0.5 in the previous frame and is now greater than or equal to .5, you can decrease the angle. If you want to decrease it going both directions, then add a second check for when f is greater than or equal to and transitions to less than .5.
A second method would be to go ahead and reduce the angle every frame, but by an amount that is scaled so that the reduction of the angle across a full cycle is the same as the single reduction you would make in the method above.
Note that angle1 and angle2 are not normalized. That is, angle1 may start in any quadrant, and angle2 reflected. Without more study, I cannot be sure, but I think you want to reduce the angle towards the average between angle1 and angle2.
Thanks for answering $$anonymous$$r Robertbu. I tired above two methods. 1)I wrote code like this
void Update() {
if (!dragging) {
if(anglespeed>0 && anglespeed<180)
{
length1 = ((anglespeed*2*($$anonymous$$athf.PI)*GUI$$anonymous$$enu.StripeValue)/180);
speed=length1/Timeperiod;
}
else if(anglespeed>180 && anglespeed<=270)
{
angles1 = anglespeed-180;
angles2 = 180-angles1;
length1 = ((angles2*2*($$anonymous$$athf.PI)*GUI$$anonymous$$enu.StripeValue)/180);
speed=length1/Timeperiod;
}
else if(anglespeed>-90 && anglespeed<0)
{
angles1 = anglespeed+90;
length1 = ((angles1*2*($$anonymous$$athf.PI)*GUI$$anonymous$$enu.StripeValue)/180);
speed=length1/Timeperiod;
}
float f = ($$anonymous$$athf.Sin (fTimer * speed - $$anonymous$$athf.PI / 2.0f) + 1.0f) / 2.0f ;
v3T.Set (0.0f, 0.0f, $$anonymous$$athf.Lerp(startAngle, endAngle, f));
pivot.eulerAngles = v3T;
fTimer += Time.deltaTime;
Timeperiod=(2*($$anonymous$$athf.PI)*($$anonymous$$athf.Sqrt(GUI$$anonymous$$enu.StripeValue/9.81f))) ;
Debug.Log(startAngle);
if(f> 0.5f)
{
{
startAngle +=1;
endAngle -=1;
anglespeed-=2;
}
}
}
}
But that is not working properly. It is not taking complete cycle. when ever the f >0.5 than it decreases the angle but not going for complete cycle. If possible please modify my update method code.
2) I will now how many frames for a cycle. ? really second one i didn't get you.
Thankyou
You are looking for the transition across the .5. So you have to save and use the value of f from the previous frame. Something like:
if (f >= 0.5 && fPrev < 0.5) {
// decrease angles
}
fPrev = f;
$$anonymous$$r Robertbu ur really great. Thanks a lot for helping me. $$anonymous$$y project trail version max completed.
can i get a suggestion how will working on force concept in simple pendulum. i mean by using force i want to get p.e and k.e. can i get any idea. than i will implement.
General design questions are really for the forums. When you actually get to the point of implementing your ke/pe, you can post a new quesiton here. If I see it go by I'll be sure to take a look. Note that the code above is based on an answer I wrote awhile back. That answer was never intended to be an accurate physics simulation of a pendulum. I just knew that pendulums were sinosodial, and wrote some code that made the pendulum look right. Since then (given all the questions about pendulums), I've gone back and looked at the equations. I don't know how to what degree you've made changes to mimic the real physics.
Here is one link that writes about PE/$$anonymous$$E of pendulums: