![]() |
![]() |
|
Deadline To give more time to developers |
Sensors
It had snowed at University and Hari Seldon was looking through the window while his brain told his finger to press the key that will deploy the application to the mobile device he connected a minute ago.
The electric signal had arrived to the first nerve and he remember how all that started. He saw the Newton cradle in rector’s desktop in the last meeting and he wanted one, so he created a new Cell•SDK project and started to write:
class MainScreen : Screen
{
/// <summary>
/// Sets the screen up (UI components, multimedia content, etc.)
/// </summary>
public override void Initialize()
{
base.Initialize();
}
}
He needed a physic world and a good looking background, so he added at the Initialize method:
CreatePhysicWorld(Vector2.UnitX * -20f, true, false, Vector2.Zero);
SetBackground(ResourceManager.CreateImage("background"),Adjustment.CENTER);
He saw himself reading the Basic and Physic tutorials from Cell•SDK web to remember a few things.
Like he wanted the application to be placed in the most important mobile devices he knew he will need a little variable to make all positions coherent depending on the screen resolution so he added a class variable called offset:
Vector2 offset;
And added these lines to the Initialize method:
#if IOS
offset=new Vector2(80,80);
#else
offset = Vector2.Zero;
#endif
The shoulder nerve receives the order to pass the message for the arm.
He was going to need three images: one for the balls metal support, other for a ball, and other to use it as part of the Sensor needed to create the magic effect between balls, so he add these class variables after the offset definition:
Image iSupport; Image iBall; Image iMetalCradle;
And initialized them in this way, after setting the background:
iSupport = ResourceManager.CreateImage(Color.Transparent, 1, 1);
iBall = ResourceManager.CreateImage("ball");
iMetalCradle = ResourceManager.CreateImage("metalSupport");
He wanted to create a Newton Cradle with six balls, so he decide to delegate this responsibility to a parameterized function and created this:
public void CreateNewtonBall(Image iGround, Image iSphere, Vector2 vPosition, int ropeLength, bool touchable)
{
Label lSupport = new Label(iGround); //Static Label
Label lBall = new Label(iSphere); //Label with the ball texture.
//We add the lSupport as a static physic object. So it does not interact with anything
AddComponent(lSupport, vPosition.X + offset.X, vPosition.Y + offset.Y, BodyShape.SQUARE, BodyType.STATIC, Category.Cat1);
//We add the IBall label
AddComponent(lBall, vPosition.X - ropeLength + offset.X, vPosition.Y + offset.Y, BodyShape.CIRCLE, BodyType.DYNAMIC, Category.Cat1);
lBall.BringToFront = false;
lBall.PhysicBody.Mass = 4.0f; //ball mass.
lBall.PhysicBody.Restitution = 1.0f;//ball restitution
lBall.PhysicBody.Friction = 8.0f; //ball friction
lBall.Touchable = touchable; // set the Touchable to true or false
lBall.Draggable = touchable; //set the Draggable to true or false
PhysicWorld.AddSensor(lSupport, lBall, lSupport.Position + lSupport.Size / 2, lBall.Position + lBall.Size / 2, false, 30, 30, 800.0f, 100.0f);
}
The elbow nerve receives the order to pass the message for the hand.
This method, he thought, will create two labels with the images passed as parameters and in the position indicated, adding the offset he knew he will need.
He saw himself writing the code necessary to initialize the six balls he added after the iMetalCradle initialization:
CreateNewtonBall(iSupport, iBall, new Vector2(420, 200 + 0 * ballSize), 300, true); CreateNewtonBall(iSupport, iBall, new Vector2(420, 200 + 1 * ballSize), 300, false); CreateNewtonBall(iSupport, iBall, new Vector2(420, 200 + 2 * ballSize), 300, false); CreateNewtonBall(iSupport, iBall, new Vector2(420, 200 + 3 * ballSize), 300, false); CreateNewtonBall(iSupport, iBall, new Vector2(420, 200 + 4 * ballSize), 300, false); CreateNewtonBall(iSupport, iBall, new Vector2(420, 200 + 5 * ballSize), 300, true);
As the evening light was fading, he remember the metal support image he will need to have a realistic Newton Cradle, so he added to the scene:
Label lMetalSupport = new Label(iMetalCradle); AddComponent(lMetalSupport, 0, 0); lMetalSupport.Touchable = false;
He knew that the physic world that Cell•SDK creates has a property called Iterations indicating how many times in a second the physic engine will calculate trajectories, velocities, and so on. The default value is 90 and he decided to set it at 120 to have a very realistic effect. As we all know Dr. Hari Seldon like this kind of things.
PhysicWorld.Iterations = 120;
The hand receives the order to pass the message for the finger and all was ended:

Today is 12.012 in Galactic Era (-24 in the Foundation Era).
Keep on cool coding.
You can get the code in Github















