From edd04b6776137e62e6ad56889bbe012cf774e590 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 5 Oct 2019 10:55:53 +0200 Subject: [PATCH] Add gravity forces --- Assets/Components/GameController.cs | 8 + Assets/Components/GameController.cs.meta | 11 + Assets/Components/Gravitation.cs | 67 ++++ Assets/Components/Gravitation.cs.meta | 11 + Assets/Prefabs/GameController.prefab | 13 + Assets/Prefabs/Gravitation Object.prefab | 136 +++++++ Assets/Prefabs/Gravitation Object.prefab.meta | 7 + Assets/Scenes/Game.unity | 144 ++++++++ Assets/Sprites/Circle.png | Bin 0 -> 78 bytes Assets/Sprites/Circle.png.meta | 347 ++++++++++++++++++ 10 files changed, 744 insertions(+) create mode 100644 Assets/Components/GameController.cs create mode 100644 Assets/Components/GameController.cs.meta create mode 100644 Assets/Components/Gravitation.cs create mode 100644 Assets/Components/Gravitation.cs.meta create mode 100644 Assets/Prefabs/Gravitation Object.prefab create mode 100644 Assets/Prefabs/Gravitation Object.prefab.meta create mode 100644 Assets/Sprites/Circle.png create mode 100644 Assets/Sprites/Circle.png.meta diff --git a/Assets/Components/GameController.cs b/Assets/Components/GameController.cs new file mode 100644 index 0000000..968fc8f --- /dev/null +++ b/Assets/Components/GameController.cs @@ -0,0 +1,8 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class GameController : MonoBehaviour +{ + public float gravitationConstant = 1; +} diff --git a/Assets/Components/GameController.cs.meta b/Assets/Components/GameController.cs.meta new file mode 100644 index 0000000..a8a5dc2 --- /dev/null +++ b/Assets/Components/GameController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1d1d0978f263bec41af567152b121a2e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Components/Gravitation.cs b/Assets/Components/Gravitation.cs new file mode 100644 index 0000000..2fc98e7 --- /dev/null +++ b/Assets/Components/Gravitation.cs @@ -0,0 +1,67 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Gravitation : MonoBehaviour +{ + + public static List simulatedObjects = null; + + protected new Rigidbody2D rigidbody; + + protected GameController gameController; + + public int positionInList; + + protected Vector3 appliedForce; + + protected float forcesIntensity; + + protected float mass { + get { + return rigidbody.mass; + } + + set { + rigidbody.mass = value; + } + } + + private void Awake() { + if(simulatedObjects == null) { + simulatedObjects = new List(); + } + positionInList = simulatedObjects.Count; + simulatedObjects.Add(this); + } + + private void Start() + { + rigidbody = GetComponent(); + gameController = GameObject.Find("GameController").GetComponent(); + appliedForce = new Vector3(0f, 0f, 0f); + forcesIntensity = 0f; + } + + // O(n²) + private void FixedUpdate() + { + foreach(Gravitation other in simulatedObjects) { + if(other.positionInList <= this.positionInList) continue; + float distance = Vector3.Distance(this.transform.position, other.transform.position); + float force = gameController.gravitationConstant * this.mass * other.mass / Mathf.Pow(distance, 2); + Vector3 direction = (other.transform.position - this.transform.position).normalized; + this.appliedForce += direction * force; + this.forcesIntensity += force; + other.appliedForce -= direction * force; + other.forcesIntensity += force; + } + rigidbody.AddForce(appliedForce); + if(forcesIntensity >= 5f) { + GetComponent().color = Color.yellow; + } + + appliedForce = new Vector3(0, 0, 0); + forcesIntensity = 0f; + } +} diff --git a/Assets/Components/Gravitation.cs.meta b/Assets/Components/Gravitation.cs.meta new file mode 100644 index 0000000..ea81e82 --- /dev/null +++ b/Assets/Components/Gravitation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d16f17bda197c324f8b088f106c66c5c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Prefabs/GameController.prefab b/Assets/Prefabs/GameController.prefab index d749fa3..6cb56f5 100644 --- a/Assets/Prefabs/GameController.prefab +++ b/Assets/Prefabs/GameController.prefab @@ -9,6 +9,7 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 9012813807232547518} + - component: {fileID: 4937878819460165300} m_Layer: 0 m_Name: GameController m_TagString: GameController @@ -30,3 +31,15 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &4937878819460165300 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4669070901862137127} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1d1d0978f263bec41af567152b121a2e, type: 3} + m_Name: + m_EditorClassIdentifier: diff --git a/Assets/Prefabs/Gravitation Object.prefab b/Assets/Prefabs/Gravitation Object.prefab new file mode 100644 index 0000000..e182042 --- /dev/null +++ b/Assets/Prefabs/Gravitation Object.prefab @@ -0,0 +1,136 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &5473375028011702754 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 5473375028011702783} + - component: {fileID: 5473375028011702782} + - component: {fileID: 5473375028011702753} + - component: {fileID: 5473375028011702752} + - component: {fileID: 5473375028011702755} + m_Layer: 0 + m_Name: Gravitation Object + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &5473375028011702783 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5473375028011702754} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: -2, y: 0.88, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!50 &5473375028011702782 +Rigidbody2D: + serializedVersion: 4 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5473375028011702754} + m_BodyType: 0 + m_Simulated: 1 + m_UseFullKinematicContacts: 0 + m_UseAutoMass: 1 + m_Mass: 0.7853982 + m_LinearDrag: 0 + m_AngularDrag: 0.05 + m_GravityScale: 0 + m_Material: {fileID: 0} + m_Interpolate: 0 + m_SleepingMode: 1 + m_CollisionDetection: 0 + m_Constraints: 0 +--- !u!58 &5473375028011702753 +CircleCollider2D: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5473375028011702754} + m_Enabled: 1 + m_Density: 1 + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_UsedByEffector: 0 + m_UsedByComposite: 0 + m_Offset: {x: 0, y: 0} + serializedVersion: 2 + m_Radius: 0.5 +--- !u!212 &5473375028011702752 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5473375028011702754} + m_Enabled: 1 + m_CastShadows: 0 + m_ReceiveShadows: 0 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 0 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_Sprite: {fileID: 21300000, guid: d8cf31929ca287542bdc219ec19f002e, type: 3} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 1, y: 1} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 + m_SpriteSortPoint: 0 +--- !u!114 &5473375028011702755 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5473375028011702754} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d16f17bda197c324f8b088f106c66c5c, type: 3} + m_Name: + m_EditorClassIdentifier: + positionInList: 0 diff --git a/Assets/Prefabs/Gravitation Object.prefab.meta b/Assets/Prefabs/Gravitation Object.prefab.meta new file mode 100644 index 0000000..b2dad52 --- /dev/null +++ b/Assets/Prefabs/Gravitation Object.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: e73d8e05bd7498c4a80a63094f4594a8 +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/Game.unity b/Assets/Scenes/Game.unity index 6ff1b0e..732d7f5 100644 --- a/Assets/Scenes/Game.unity +++ b/Assets/Scenes/Game.unity @@ -121,6 +121,140 @@ NavMeshSettings: debug: m_Flags: 0 m_NavMeshData: {fileID: 0} +--- !u!1 &1005736364 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1005736369} + - component: {fileID: 1005736368} + - component: {fileID: 1005736367} + - component: {fileID: 1005736366} + - component: {fileID: 1005736365} + m_Layer: 0 + m_Name: Gravitation Object + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1005736365 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1005736364} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d16f17bda197c324f8b088f106c66c5c, type: 3} + m_Name: + m_EditorClassIdentifier: + positionInList: 0 +--- !u!212 &1005736366 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1005736364} + m_Enabled: 1 + m_CastShadows: 0 + m_ReceiveShadows: 0 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 0 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_Sprite: {fileID: 21300000, guid: d8cf31929ca287542bdc219ec19f002e, type: 3} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 1, y: 1} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 + m_SpriteSortPoint: 0 +--- !u!58 &1005736367 +CircleCollider2D: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1005736364} + m_Enabled: 1 + m_Density: 1 + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_UsedByEffector: 0 + m_UsedByComposite: 0 + m_Offset: {x: 0, y: 0} + serializedVersion: 2 + m_Radius: 0.5 +--- !u!50 &1005736368 +Rigidbody2D: + serializedVersion: 4 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1005736364} + m_BodyType: 0 + m_Simulated: 1 + m_UseFullKinematicContacts: 0 + m_UseAutoMass: 1 + m_Mass: 0.7853982 + m_LinearDrag: 0 + m_AngularDrag: 0.05 + m_GravityScale: 0 + m_Material: {fileID: 0} + m_Interpolate: 0 + m_SleepingMode: 1 + m_CollisionDetection: 0 + m_Constraints: 0 +--- !u!4 &1005736369 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1005736364} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: -15.4, y: 6.35, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 2 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1001 &1803533283597936005 PrefabInstance: m_ObjectHideFlags: 0 @@ -188,6 +322,11 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} + - target: {fileID: 4937878819460165300, guid: bf08d766b6e86cf4a8773489143ab2d5, + type: 3} + propertyPath: gravitationConstant + value: 2 + objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: bf08d766b6e86cf4a8773489143ab2d5, type: 3} --- !u!1001 &9221969453494073200 @@ -257,5 +396,10 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} + - target: {fileID: 9221969453073420047, guid: 6baf9ec6ad712d940a83a3bf6ae3e6b1, + type: 3} + propertyPath: orthographic size + value: 20 + objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 6baf9ec6ad712d940a83a3bf6ae3e6b1, type: 3} diff --git a/Assets/Sprites/Circle.png b/Assets/Sprites/Circle.png new file mode 100644 index 0000000000000000000000000000000000000000..8eb1b1ecc13b6e1d2375151866134cd7a040340c GIT binary patch literal 78 zcmeAS@N?(olHy`uVBq!ia0vp^EFjFm1|(O0oL2{=L_J*`LpWrU|M0W$cr!i aF)*;jval%k