Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
wooju217 committed Jul 8, 2023
1 parent 3fab733 commit 14b5bc2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
15 changes: 8 additions & 7 deletions Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -443,12 +443,13 @@ MonoBehaviour:
m_EditorClassIdentifier:
Mass1: 1
Velocity1: 1
Mass2: 100
Mass2: 10000
Velocity2: 1
WallPos: {x: -12, y: -5.45, z: 0}
Balls:
- {fileID: 1269859177}
- {fileID: 1309125083}
CollisionText: {fileID: 671530386}
--- !u!1 &422928167
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -876,7 +877,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!224 &864241683
RectTransform:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -916,7 +917,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!114 &926498702
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1179,7 +1180,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!224 &1062878606
RectTransform:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1535,8 +1536,8 @@ MonoBehaviour:
m_EditorClassIdentifier:
Velocity: 0
Mass: 1
Dir: -1
IsBigMass: 0
Count: 0
OtherBall: {fileID: 1309125083}
CollisionDistance: 1.25
CheckCollision: 1
Expand Down Expand Up @@ -1833,10 +1834,10 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: bac9983deb4da304297380ab5633f61e, type: 3}
m_Name:
m_EditorClassIdentifier:
Velocity: 100
Velocity: -1
Mass: 100
Dir: -1
IsBigMass: 1
Count: 0
OtherBall: {fileID: 1269859177}
CollisionDistance: 1
CheckCollision: 0
Expand Down
48 changes: 34 additions & 14 deletions Assets/Scripts/Ball.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ public class Ball : MonoBehaviour
{
public float Velocity = 1;
public float Mass = 1;
public int Dir = -1;
public bool IsBigMass;

public int Count;

public Ball OtherBall;

public float CollisionDistance;
Expand All @@ -23,36 +24,52 @@ public void Init(float mass, float velocity)
{
Mass = mass;
Velocity = velocity;
Dir = -1;

}

private void Update()
float lastTime;

private void FixedUpdate()
{
if(Time.timeScale == 0)
return;

transform.position = new Vector3(Velocity/Mass * Dir * Time.deltaTime, 0, 0) + transform.position;
lastPos = transform.position.x;
transform.position += new Vector3(Velocity * Time.deltaTime, 0, 0);



float d = Vector3.Distance(transform.position, OtherBall.transform.position);
float d_wall = Vector3.Distance(transform.position, GameManager.Instance.WallPos);
if(CheckCollision && !hit)
{
bool flag = false;
if(d <= CollisionDistance)
if(d < CollisionDistance)
{
Dir *= -1;
Velocity = GetVelocity(Mass, OtherBall.Mass, OtherBall.Velocity);
Debug.Log(Mass + "/" + OtherBall.Mass +"/" + OtherBall.Velocity);
//OtherBall.Velocity = GetVelocity(OtherBall.Mass, Mass, OtherBall.Velocity);
Count++;

Velocity *= -1;
float va0 = Velocity * -1;
Velocity = ((OtherBall.Mass * (va0 - 2*OtherBall.Velocity)-Mass*va0)/(-(Mass + OtherBall.Mass)));
//Velocity = GetVelocity(OtherBall);
//Debug.Log(Mass + "/" + OtherBall.Mass +"/" + OtherBall.Velocity);
OtherBall.Velocity = (Velocity + va0 - OtherBall.Velocity); //(va0 - OtherBall.Velocity + Velocity);
flag = true;
print(OtherBall.Velocity);
//print(Count + $" | 작은거 속도: {Velocity}, 큰거 속도: {OtherBall.Velocity} | 시간차 {time}");


//Time.timeScale = 0 ;
}
else if(d_wall <= 0.5f+0.1f/2f)
if(d_wall <= 0.5f+0.1f)
{
Dir *= -1;
Count++;
Velocity *= -1;
flag = true;
transform.position = new Vector3(-12+0.75f, -5.45f, 0);
}

GameManager.Instance.UpdateText(Count);

hit = flag;
}
else
Expand All @@ -63,8 +80,11 @@ private void Update()



public float GetVelocity(float thisMass, float otherMass, float otherVelocity)
public float GetVelocity(Ball otherBall)
{
return (GameManager.PEnergy - (otherMass * otherVelocity))/thisMass;
return (otherBall.Mass * (Velocity - 2 * otherBall.Velocity) - Mass*Velocity)
/-(Mass + otherBall.Mass);
}


}
9 changes: 9 additions & 0 deletions Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
Expand All @@ -15,11 +16,14 @@ public class GameManager : MonoBehaviour
public Vector3 WallPos;
public Ball[] Balls;

public Text CollisionText;

public static float PEnergy;

private void Awake()
{
Instance = this;

}

void Start()
Expand All @@ -32,4 +36,9 @@ void Start()
//에너지 보존 법칙
PEnergy = Mass1 * Velocity1 + Mass2 * Velocity2;
}

public void UpdateText(int count)
{
CollisionText.text = $"충돌횟수: {count}";
}
}

0 comments on commit 14b5bc2

Please sign in to comment.