Hi All,
I am developing sample simple pendulum experiment. i done that with the help of Mr.Robertbu.
My pendulum code is like this:
using UnityEngine;
using System.Collections;
public class Pendulum2 : MonoBehaviour {
public Transform pivot;
public static float speed=0.0f ;
private Vector3 v3Pivot; // Pivot in screen space
private bool dragging = false;
private float startAngle = 0.0f;
private float endAngle = -180.0f;
private float fTimer = 0.0f;
private float angleDiff;
private Vector3 v3T = Vector3.zero;
private float angle;
private float anglespeed,angles1,angles2;
public GUIStyle fontstyle;
private float length1,length2,length3;
private float Timeperiod=0.0f;
//var fontSize : int = 20;
private int count=0;
private float fPrev;
void Start () {
v3Pivot = Camera.main.WorldToScreenPoint (pivot.position);
}
void Update() {
if( !dragging ) {
if (anglespeed>0 && anglespeed<180)
{
length1 = ((anglespeed*2*(Mathf.PI)*GUIMenu.StripeValue)/180);
speed=length1/Timeperiod;
}
else if(anglespeed>180 && anglespeed<=270)
{
angles1 = anglespeed-180;
angles2 = 180-angles1;
length1 = ((angles2*2*(Mathf.PI)*GUIMenu.StripeValue)/180);
speed=length1/Timeperiod;
}
else if(anglespeed>-90 && anglespeed<0)
{
angles1 = anglespeed+90;
length1 = ((angles1*2*(Mathf.PI)*GUIMenu.StripeValue)/180);
speed=length1/Timeperiod;
}
float f = (Mathf.Sin (fTimer * speed - Mathf.PI / 2.0f) + 1.0f) / 2.0f ;
v3T.Set (0.0f, 0.0f, Mathf.Lerp(startAngle, endAngle, f));
pivot.eulerAngles = v3T;
fTimer += Time.deltaTime;
Timeperiod=(2*(Mathf.PI)*(Mathf.Sqrt(GUIMenu.StripeValue/9.81f))) ;
Debug.Log(startAngle+","+endAngle);
if (f >= 0.5 && fPrev < 0.5) {
if(startAngle>endAngle){
startAngle -=1;
endAngle +=1;
count +=1;
}
else {
startAngle = 270;
endAngle = 270;
GUIMenu.abo = true;
}
}
fPrev = f;
}
}
void OnMouseDown() {
dragging = true;
Vector3 v3T = (Vector3)Input.mousePosition - v3Pivot;
angle = Mathf.Atan2 (v3T.y, v3T.x) * Mathf.Rad2Deg;
}
void OnMouseDrag() {
Vector3 v3T = (Vector3)Input.mousePosition - v3Pivot;
float angleT = Mathf.Atan2 (v3T.y, v3T.x) * Mathf.Rad2Deg;
angleDiff = Mathf.DeltaAngle(angle, angleT);
pivot.Rotate(new Vector3(0.0f, 0.0f, angleDiff));
angle = angleT;
}
void OnMouseUp() {
Vector3 v3T = Camera.main.WorldToScreenPoint(transform.position);
v3T = v3T - v3Pivot;
float angle = Mathf.Atan2 (v3T.y, v3T.x) * Mathf.Rad2Deg;
Debug.Log (angle);
anglespeed=angle+90;
if (angle < 0.0f)
angle += 360.0f;
count = 0;
startAngle = angle;
if (angle <= 90.0f) {
endAngle = angle - 2.0f * angle - 180.0f;
}
else if (angle <= 180.0f) {
endAngle = angle + 2.0f * (180.0f - angle) + 180.0f;
}
else if (angle <= 270.0f) {
endAngle = angle + 2.0f * (270.0f - angle);
}
else {
endAngle = angle - 2.0f * (angle - 270.0f);
}
if(startAngle < endAngle) {
float temp = startAngle;
startAngle = endAngle;
endAngle = temp;
}
dragging = false;
fTimer = 0.0f;
}
void OnGUI()
{
GUI.Label (new Rect (Screen.width-(Screen.width/3.5f), 20, 200, 130), " Timeperiod :"+Timeperiod,fontstyle);
GUI.Label (new Rect (Screen.width-(Screen.width/5), 50, 200, 130), " Count :"+count,fontstyle);} }
And My GUi Code is like this:
using UnityEngine;
using System.Collections;
public class GUIMenu : MonoBehaviour {
public static float StripeValue,bottomValue ;
public GameObject Stripe,Bottom;
public int x ,y;
public Transform pivot;
public bool realBool = false;
public bool OneByFourBool = false;
public bool OneBySixteenBool = false;
public GUIStyle fontstyle,pauseStyle,resumeStyle,newfontStyle;
public Rect windowRect,windowRect1,windowRect3;
public static bool temp=false;
public bool aboutFlag;
public static bool abo = false;
void Start()
{
x=20;
y=60;
StripeValue=20f;
bottomValue=15;
windowRect = new Rect(20, 10, 280, 600);
windowRect1 = new Rect(300, 20, 400, 200);
windowRect3 = new Rect(Screen.width-(Screen.width/3),Screen.height-300, 400, 200);
}
void OnGUI()
{
if(!temp){
if(GUI.Button(new Rect(700,50,60,60),"",pauseStyle))
{ Time.timeScale=0;
Pendu.stopTimer=true;
temp=true;
} }
if(temp)
{
if(GUI.Button(new Rect(700,50,60,60),"",resumeStyle))
{ temp=false;
Pendu.stopTimer=false;
Time.timeScale=1;
}}
Stripe. transform.localScale = new Vector3(1,StripeValue/33,1);
Bottom. transform.localScale = new Vector3(bottomValue*1.1f,bottomValue/24,bottomValue*1.1f);
windowRect = GUI.Window(0, windowRect, DoMyWindow, "");
if(aboutFlag)
{
windowRect1 = GUI.Window(1, windowRect1, DoMyWindow1, "");
}
if(abo)
{
windowRect3 = GUI.Window(2, windowRect3, DoMyWindow3, "");
}
if(GUI.Button(new Rect(690,523,150,70),"Pendulum Formulas"))
{
aboutFlag=true;
}
}
void DoMyWindow1(int windowID)
{
GUI.Label(new Rect(60,40,330,40),
" T=2PI(Sqrt(Length/Gravity))",fontstyle);
if(GUI.Button(new Rect(160,150,80,30),"OK"))
{
aboutFlag=false;
}
}
void DoMyWindow3(int windowID)
{
GUI.Label(new Rect(0,0,400,100),
" S.no Angle Timeperiod Length ",fontstyle);
}
void DoMyWindow2(int windowID)
{
if(GUI.Button(new Rect(160,150,80,30),"Earth"))
{
}
if(GUI.Button(new Rect(160,150,80,30),"Moon"))
{
}
if(GUI.Button(new Rect(160,150,80,30),"Moon"))
{
}
}
void DoMyWindow(int windowID)
{
if(GUI.Button(new Rect(90,20,100,40),"Real Time"))
{
Pendulum2.speed=1.0f;
}
if(GUI.Button(new Rect(90,70,100,40),"1/4 Time"))
{
Pendulum2.speed=1.0f/4.0f;
}
if(GUI.Button(new Rect(90,120,100,40),"1/16 Time"))
{
Pendulum2.speed=1.0f/16.0f;
}
if(GUI.Button(new Rect(100,310,80,30),"Moon"))
{
Pendu.angle=360;
Pendu.start=0;
Pendu.stop=360;
Pendu.speed=0;
}
if(GUI.Button(new Rect(100,350,80,30),"Earth"))
{
Pendu.angle=130;
Pendu.start=50;
Pendu.stop=130;
Pendu.speed=50;
pivot.transform.localEulerAngles =new Vector3(0,0,270);
}
if(GUI.Button(new Rect(100,390,80,30),"Gravity"))
{
Pendu.angle=90;
Pendu.start=70;
Pendu.stop=110;
Pendu.speed=50;
pivot.transform.localEulerAngles =new Vector3(0,0,270);
}
GUI.Label(new Rect(50,175,100,25),"LENGTH",newfontStyle);
GUI.Label(new Rect(50,240,100,25),"MASS",newfontStyle);
StripeValue = GUI.HorizontalSlider ( new Rect(20, 205, 200, 30), StripeValue, 8f, 30.0f);
bottomValue = GUI.HorizontalSlider ( new Rect(20, 270, 200, 30), bottomValue, 8f, 30.0f);
}}
After completion of total oscilations the pendulum comes into 0 position. on that case i want to pass s.no,angle,time period and length values to that window shown in image. how will i do that. I want every time get that values and updated in to that window.
Please help me.
Thanksyou
Shankar