15 Apr 2015

Programming for Physics Simulations – How is it Done ?


A lot of people even those who have experience in programming have very wrong ideas about how programs work. Moreover the common notion among the general public is that software development only requires knowledge and experience in programming, however its actually quite the contrary. Software development can have very high level of specialization unlike many other fields including medical, for instance one experienced cardiologist can do the same another cardiologist can. How two highly experienced web developers may not be able to do what the other can. What people don’t understand is programming on its own is just a tool and nearly worthless on its own. Its like maths, whats the use of maths? No use. But apply math in physics, chem, astronomy …… then suddenly it becomes very useful. That’s how programming is too , People often specialize in different fields like maths, physics and  architecture. Like for example if a developer needs to develop a CAD program , he or she would need to have a good knowledge of architecture to effectively develop a suitable product, therefore they would need to specialize in architecture too.

So here in this post I intend to instill some ideas into the minds of new developers. To do this I will explain how to simulate a basic physics concept used in cut the rope from scratch through C++ code. I will keep it as simple as possible. However one needs basic knowledge of trigonometry to follow. Please note that all the programs embedded on this post is either HTML5 or flash, however I will be discussing from C++ point of view.



This is what we are gonna make using C++, Its pretty basic right ? Yeah it is. But even this is not very easy as people might feel, Many people will be thinking oh whats the big deal, you pull it, it follows , whats so hard? Indeed you pull it, it follows but nothing on a computer ever happens on its own, Someone should take care of the physics through code.

Ok so how do I think the right way ?

The right way to think is not to think in terms of code but in terms of logic, Imagine this as a physics situation, there are 2 balls, separated by a distance and an elastic rope joined between them,  larger the distance, faster the ball moves. However even if the rope becomes slack, due to inertia the ball continues to be in motion. So inertia has to be also considered. Gravity needs to be considered to make the thing realistic. Finally the fact that we are working in a 2D space has to be kept in mind.  
On further analysis, You might feel that there is a need for friction / resistance between the balls, or they would never stop moving

Physics solution :

This is the solution to the problem in physics point of view

Here 'T' is the tension force in the elastic rope. 'x' is the extension (elongation) of  the rope, 'A' is the rope’s cross sectional area, 'L' is the natural length of the rope while 'Y' is the Young's modulus.  This is the formula derived from Hooke's law.

We need to resolve these forces into vectors since we are working in 2D space. Then we need to add the gravitational force on the vertical component so it will be T sinθ + mg.

From here its easy, all one needs to do is calculate acceleration in both components using 'F = ma', then find change in velocity using 'v = u + at', And from velocity you can find new position of the balls. But you might have already realized both acceleration and velocities keep on changing , that means to correctly calculate the final velocity, you will need to use integral calculus which is beyond the scope of this post.

However this analysis is correct and can be implemented in programming

Programming Implementation :

Remember I said we need integration to calculate the velocities and position at a given time? But in programming we have a work around i.e  Running several iterations to approximately calculate the velocity and position . This means we split a time interval, say 1 sec into many smaller time intervals Say 0.1 s, Since 0.1s is a very small interval, we can assume the acceleration and velocity doesn't change in that interval . Accuracy can be increased to any desired extent by decreasing the small time interval.

Ok now we are finally ready to get started
Main variables

float x1=200,y1=200,x2=200,y2=200, s=100;
float vx2=0,vy2=0;

(x1,y1) are the initial position of the first ball , the ball which is controlled by the mouse. (x2,y2) is the position of the ball which is connected to the first ball. 'vx2' and 'vy2' are the components of velocity of the second ball in the X and Y directions respectively. 's' is the natural length of the rope.

The following bits of code has to be put in a loop that runs the iterations

1)  Getting the coordinates of the mouse and setting that as the coordinates of the first ball
 
x1=mousex(); 
y1=mousey();

2) Finding distance and angle between the 2 centres.


float d1=distance(x1,y1,x2,y2);
float ang1=angle(x1,y1,x2,y2);

Here distance() is a function which calculates distance between 2 points at an instant of time and angle() is a function which calculates angle between 2 points i.e. angle made by the line joining the 2 points and the horizontal.

3) Checking if the rope is stretched

We calculate distance between the 2 balls to know if the rope is stretched and angle to resolve force into components

//If the rope is stretched
if(d1-s>0) {   //Resolving tension into vectors
    vy2+=(d1-s)*sin(ang1)*.007;
    vx2+=(d1-s)*cos(ang1)*.007;
}

/*  here all the constants clubbed together as 0.007 , you can change it
We are adding the force directly to velocities by assuming balls to be of unit
mass and time interval to also be unit time
*/

4) Adding gravitational force

 vy2+=.23;
 //Gravitational force added on y coordinate
// 0.23 is an experimentally calculated value ( you can experiment too ! )

5) Moving the body and friction
We move the body as follows as we have assumed time interval to be unit time interval

y2+=vy2;
 x2+=vx2;
//Friction reduces velocity by 1%
vy2*=.99;
vx2*=.99;
      
6) Summing up

//draws 2 circles on the screen
circle(x1,y1,25);
circle(x2,y2,25);
//draws a line between them
line(x1,y1,x2,y2);

And we are done !!
Several improvements can be made like having parameters such as length, area, masses of spheres etc. Further more this concept can be expanded on more than 2 balls


Full code :

#include<graphics.h>

#include<math.h>

//Distance is a function which calculates Distance between 2 points

float distance(int x1,int y1,int x2,int y2)

{

    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

}

//Angle is a function which calculates the angle between 2 points

// ( tan inverse of the Slope of the line joining the 2 points )

float angle(int x1 , int y1, int x2 , int y2)

{

    float ang;

    //In case the 2 points are perpendicular

    if(x1-x2==0)

    {

        ang=-1.57;

    }

    else

    {

        ang = atan((float)(y1 - y2) / (x1 - x2));

    }

    //Quadtrants

    if (ang < 0 && y1> y2)

    {

        ang += 3.14;

    }

    else if (ang > 0 && x1 < x2)

    {

        ang += 3.14;

    }

    return ang;

}

int main()

{

    //declaration of variables

    float x1=200,y1=200,x2=200,y2=200, s=100;

    float vx2=0,vy2=0;

    //Creating a new window

    initwindow (550, 400,"Windows BGI",50,50, false,true);

    while(1)

    {

        //Getting the coordinates of the mouse

        x1=mousex();

        y1=mousey();

        //Finding distance and angle between 2 points

        float d1=distance(x1,y1,x2,y2);

        float ang1=angle(x1,y1,x2,y2);

        //If the rope is stretched

        if(d1-s>0)

        {

            //Resolving tension into vectors

            vy2+=(d1-s)*sin(ang1)*.007;

            vx2+=(d1-s)*cos(ang1)*.007;

        }

        //Gravitational force on y coordinate

        vy2+=.23;

        //Now moving the body

        y2+=vy2;

        x2+=vx2;

        //Friction reduces velocity by 1%

        vy2*=.99;

        vx2*=.99;

        //draws 2 circles on the screen

        circle(x1,y1,25);

        circle(x2,y2,25);

        //draws a line between them

        line (x1,y1,x2,y2);

        //waits for 40 milliseconds

        delay(40);

        //something to do with memory

        swapbuffers();

        //clear the screen so that a new frame can be redrawn

        cleardevice();

    }

    return 0;

}

9 Apr 2015

Why I moved to an iPhone from Android


My first smartphone was an android phone. The first phone I got which wasn’t a hand-me-down was an android phone. The first phone I saved up for and bought was an android phone. Android introduced me to the world of tech, to the wondrous world of rooting, ROMming and the incredible XDA forums. Being known as someone with a major predilection for android, I surprised most of my friends when I told them that I had, a few weeks ago, switched to the iPhone 6. And now, after these few weeks of use, I do admit something that a year ago I wouldn't have said even if my life depended on it: The iPhone is better than most, if not all, android phones.

Back in March 2013, when I got my first respectable android phone, the Nexus 4, I despised the iPhone. I saw the iPhone 5 as the epitome of business exploitation. Exorbitant prices, specifications that were mostly along the lines of current android flagships, if not worse and a cagey OS with no customizability and ridiculous restrictions. And iTunes was (and still is, to be frank) a joke. Granted, its DAC put android phones to shame and in my eyes, it looked better than any other smartphone. That didn't make up for the other shortfalls though. I remember vehemently arguing against the iPhone 5S when it came out, convincing my dad to hang on to his failing Galaxy S2. My mom merely gave me a grin as I berated the iPhone franchise and ridiculed her aging iPhone 4. I told myself I would abhor iPhones and anything else that Apple made that ran iOS.


Two years later, what changed? Not much. I still believe Android is exponentially more powerful than iOS, has much greater potential and, after Lollipop, is more of a looker than iOS is.

Why switch to iPhone then? Two words: Consistency and Ecosystem.

Yes, the Galaxies and HTC Ones have more RAM. Yes, the Note 4 has twice as many megapixels (16 MP) in the primary camera. I’ll admit the “Retina” resolution of 750 × 1334 sounds pathetic against an LG G3’s 2K (QHD) screen. A fingerprint scanner and an aluminum body aren’t all that salient anymore. Sony’s Z phones can withstand dips, while the iPhone can’t.


Knowing all this, I would still recommend an iPhone over any other phone today. Coming from someone who was, till a few months ago a borderline fanboy for Android, that’s saying something.


The first thing that struck me after using the iPhone for a few days was how rewarding it was to be invested in Apple’s ecosystem. The iPhone and my Macbook sync so well, it seems like a given. Continuity, Handoff and everything else that had me excited for Yosemite make the iPhone and Macbook combination much more than just a sum of parts. Airdrop and Airplay live up to the hype. Assignments I work on in the bus on the iPhone sync with the Mac as soon as I connect to the school’s WiFi. Tabs I open on the Mac in school I can read on the iPhone, on the bus home. Backing up Photos on a computer aren't an issue anymore. Not that Android can’t compete: Chrome OS and Android supposedly feature similar integration, but Chrome OS doesn’t really cut it.


I don’t intend this as a damning statement, but the iPhone has worked better for me than any Android phone I’ve used. It doesn’t do as much as a Galaxy or a Note, but what it does do, it does perfectly. This and the consistency with which it works sets it apart from Android. If there’s one thing that’s set every iPhone apart from the Android flagships of its time, it’s how well it works. The Galaxy S6 has a truckload of features (gimmicks?), Sony’s Xperia Z3 boasts waterproofing and the LG G3 has some pretty slick camera tricks and knock on, putting the iPhone 6 seem austere in comparison. Every Android flagship puts the iPhone to shame on paper, with numerous processor cores, twice, sometimes thrice, as much RAM as the iPhone and much denser screens. But none of these seem to hold a candle to the iPhone in terms of everyday user experience and longevity.
In my experience at least, the iPhone 6 has just worked so much better than Android. I haven’t had to deal with abysmal battery life (Nexus 4 and 5), tons of rubbish gimmicks (Note 3), Touchwiz, which cannot seem to fix its issue of giving crazy lag after just a few months of use, and lots more. The Note 3 had a much larger battery, but for whatever reason the iPhone lasts just as long, if not longer. The Note 3 (13MP) and Nexus 4 (8MP) had better cameras on paper, but don’t even come within a one mile radius of the iPhone’s picture quality and its ability to take good shots 8 out of 10 times. I will admit that I prefer the Note’s (over)saturated AMOLED screen, but the iPhone’s IPS LCD is pretty darn good too. And Apple’s DAC makes the Note sound laughable.

I could go on about this, but I’ve also got to recognize Android’s merits over iOS. I really do miss the customizability and openness of Android. I miss the crazy power that it had at times, like rooting and ROMming and even Samsung’s S Note features. Perhaps the HTC One M9 would be the closest competitor to the iPhone from the Android world. I love HTC’s design for the One series, Sense 7 is beautiful most times, if not all, and it’s the only one with audio capabilities rivaling iPhones.

Maybe in a world where IB didn't take up all my time, I would've still stuck with Android. But at this point in my life, I’ve got a dedicated gaming system (PS4) and a Mac for productivity and Designing, leaving me with very few demands from my smartphone. The only prerequisite is that whatever it does, it should be able to do it perfectly, consistently. And at this moment, I don't see anything apart from an iPhone fulfilling my needs.

Maybe in a few years, when it’s time for an upgrade, I’ll buy the latest Nexus or One or hopefully a project Ara device and wait for the waves of nostalgia from years of android use to come crashing back.


(This article is comprised entirely of the author’s opinions and points of view. Your mileage is likely to vary, so please refrain from taking offense eat any statement made.)