Page 4 of 4

Re: List of loaders for RUBE scenes

Posted: Thu Nov 26, 2015 4:29 pm
by gOzaru
Is rube can be implemented in Unity 5?
It is using C#.

Re: List of loaders for RUBE scenes

Posted: Fri Nov 27, 2015 8:20 am
by iforce2d
What do you mean when you say "implement"? I think unity has its own 2d physics engine doesn't it?
In any case, if Unity can read in a .json file and do something useful with it, then I guess you could 'implement' something...

Re: List of loaders for RUBE scenes

Posted: Fri Nov 27, 2015 9:23 am
by gOzaru
you are right.
I just found out about it.
it is called polygon collider 2D, rigid body 2D.
thank you.

Re: List of loaders for RUBE scenes

Posted: Mon Apr 04, 2016 3:03 am
by johns1111
I just wanted to share a simple loader. The UserData is just inherited from Dictionary<string,object> with no modifications (yet).

Code: Select all

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace JohnDoe
{
	[DataContract]
	public class RubeWorld
	{
		public static RubeWorld Load(System.IO.Stream stream)
		{
			var dataContractJsonSerializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(RubeWorld));
			return dataContractJsonSerializer.ReadObject(stream) as RubeWorld;
		}

		public void Apply(out FarseerPhysics.Dynamics.World world, out UserData worldUserData, out List<BodyImage> bodyImages)
		{
			world = new FarseerPhysics.Dynamics.World(new Microsoft.Xna.Framework.Vector2(0, -10f));
			worldUserData = new UserData();
			bodyImages = new List<BodyImage>();

			this.ApplyCustomProperties(worldUserData, this.customProperties);

			world.Gravity = new Microsoft.Xna.Framework.Vector2(this.gravity.x, this.gravity.y);
			if (this.autoClearForces)
				world.ClearForces();

			List<FarseerPhysics.Dynamics.Body> bodyList = new List<FarseerPhysics.Dynamics.Body>();
			if (this.bodies != null)
			{
				foreach (var rubeBody in this.bodies)
				{
					FarseerPhysics.Dynamics.Body body = new FarseerPhysics.Dynamics.Body(world);
					bodyList.Add(body);
					// apply body data
					UserData bodyUserData = new UserData();
					body.UserData = bodyUserData;
					bodyUserData["name"] = rubeBody.name;
					this.ApplyCustomProperties(bodyUserData, rubeBody.customProperties);
					body.BodyType = (FarseerPhysics.Dynamics.BodyType)rubeBody.type;
					body.Rotation = rubeBody.angle;
					body.AngularDamping = rubeBody.angularDamping;
					body.AngularVelocity = rubeBody.angularVelocity;
					body.Awake = rubeBody.awake;
					body.IsBullet = rubeBody.bullet;
					body.FixedRotation = rubeBody.fixedRotation;
					body.LinearDamping = rubeBody.linearDamping;
					if (rubeBody.linearVelocity != null)
						body.LinearVelocity = new Microsoft.Xna.Framework.Vector2(rubeBody.linearVelocity.x, rubeBody.linearVelocity.y);
					body.SleepingAllowed = this.allowSleep;
					if (rubeBody.massDataCenter != null)
						body.LocalCenter = new Microsoft.Xna.Framework.Vector2(rubeBody.massDataCenter.x, rubeBody.massDataCenter.y);
					body.Mass = rubeBody.massDataMass;
					if (rubeBody.position != null)
						body.Position = new Microsoft.Xna.Framework.Vector2(rubeBody.position.x, rubeBody.position.y);

					// apply fixtures
					if (rubeBody.fixtures != null)
					{
						foreach (var rubeFixture in rubeBody.fixtures)
						{
							UserData fixtureUserData = new UserData();
							this.ApplyCustomProperties(fixtureUserData, rubeFixture.customProperties);

							if (rubeFixture.polygon != null)
							{
								FarseerPhysics.Common.Vertices vertices = new FarseerPhysics.Common.Vertices();
								for (int i = 0; i < rubeFixture.polygon.vertices.x.Length; i++)
									vertices.Add(new Microsoft.Xna.Framework.Vector2(rubeFixture.polygon.vertices.x[i], rubeFixture.polygon.vertices.y[i]));

								FarseerPhysics.Factories.FixtureFactory.AttachPolygon(vertices, rubeFixture.density, body, fixtureUserData);
							}
							else if (rubeFixture.chain != null)
							{
								FarseerPhysics.Common.Vertices vertices = new FarseerPhysics.Common.Vertices();
								for (int i = 0; i < rubeFixture.chain.vertices.x.Length; i++)
									vertices.Add(new Microsoft.Xna.Framework.Vector2(rubeFixture.chain.vertices.x[i], rubeFixture.chain.vertices.y[i]));

								FarseerPhysics.Factories.FixtureFactory.AttachChainShape(vertices, body, fixtureUserData);
							}
						}
					}
				}
			}

			if (this.images != null)
			{
				foreach (var rubeImage in this.images)
				{
					BodyImage bodyImage = new BodyImage();
					if (rubeImage.center != null)
						bodyImage.Center = new Microsoft.Xna.Framework.Vector2(rubeImage.center.x, rubeImage.center.y);
					bodyImage.Height = rubeImage.scale;
					if (!String.IsNullOrWhiteSpace(rubeImage.file))
					{
						bodyImage.File = rubeImage.file;
						if (bodyImage.File.Contains('/'))
							bodyImage.File = bodyImage.File.Substring(bodyImage.File.LastIndexOf('/') + 1);
						if (bodyImage.File.Contains('.'))
							bodyImage.ImageName = bodyImage.File.Substring(0, bodyImage.File.IndexOf('.'));
					}
					bodyImage.Name = rubeImage.name;

					bodyImage.Body = bodyList[rubeImage.body];
					(bodyImage.Body.UserData as UserData) ["image"] = bodyImage;

					if (bodyImages != null)
						bodyImages.Add(bodyImage);
				}
			}
		}

		private void ApplyCustomProperties(UserData userData, RubeCustomProperty[] customProperties)
		{
			if (customProperties != null)
			{
				foreach (var rubeCustomProperty in customProperties)
				{
					if (rubeCustomProperty.boolValue.HasValue)
						userData[rubeCustomProperty.name] = rubeCustomProperty.boolValue.Value;
					if (rubeCustomProperty.colorValue != null)
						userData[rubeCustomProperty.name] = new Microsoft.Xna.Framework.Color(rubeCustomProperty.colorValue[0], rubeCustomProperty.colorValue[1], rubeCustomProperty.colorValue[2], rubeCustomProperty.colorValue[3]);
					if (rubeCustomProperty.floatValue.HasValue)
						userData[rubeCustomProperty.name] = rubeCustomProperty.floatValue.Value;
					if (rubeCustomProperty.intValue.HasValue)
						userData[rubeCustomProperty.name] = rubeCustomProperty.intValue.Value;
					if (!String.IsNullOrWhiteSpace(rubeCustomProperty.stringValue))
						userData[rubeCustomProperty.name] = rubeCustomProperty.stringValue;
				}
			}
		}

		[DataMember]
		public RubeVector gravity;
		[DataMember]
		public bool allowSleep;
		[DataMember]
		public bool autoClearForces;
		[DataMember]
		public int positionIterations;
		[DataMember]
		public int velocityIterations;
		[DataMember]
		public int stepsPerSecond;
		[DataMember]
		public bool warmStarting;
		[DataMember]
		public bool continuousPhysics;
		[DataMember]
		public bool subStepping;

		[DataMember(Name = "body")]
		public RubeBody[] bodies;

		[DataMember(Name = "image")]
		public RubeImage[] images;

		[DataMember]
		public RubeCustomProperty[] customProperties;
	}

	[DataContract]
	public class RubeBody
	{
		[DataMember]
		public string name;
		[DataMember]
		public int type;
		[DataMember]
		public float angle;
		[DataMember]
		public float angularDamping;
		[DataMember]
		public float angularVelocity;
		[DataMember]
		public bool awake;
		[DataMember]
		public bool bullet;
		[DataMember]
		public bool fixedRotation;
		[DataMember]
		public float linearDamping;
		[DataMember]
		public RubeVector linearVelocity;
		[DataMember(Name = "massData-mass")]
		public float massDataMass;
		[DataMember(Name = "massData-center")]
		public RubeVector massDataCenter;
		[DataMember(Name = "massData-I")]
		public float massDataI;
		[DataMember(Name = "position")]
		public RubeVector position;
		[DataMember(Name = "fixture")]
		public RubeFixture[] fixtures;
		[DataMember]
		public RubeCustomProperty[] customProperties;
	}

	[DataContract]
	public class RubeFixture
	{
		[DataMember]
		public string name;
		[DataMember]
		public float density;
		[DataMember(Name = "filter-categoryBits")]
		public int filterCategoryBits;
		[DataMember(Name = "filter-maskBits")]
		public int filterMaskBits = 1;
		[DataMember(Name = "filter-groupIndex")]
		public int filterGroupIndex = 1;
		[DataMember]
		public float friction;
		[DataMember]
		public float restitution;
		[DataMember]
		public bool sensor;
		[DataMember]
		public RubeCircleShape circle;
		[DataMember]
		public RubePolygonShape polygon;
		[DataMember]
		public RubeChainShape chain;
		[DataMember]
		public RubeCustomProperty[] customProperties;
	}

	[DataContract]
	public class RubeCircleShape
	{
		[DataMember]
		public RubeVector center;
		[DataMember]
		public float radius;
	}

	[DataContract]
	public class RubePolygonShape
	{
		[DataMember]
		public RubeVertices vertices;
	}

	[DataContract]
	public class RubeChainShape
	{
		[DataMember]
		public RubeVertices vertices;
		[DataMember]
		public bool hasNextVertex;
		[DataMember]
		public bool hasPrevVertex;
		[DataMember]
		public RubeVector nextVertex;
		[DataMember]
		public RubeVector prevVertex;
	}

	[DataContract]
	public class RubeCustomProperty
	{
		[DataMember]
		public string name;
		[DataMember(Name = "bool")]
		public bool? boolValue;
		[DataMember(Name = "color")]
		public int[] colorValue;
		[DataMember(Name = "float")]
		public float? floatValue;
		[DataMember(Name = "int")]
		public int? intValue;
		[DataMember(Name = "string")]
		public string stringValue;
	}

	[DataContract]
	public class RubeImage
	{
		[DataMember]
		public float aspectScale;
		[DataMember]
		public int body;
		[DataMember]
		public RubeVector center;
		[DataMember]
		public RubeVertices corners;
		[DataMember]
		public string file;
		[DataMember]
		public int filter;
		[DataMember]
		public float[] glDrawElements;
		[DataMember]
		public float[] glTexCoordPointer;
		[DataMember]
		public float[] glVertexPointer;
		[DataMember]
		public string name;
		[DataMember]
		public float opacity;
		[DataMember]
		public float scale;
	}

	[DataContract]
	public class RubeVertices
	{
		[DataMember]
		public float[] x;
		[DataMember]
		public float[] y;
	}

	[DataContract]
	public class RubeVector
	{
		[DataMember]
		public float x;
		[DataMember]
		public float y;
	}
}

Re: List of loaders for RUBE scenes

Posted: Sun May 01, 2016 9:01 pm
by cikamika80
The RUBE Loader for Phaser Box2D doesn't seem to work when the simulation is compiled from Typescript. Is there any chance of getting Typescript support for RUBE Loader?

Re: List of loaders for RUBE scenes

Posted: Sun May 01, 2016 9:12 pm
by iforce2d
cikamika80 wrote:Is there any chance of getting Typescript support for RUBE Loader?
Let just me google "typescript".... :D
If you could be more descriptive than "doesn't work" you might find more help from others who know about it.

Re: List of loaders for RUBE scenes

Posted: Sun May 01, 2016 9:38 pm
by cikamika80
iforce2d wrote:
cikamika80 wrote:Is there any chance of getting Typescript support for RUBE Loader?
Let just me google "typescript".... :D
If you could be more descriptive than "doesn't work" you might find more help from others who know about it.
The Box2D plugin for Phaser just fails silently after I link the phaser-rube.min.js in my HTML file, even without any attempt to load a RUBE scene. If I remove phaser-rube.min.js, the game loads fine. My main.js file is compiled from Typescript into Javascript. I'm guessing it's a namespacing issue. :?:

Code: Select all

<script src="js/phaser.2.4.7.min.js"></script>
<script src="js/box2d-plugin-full.min.js"></script>
<script src="js/phaser-rube.min.js"></script>
<script src="js/main.js"></script>

Re: List of loaders for RUBE scenes

Posted: Fri May 20, 2016 3:21 pm
by sheik
cikamika80 wrote:
iforce2d wrote:
cikamika80 wrote:Is there any chance of getting Typescript support for RUBE Loader?
Let just me google "typescript".... :D
If you could be more descriptive than "doesn't work" you might find more help from others who know about it.
The Box2D plugin for Phaser just fails silently after I link the phaser-rube.min.js in my HTML file, even without any attempt to load a RUBE scene. If I remove phaser-rube.min.js, the game loads fine. My main.js file is compiled from Typescript into Javascript. I'm guessing it's a namespacing issue. :?:

Code: Select all

<script src="js/phaser.2.4.7.min.js"></script>
<script src="js/box2d-plugin-full.min.js"></script>
<script src="js/phaser-rube.min.js"></script>
<script src="js/main.js"></script>
I'm having a similar issue with Phaser 2.4.7 and 2.4.8.
I created a thread in the "Bug reports" board you may want to follow.
https://www.iforce2d.net/forums/viewtopic.php?f=7&t=510