Browse Source

Adding simple Atomic C# Physics spawner example

Josh Engebretson 9 years ago
parent
commit
606d41801e

+ 3 - 0
AtomicNET/Physics2D/.gitignore

@@ -0,0 +1,3 @@
+Cache/*
+AtomicNET/*
+Resources/AtomicProject.*

+ 0 - 0
AtomicNET/Physics2D/Physics2D.atomic


+ 5 - 0
AtomicNET/Physics2D/Resources.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "c5a14ededf7c8df707fd8b3f83ffe0d3",
+	"FolderImporter": {}
+}

+ 5 - 0
AtomicNET/Physics2D/Resources/Components.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "454d0bd8b7099b6e27219171adfda921",
+	"FolderImporter": {}
+}

+ 119 - 0
AtomicNET/Physics2D/Resources/Components/PhysicsSpawner.cs

@@ -0,0 +1,119 @@
+using System;
+using AtomicEngine;
+
+public class PhysicsSpawner : CSComponent
+{
+    public void Start()
+    {
+
+        viewport = AtomicNET.GetSubsystem<Renderer>().GetViewport(0);
+
+        boxSprite = AtomicNET.Cache.GetResource<Sprite2D>("Sprites/Box.png");
+        ballSprite = AtomicNET.Cache.GetResource<Sprite2D>("Sprites/Ball.png");
+
+        var ground = Scene.CreateChild("Ground");
+
+        ground.Position = new Vector3(0.0f, -5.0f, 0.0f);
+        ground.Scale = new Vector3(200.0f, 1.0f, 0.0f);
+
+        ground.CreateComponent<RigidBody2D>();
+        ground.CreateComponent<StaticSprite2D>().Sprite = boxSprite;
+
+        // Create box collider for ground
+        var groundShape = ground.CreateComponent<CollisionBox2D>();
+        // Set box size
+        groundShape.Size = new Vector2(0.32f, 0.32f);
+        // Set friction
+        groundShape.Friction = 0.5f;
+
+    }
+
+
+    void Update(float timeStep)
+    {
+        if (spawnDelta > 0)
+        {
+            spawnDelta -= timeStep;
+            return;
+        }
+
+        var input = AtomicNET.GetSubsystem<Input>();
+
+        if (input.GetMouseButtonDown(Constants.MOUSEB_LEFT))
+        {
+            var mousePos = input.GetMousePosition();
+
+            var screenPos = viewport.ScreenToWorldPoint(mousePos.X, mousePos.Y, 0);
+
+            if ((lastSpawn - screenPos).Length < 0.35)
+                return;
+
+            lastSpawn = screenPos;
+            spawnDelta = .025f;
+
+            var node = Scene.CreateChild("RigidBody");
+
+            node.SetPosition(new Vector3(screenPos.X, screenPos.Y, 0.0f));
+
+            // Create rigid body
+            var body = node.CreateComponent<RigidBody2D>();
+
+            body.BodyType = BodyType2D.BT_DYNAMIC;
+
+            var staticSprite = node.CreateComponent<StaticSprite2D>();
+
+            if (spawnCount % 2 == 0)
+            {
+                staticSprite.Sprite = boxSprite;
+
+                // Create box
+                var box = node.CreateComponent<CollisionBox2D>();
+                // Set size
+                box.Size = new Vector2(0.32f, 0.32f);
+                // Set density
+                box.Density = 1.0f;
+                // Set friction
+                box.Friction = 0.5f;
+                // Set restitution
+                box.Restitution = 0.1f;
+            }
+            else
+            {
+                staticSprite.Sprite = ballSprite;
+
+                // Create circle
+                var circle = node.CreateComponent<CollisionCircle2D>();
+                // Set radius
+                circle.Radius = 0.16f;
+                // Set density
+                circle.Density = 1.0f;
+                // Set friction.
+                circle.Friction = 0.5f;
+                // Set restitution
+                circle.Restitution = 0.1f;
+            }
+
+            spawnCount++;
+
+        }
+
+    }
+
+    float Random()
+    {
+        return (float)random.NextDouble();
+    }
+
+    Sprite2D boxSprite;
+    Sprite2D ballSprite;
+    Viewport viewport;
+
+    float spawnDelta = 0;
+    Vector3 lastSpawn = new Vector3(1000, 1000, 0);
+
+    Random random = new Random();
+    uint spawnCount = 0;
+
+}
+
+

+ 5 - 0
AtomicNET/Physics2D/Resources/Components/PhysicsSpawner.cs.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "c9462fe817faf7313b1be2ad97274422",
+	"CSharpImporter": null
+}

+ 5 - 0
AtomicNET/Physics2D/Resources/Particles.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "446a4b7ae157fcb28a8d8698fcbb07d0",
+	"FolderImporter": {}
+}

+ 39 - 0
AtomicNET/Physics2D/Resources/Particles/particle.pex

@@ -0,0 +1,39 @@
+<particleEmitterConfig>
+  <texture name="texture.png"/>
+  <sourcePosition x="100.00" y="100.00"/>
+  <sourcePositionVariance x="30" y="30"/>
+  <speed value="1"/>
+  <speedVariance value=".25"/>
+  <particleLifeSpan value="2"/>
+  <particleLifespanVariance value="5"/>
+  <angle value="0"/>
+  <angleVariance value="360"/>
+  <gravity x="-44.6" y="-226.78"/>
+  <radialAcceleration value="-24.26"/>
+  <tangentialAcceleration value="-8.79"/>
+  <radialAccelVariance value="124.06"/>
+  <tangentialAccelVariance value="170.19"/>
+  <startColor red="0.5" green="0.5" blue="0.5" alpha="1.0"/>
+  <startColorVariance red="0.25" green="0.25" blue="0.25" alpha="0.0"/>
+  <finishColor red="0.5" green="0.5" blue="0.5" alpha="1.0"/>
+  <finishColorVariance red="0.25" green="0.25" blue="0.25" alpha="0.0"/>
+  <maxParticles value="250"/>
+  <startParticleSize value="8"/>
+  <startParticleSizeVariance value="5"/>
+  <finishParticleSize value="10"/>
+  <FinishParticleSizeVariance value="5"/>
+  <duration value="-1.00"/>
+  <emitterType value="0"/>
+  <maxRadius value="43.98"/>
+  <maxRadiusVariance value="377.4"/>
+  <minRadius value="84.07"/>
+  <minRadiusVariance value="307.95"/>
+  <rotatePerSecond value="271.58"/>
+  <rotatePerSecondVariance value="243.34"/>
+  <blendFuncSource value="770"/>
+  <blendFuncDestination value="1"/>
+  <rotationStart value="43.88"/>
+  <rotationStartVariance value="275.21"/>
+  <rotationEnd value="184.57"/>
+  <rotationEndVariance value="180.85"/>
+</particleEmitterConfig>

+ 5 - 0
AtomicNET/Physics2D/Resources/Particles/particle.pex.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "f8c6188180699fbedda600eae3f4a49f",
+	"PEXImporter": {}
+}

BIN
AtomicNET/Physics2D/Resources/Particles/texture.png


+ 5 - 0
AtomicNET/Physics2D/Resources/Particles/texture.png.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "119e283e886e515dd05420f7fe21d273",
+	"TextureImporter": {}
+}

+ 3 - 0
AtomicNET/Physics2D/Resources/README.TXT

@@ -0,0 +1,3 @@
+
+AtomicNET Physics
+------------------

+ 5 - 0
AtomicNET/Physics2D/Resources/README.TXT.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "85810cbd3375757ff848760fb2a2548a",
+	"TextImporter": {}
+}

+ 5 - 0
AtomicNET/Physics2D/Resources/Scenes.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "eff0f6bb8943a7f8c983419e408e15b2",
+	"FolderImporter": {}
+}

+ 75 - 0
AtomicNET/Physics2D/Resources/Scenes/TheScene.scene

@@ -0,0 +1,75 @@
+<?xml version="1.0"?>
+<scene id="1">
+	<attribute name="Name" value="" />
+	<attribute name="Time Scale" value="1" />
+	<attribute name="Smoothing Constant" value="50" />
+	<attribute name="Snap Threshold" value="5" />
+	<attribute name="Elapsed Time" value="0" />
+	<attribute name="Next Replicated Node ID" value="369" />
+	<attribute name="Next Replicated Component ID" value="1982" />
+	<attribute name="Next Local Node ID" value="16778496" />
+	<attribute name="Next Local Component ID" value="16777216" />
+	<attribute name="Variables" />
+	<attribute name="Variable Names" value="" />
+	<component type="Octree" id="2" />
+	<component type="DebugRenderer" id="3" />
+	<component type="PhysicsWorld2D" id="1977">
+		<attribute name="Allow Sleeping" value="true" />
+		<attribute name="Warm Starting" value="true" />
+		<attribute name="Auto Clear Forces" value="true" />
+	</component>
+	<node id="2">
+		<attribute name="Is Enabled" value="true" />
+		<attribute name="Name" value="Zone" />
+		<attribute name="Position" value="0 0 0" />
+		<attribute name="Rotation" value="1 0 0 0" />
+		<attribute name="Scale" value="1 1 1" />
+		<attribute name="Variables" />
+		<component type="Zone" id="4">
+			<attribute name="Bounding Box Min" value="-10000 -10000 -10000" />
+			<attribute name="Bounding Box Max" value="10000 10000 10000" />
+			<attribute name="Ambient Color" value="0.4 0.4 0.4 1" />
+		</component>
+	</node>
+	<node id="3">
+		<attribute name="Is Enabled" value="true" />
+		<attribute name="Name" value="GlobalLight" />
+		<attribute name="Position" value="0 0 0" />
+		<attribute name="Rotation" value="0.888074 0.325058 -0.325058 0" />
+		<attribute name="Scale" value="1 1 1" />
+		<attribute name="Variables" />
+		<component type="Light" id="5">
+			<attribute name="Light Type" value="Directional" />
+			<attribute name="Cast Shadows" value="true" />
+			<attribute name="CSM Splits" value="10 20 50 0" />
+			<attribute name="View Size Quantize" value="1" />
+			<attribute name="View Size Minimum" value="5" />
+			<attribute name="Depth Constant Bias" value="0.00025" />
+			<attribute name="Depth Slope Bias" value="0.001" />
+		</component>
+	</node>
+	<node id="361">
+		<attribute name="Is Enabled" value="true" />
+		<attribute name="Name" value="Camera" />
+		<attribute name="Position" value="0 0 -5" />
+		<attribute name="Rotation" value="1 0 0 0" />
+		<attribute name="Scale" value="1 1 1" />
+		<attribute name="Variables" />
+		<component type="Camera" id="1973">
+			<attribute name="Near Clip" value="0" />
+			<attribute name="Orthographic" value="true" />
+		</component>
+	</node>
+	<node id="363">
+		<attribute name="Is Enabled" value="true" />
+		<attribute name="Name" value="Spawner" />
+		<attribute name="Position" value="0 0 0" />
+		<attribute name="Rotation" value="1 0 0 0" />
+		<attribute name="Scale" value="1 1 1" />
+		<attribute name="Variables" />
+		<component type="CSComponent" id="1975">
+			<attribute name="Assembly" value="CSComponentAssembly;AtomicProject.dll" />
+			<attribute name="Class" value="PhysicsSpawner" />
+		</component>
+	</node>
+</scene>

+ 8 - 0
AtomicNET/Physics2D/Resources/Scenes/TheScene.scene.asset

@@ -0,0 +1,8 @@
+{
+	"version": 1,
+	"guid": "ed87e2d80487df697a2e8fafdcca1229",
+	"SceneImporter": {
+		"sceneCamRotation": "1 0 0 0",
+		"sceneCamPosition": "0 0 0"
+	}
+}

+ 5 - 0
AtomicNET/Physics2D/Resources/Scripts.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "e4a74b5d6803ecf9eb1be3ec3fea325d",
+	"FolderImporter": {}
+}

+ 7 - 0
AtomicNET/Physics2D/Resources/Scripts/main.js

@@ -0,0 +1,7 @@
+// This script is the main entry point of the game
+
+
+var scene = Atomic.player.loadScene("Scenes/TheScene.scene");
+var camera = scene.getChild("Camera").getComponent("Camera");
+camera.setOrthoSize(Atomic.graphics.height * Atomic.PIXEL_SIZE);
+camera.setZoom(.75 * Math.min(Atomic.graphics.width / 1280.0, Atomic.graphics.height / 800.0));

+ 7 - 0
AtomicNET/Physics2D/Resources/Scripts/main.js.asset

@@ -0,0 +1,7 @@
+{
+	"version": 1,
+	"guid": "eca179757c676750cf21705aac3161ad",
+	"JavascriptImporter": {
+		"IsComponentFile": false
+	}
+}

+ 5 - 0
AtomicNET/Physics2D/Resources/Sprites.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "084acd70b72b2db1f3204453d2af4a0b",
+	"FolderImporter": {}
+}

BIN
AtomicNET/Physics2D/Resources/Sprites/Ball.png


+ 5 - 0
AtomicNET/Physics2D/Resources/Sprites/Ball.png.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "e7735ed21e7a01c3453aa2fc19849b88",
+	"TextureImporter": {}
+}

BIN
AtomicNET/Physics2D/Resources/Sprites/Box.png


+ 5 - 0
AtomicNET/Physics2D/Resources/Sprites/Box.png.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "926f15d4f4871721e45bd7b21063803f",
+	"TextureImporter": {}
+}

+ 51 - 0
AtomicNET/Physics2D/Settings/Engine.json

@@ -0,0 +1,51 @@
+{
+  "desktop": {
+
+    "engine" : {
+      "workerThreads" : true,
+      "logQuiet" : false,
+      "logLevel" : 1
+    },
+
+    "graphics": {
+      "headless": false,
+      "frameLimiter" : true,
+      "flushGPU" : false,
+      "forceGL2" : false,
+      "orientations" : "LandscapeLeft LandscapeRight",
+      "vsync" : false,
+      "tripleBuffer" : false,
+      "multiSample" : 1,
+      "renderPath" : "forward",
+      "shadows" : true,
+      "lowQualityShadows" : false,
+      "materialQuality" : "high",
+      "textureQuality" : "high",
+      "textureFilterMode" : "trilinear",
+      "textureAnisotropy" : 4
+    },
+
+    "window" : {
+      "title" : "Atomic 2D Physics",
+      "fullscreen" : false,
+      "borderless" : false,
+      "resizable" : false,
+      "width" : 1280,
+      "height" : 720
+    },
+
+    "sound": {
+      "enabled" : true,
+      "interpolation" : true,
+      "stereo" : true,
+      "bufferMS": 100,
+      "mixRate" : 44100
+    },
+
+    "input" : {
+      "touchEmulation" : false
+    }
+
+  }
+
+}