Thursday, February 14, 2013

Designing a Social Web Portal


Here's another novel idea of mine, "Designing a Social Web Portal"  that has made it to IP.com (intellectual property).
I have included below the full article in which the Web Portal is re-imagined by adding the social paradigm to a portal.

Abstract

The Social Web Portal, re-imagines the Web Portal using the social paradigm The Social Web Portal is common portal into which all users would login similar to Facebook, Google+ or Twitter. In the Social Web Portal users can choose their family, friends, acquaintances, professional colleagues. Once users are registered in the portal, the Social Web Portal will analyze the click stream history of all the registered users and display the relevant links for each user based on the user's social circle. Hence in the Social Web Portal each user will get an instantaneous update of relevant,trending URLs/newsitems of his/her social circle based on the click stream data of the social circle in addition to articles of personal interest. Such a portal becomes important in this age of exploding information. The user is completely abreast of all topics of interest of his immediate social circle and the world at large.
Introduction
A large part of our lives is spent on the net. We browse the web for news, stock prices, technology tends, sports updates etc. To do this we typically go to our favorite web sites which are either news aggregators or news curators or we search the web for the required information. This article describes a completely new web browsing experience that is based on the social networking paradigm. This article describes a web portal where the content displayed is based on the browsing preferences of the user, the user’s friends circle, the user’s professional network and the world at large. So the Social Web Portal will display content that is based on the user own preferences, the collective browsing click streams of his/her social network, the user’s professional friends and the world at large. Such a web portal will give the user a snapshot of the kind of news articles that will be of great interest to
him/her. The inclusion of the social paradigm to web browsing provides the user a web browsing experience that is most closely tailored to the user's taste.
Summary
Web portals like Lycos, Alta-vista, Yahoo, and Excite had their day in the sun early 1990’s. However all this changed with the entry of Google. It had a webpage with a single search bar. With a single stroke Google pushed all the portals to virtual oblivion.
It became obvious to the user that all information was just a “search away”. But much has changed since then. Many pages have been uploaded into the trillion servers that make up the internet. There is so much more information in the worldwide web. News articles, wikis, blogs, tweets, webinars, podcasts, photos, you tube content, social networks etc.
The internet now contains 8.11+ billion pages has more than 1.97 billion users, 266 million websites. We can expect the size to keep growing as the rate of information generation and our thirst for information keeps increasing.
In this world of exploding information the “humble search” will no longer be sufficient. As a user we would like to browse the web in a much more efficient, effective and personalized way. Neither will site aggregators like StumbleUpon, Digg, Reddit or sites which are news curators will be useful. We need to have a smart way to be able to navigate through this information deluge that is personalized to our tastes and to our social circle’s tastes.
We have now entered into an era of social networking where we keep contact with friends on social sites like Facebook, Google+ and with our professional network on LinkedIn and with the world at large on sites like Twitter and Pinterest. These social web sites deliver content based on our connections or our network.
The social web portal delivers content to the portal based on the user’s social network and the user's social network's browsing tendencies. It is in this context that it makes great sense to deliver a web portal experience that is based on the user’s personal, family, friend, professional and world browsing
preferences.
Description
Can a Web Portal render content to a single page with topics & news items based on the user's social circle centered on the user?
The Social Web Portal discusses such a portal for the user which renders content dynamically based on the click stream of the user’s social network. The Social Web Portal will deliver content that has the user’s browsing preferences as the focal center while also displaying the browsing trends of the user’s family, the user’s close friends, the user’s professional colleagues and associates. Finally the portal would also include inputs from what the world at large is interested in and following. The web portal would analyze the key user’s preferences and then create a web portal based on its analysis of
what the user would like to see.
Social Web Portal - Fundamental concept and premise
The Social Web Portal is not a personalized home page in which RSS feeds or inputs from feed aggregators or site aggregators are taken. The Social Web Portal is common portal into which all users would log into similar to Facebook, Google+ or Twitter. All users can choose their friends, acquaintances, professional colleagues in this portal. Once users are registered the click stream history of the all the registered users are continuously updated by the Social Web Portal to a back-end database. Then based on each individual's social circle the Social Web Portal will perform a statistical analysis of those URLs which were more relevant in the user's social circle and display these URLs in the user's page on the Social Web Portal. So when the user logs into the Social Web Portal the webpage will be personalized based on the user's individual preferences and the collective browsing history of the user's social circle (friends, colleagues,acquaintances etc).
The Social Web Portal does not take any feeds from existing social networking web sites like Facebook, Google+, Twitter or Youtube. It is independent of these sites. It does not aggregate the feeds from these sites nor does it depend on the social signals from these sites.
The Social Web Portal will generate 'social signals" independently and completely based on the user's social circle and the collective browsing history of the user's social network.
Also the “Social Web Portal” is fundamentally different from link aggregators or a feed aggregators. As mentioned above the Social Web Portal will be based on a statistical analysis of the user network's browsing history. So regardless of whether a user manually updates a Facebook/Google status, or a user submits a link to a link aggregating web site, the Social Web Portal will analyze the browsing history of the user’s social network and render the portal with the most browsed content.
The collective click stream of the user’s social circle will be analyzed statistically and the sites that have been most visited based on the user’s social circle will be displayed. Hence the user will be aware of the topics of interest of his/her social circle.
The major difference that the Social Web portal has with respect to link aggregators or feed aggregators mentioned above is that the Social Web Portal does not rely on either links submitted by the user’s social circle nor does it depend on the status updates of the user’s network.
The Web page rendered by the Social Web Portal will be based on a statistical ranking of the browsing history of the user’s social circle and also on the relative importance of the friends in the user’s social circle.
Detailed description
The Social Web Portal is based on the collective click stream activity of a user’s family, friends, professional circle and the world at large. This web portal will required to be signed on like any of the social network sites like FB, Google+ or Twitter. The web portal will have a window on the top right corner where the user can send invites, connection requests to his family members, friends and his professional colleagues. The click streams of all those who accept the user’s invite will be used to provide the web browsing experience for the user.
The user can also assign a degree of importance to each of his associations. So while a typical social network site like FB, Google+ or Twitter will provide the status updates of the connections of the user to the user’s updates and include the user’s updates in the connection’s updates, the Social Web Portal will keep track of the click streams of the all the users who have signed into the Social Web Portal. The browsing history of all the users who are registered in the Social Web Portal will be sent to a back-end database for subsequent processing and displaying in the appropriate social circle. Hence as the registered users travel from site to site their browsing history is captured and sent to the back-end database. The click stream history of all the registered users will be continuously updated to a back-end database. It will then render content to each of the individuals in the Social Web Portal
based on the network of that particular user.
The back-end database will be a repository of the browsing click streams of all the users who have signed up for the Social Web Portal. The browsing history of all registered users will be captured and sent to a back-end database, probably using cookies, on a regular basis. These cookies will be analyzed statistically by an application layer over the database which will then display content to a user based on the browsing history of the user's social circle. Each association in the social circle will be ranked based on a degree of importance assigned by the user.
When a user opens the Social Web Portal the portal will query the back-end database based on the social network that the user has and the degree of importance that the user has for each of his/her connections.
The query will return the overall browsing preferences that are based on the user’s network i.e. the Social Web Portal will render the web page with the aggregate, collective web browsing tendencies of the user’s family, friends, colleagues and friends besides including the user’s own tastes and browsing preferences. So every user will be aware of the common trends and popular items in his/her social circle along with the trending topics in the world at large.
This can be represented in the diagram below
win
Fig 1. Dynamic Window in the Social Web Portal
The rectangle shown in the above window is something that can be tuned by each user for his/ her individual taste. The user can specify how much of the browsing tendencies of friends, family and colleagues he or she would like to include in the Social Web Portal. Based on the user’s taste the content that will be displayed on the user’s Social Web Portal will have appropriate content of the user’s family, friend, colleagues and World
The Social Web Portal for a user can be visualized to be represented as shown below
portal
Fig 2. Snapshot of what each user would see when he logs into the Social Web Portal
As can be seen this Web page will be customized to the user. It will display all the relevant news items and articles of interest for the user. Any user will also be interested to see what people in his/her particular domain are reading. For e.g. a person in finance would like to see specific topics in finance while also being interested in the other relevant news items that he may have missed but may have been read by his/her friends or colleagues.
In other words each user will get a snapshot of information. This information will be tailored to the user based on the individual's personal preferences, the trending topics in among his family, friends, colleagues, acquaintances and the world at large. So every user will be fully abreast of the popular topics issues in the world without having to individually browse sites. The above figure shows how this snapshot would look for each user.
People also typically like to see if they are up to date with the world on topics. The Social Web Portal will ensure that popular articles automatically bubble up to each and every user.
A diagrammatic representation of the Social Web Portal in action can be represented as below
data
Fig 3. Browsing history maintained in a back-end database and displayed for each user.
In the above figure the click streams of the network of all the users of the Social Web Portal are collected in the distributed database. When a user logs into his Social Web Portal the query will return the overall browsing trends of the user’s family, friends, professional colleagues and the world. Those news items that are popular will be bubbled up to the user along with his or her own preferences. Hence the user will feel connected to his/her network and will have a novel browsing experience.
A diagrammatic representation of the Social Web Portal is shown below
swp1
Fig 4. A schematic of how the personalization happens in the Social Web Portal
In Fig 4 it can be seen that the bottom most layer contains the collective browsing history of all the registered users as they browse different web sites. This click stream will be updated at regular intervals. This browsing history is analyzed statistically to determine the most relevant and popular sites for each user's social network and then ranked on the degree of importance of each individual in the social circle.
fc
Fig 5. Flow chart for the Social Web Portal
Hence the Social Web Portal will broadly perform the following activities
  • The collective browsing history of all registered users of the Social Web Portal will be sent for analysis to a back-end database
  • The Social Web Portal will render content based on the statistical analysis of the collective click stream activity of a user’s family, friends, professional circle and the world at large
  • The Social Web Portal will render content dynamically based on the statistical ranking of browsing history of user’s social circle
  • A user can configure the order of importance to each of the people in his/her social circle. The Social Web Portal the portal will query the back-end database based on the relative importance of each of the acquaintance of the user and also the statistical weight of “visited sites”
  • The Social Web Portal will render the web page with the “most visited sites” based on the aggregate, collective web browsing tendencies of the user’s family, friends, colleagues and friends besides including the user’s own tastes and browsing preferences.
Benefits
The Social Web Portal will usher in a completely new Web browsing experience. Adding the social paradigm to a user’s browsing experience can have multiple benefits. It will allow each user to know what new articles or items are popular among his or her network. A person can keep abreast of all the trends that are of interest to him/her. The Social Web Portal will be novel experience that will be completely tailored to each and every user.

Tuesday, February 5, 2013

A method for optimal bandwidth usage by auctioning available bandwidth using the OpenFlow protocol


Here is a recent idea of mine that has made it to IP.com (Intellectual Property.com). See link
In this article I provide some more details to my earlier post - Towards an auction-based internet.
Abstract:
As the data that traverses the internet continues to explode exponentially the issue of a huge bandwidth crunch will be a distinct possibility in the not too distant future. This invention describes a novel technique for auctioning the available bandwidth to users based on bid price, quality of service expected and the type of traffic. The method suggested in this invention is to use the OpenFlow protocol to dynamically allocate bandwidth to users for different flows over a virtualized network infrastructure.
Introduction:
Powerful smartphones, bandwidth-hungry applications, content-rich applications, and increasing user awareness, have together resulted in a virtual explosion of mobile broadband and data usage. There are 2 key drivers behind this phenomenal growth in mobile data. One is the explosion of devices viz. smartphones, tablet PCs, e-readers, laptops with wireless access. The second is video. Over 30% of overall mobile data traffic is video streaming, which is extremely bandwidth hungry. Besides these, new technologies like the “Internet of Things” & “Smart Grids” now have millions and millions of sensors and actuators connected to the internet and contending for scarce bandwidth. In other words there is an enormous data overload happening in the networks of today.
Two key issues of today’s computing infrastructure deal with data latency and the economics of data transfer. Jim Gray (Turing award in 1998) in his paper on “Distributed Computing Economics” tells us that the economics of today’s computing depends on four factors namely computation, networking, database storage and database access. He then equates $1 as follows
One dollar equates to
= 1 $
≈ 1 GB sent over the WAN
≈ 10 Tops (tera CPU operations)
≈ 8 hours of CPU time
≈ 1 GB disk space
≈ 10 M database accesses
≈ 10 TB of disk bandwidth
≈ 10 TB of LAN bandwidth
As can be seen from above breakup, there is a disproportionate contribution by the WAN bandwidth in comparison to the others. In others words while the processing power of CPUs and the storage capacities have multiplied accompanied by dropping prices, the cost of bandwidth has been high. Moreover the available bandwidth is insufficient to handle the explosion of data traffic.
It is claimed that the “cheapest and fastest way to move a Terabyte cross country is sneakernet (i.e. the transfer of electronic information, especially computer files, by physically carrying removable media such as magnetic tape, compact discs, DVDs, USB flash drives, or external drives from one computer to another).
While there has been a tremendous advancement in CPU processing power (CPU horsepower in the range of petaflops) and enormous increases in storage capacity(of the order of petabytes) coupled with dropping prices, there has been no corresponding drop in bandwidth prices in relation to the bandwidth capacity.
It is in this context an auction-based internet makes eminent sense. An auction-based internet would be a business model in which bandwidth would be allocated to different data traffic on the internet based on dynamic bidding by different network elements. Such an approach becomes imperative while considering the economics and latencies involved in data transfer and the emergence of the promising technology known as the OpenFlow protocol. This is further elaborated below
Description
As mentioned in Jim Turing’s paper a key issue that we are going to face in the future has to do with the economics of data transfer and the associated WAN latencies
As can be seen there are 3 distinct issues with the current state of technology
1) There is an exponential increase in data traffic circling the internet. According to a Cisco report the projected increase in data traffic between 2014 and 2015 is of the order of 200 exabytes (10^18)).The internet is thus clogged due to the many bandwidth hungry applications and millions of devices that make the internet
2) WAN latencies and the economics of data transfers are two key issues of the net
3) Service Providers have not found a good way to monetize this data explosion.
Clearly bandwidth is a resource that needs to be utilized judiciously given that there are several contenders for the usage of bandwidth.
Detailed description: This invention suggests a scheme by which internet bandwidth can be auctioned between users based on their bid price, Quality of Service (QoS) required and the type of traffic (video, voice, data, streaming). The energy utility already auctions electricity to the highest bidder. This invention suggests a similar approach to auction scarce bandwidth to competing bidders.
The internet pipes get crowded at different periods of the day, during seasons and during popular sporting events. This invention suggests the need for an intelligent network to price data transfer rates differently depending on the time of the day, the type of traffic and the quality of service required. In this scheme of things the internet will be based on an auction mechanism in which different devices bid for scarce bandwidth based on the urgency, speed and quality of services required.
Such a network can be realized today provided the network and the network elements that constitute the internet implement the OpenFlow protocol.
Software Defined Networks (SDNs) is the new, path breaking innovation in which network traffic can be controlled programmatically through the use of the OpenFlow protocol. SDN is the result of pioneering effort by Stanford University and University of California, Berkeley and is based on the Open Flow Protocol and represents a paradigm shift to the way networking elements operate.
SDNs can be made to dynamically route traffic flows based on decisions in real time. The flow of data packets through the network can be controlled in a programmatic manner through the OpenFlow protocol. In order to dynamically allocate smaller or fatter pipes for different flows, it necessary for the logic in the Flow Controller to be updated dynamically based on the bid price, QoS parameters and the traffic type.
The OpenFlow protocol has a Flow Controller element which can be made to create different flows by manipulating the flow tables of the different network elements. Hence the Flow Controller depending on the bid price, the bandwidth rate and the QoS will auction the different bids and create different flows for different users. The Flow Controller will then update the flow tables of the network elements that will participate to realize this end-to-end flow of traffic for different users.
A typical scenario can be visualized as below
pat1
In the above figure different users bid for available bandwidth. For e.g. User A could bid for A Mbps @ $a/bit for traffic type A, User B could bid for B Mbps @ $b/bit for traffic type B and User C could bid for C Mbps @ $c/bit for traffic type C. The different QoS parameters like delay, throughput, and jitter are all sent in the user requests. The Flow controller receives all these bids with associated parameters and auctions the available bandwidth against the bid prices that the network elements bid for. The Flow Controller then ranks the bids against the most optimal bandwidth allocation that has the highest return.
The Flow Controller can then allocate different bandwidths to the different users based on the bids from the highest to the lowest, quality of service and the type of traffic. Software Defined Networks (SDNs) can then create different flows for across the networks. SDN can create different slices of network elements from end-to–end for each of the different flow requirements.
The Flow Controller can then create these flows and update the flow tables of the network elements based on the allotted speeds for the bid price.
This is shown diagrammatically below
pat2

For e.g. we could assume that a corporate has 3 different flows namely, Immediate, ASAP (As soon as possible) and  price below $x. Based on the upper ceiling for the bid price, the OpenFlow controller will allocate a flow for the immediate traffic of the corporation. For the ASAP flow, the corporate would have requested that the flow be arranged when the bid price falls between a range $a – $b. The OpenFlow Controller will ensure that it can arrange for such a flow. The last type of traffic will be allotted a default flow during non-peak hours. This will require that the OpenFlow controller be able to allocate different flows dynamically based on winning the auction process that happens in this scheme.
Using the OpenFlow paradigm to auction bandwidth
These will be the typical steps that will occur during
  1. Let us assume that it is the period of the day when the usage is at its peak
  2. Let there be 3 users User A, User B and User C who would like to video-conference, video stream and make a voice call respectively
  3. Depending on the urgency and the price that the users can afford these 3 users will bid for a slice of a bandwidth to complete their call
  4. Let user A request A Mbps @ $a/bit for QoS parameters p(a). Let user B request B Mbps @ $b/bit for QoS parameters p(b) and user C request C Mbps @ $c/bit for QoS parameters p(c).
  5. When the Flow Controller receives these requests, based on the available bandwidth at its disposal (assuming it has already used X Mbps for already existing flows) it will normalize these requests and auction them so that it results in the highest bid winning its requested bandwidth slice followed by the ones lower than it. If a user does not qualify the auction the user will have a bid at a later time according to some algorithm. Let us assume that user A and user C win their bids
  6. The Flow Controller will now algorithmically decide the contents of the flow tables of the intervening network elements and will accordingly populate these flow tables
  7. The flows for User A and User C are now in progress.
  8. The Flow Controller will accept bids whenever there is spare bandwidth that can be put up for auction.
As can be seen such a mechanism will result in a varying price for bandwidth with the highest value during peak periods and lower values during off-peak periods.
Benefits: The current protocols of the internet of today namely IntServ, DiffServ allocate pipes based on the traffic type & class which is static once allocated. This strategy enables OpenFlow to dynamically adjust the traffic flows based on the current bid price prevailing in that part of the network. Moreover the usage of OpenFlow protocol can generate a lot more granualar flow types.
The ability of the OpenFlow protocol to be able to dynamically allocate different flows will once and for all solve the problem of being able to monetize mobile and fixed line data This will be a win-win for both the Service Providers and the consumer. The Service Provider will be able to get a ROI for the infrastructure based on the traffic flowing through his network. Users can decide the type of service they are interested and choose appropriately. The consumer rather than paying a fixed access charge could have a smaller charge because of low bandwidth usage.
Conclusion: An auction-based internet is a worthwhile business model to pursue. The ability to route traffic dynamically based on an auction mechanism in the internet enables the internet infrastructure to be utilized optimally. It will serve the dual purpose of solving traffic congestion, as highest bidders will get the pipe but will also monetize data traffic based on its importance to the end user.

Monday, January 28, 2013

The computer is not a dumb machine!


computer“The computer is a dumb machine. It needs to be told what to do at every step”. How often have we heard of this refrain from friends and those who have only an incidental interaction with computers?  To them a computer is like a ball which has to be kicked from place to place. Only those who are ignorant or have a fear of computers would make such a statement. However this is so far from the truth.  In this post, my 100th, I come to the defense of the computer in a slightly philosophical way.
The computer is truly a marvel of technology. The computer truly embodies untapped intelligence. In my opinion even a safety pin is frozen intelligence. From a piece of metal the safety pin can now hold things together while pinning them, besides incorporating an aspect of safety.
Stating that the computer is a dumb machine is like saying that a television is dumb and an airplane is dumber.  An airplane probably represents a modern miracle in which the laws of flight are built into every nut and bolt that goes into the plane. The electronics and the controls enable it to lift off, fly and land with precision are work in tandem and perform a daily miracle.
Similarly a computer from the bare hardware to the upper most layer of software is nothing but layer and layer of human ingenuity, creativity and innovation. At the bare metal the hardware of the computer is made up integrated chips that work at the rate of 1 billion instructions per second. The circuits are organized so precisely that they are able to work together and produce a coherent output, all at blazing speeds of less than a billionth of a second.
computer3
On top of the bare bones hardware we have some programs that work at the assembly and machine code made of 0’s and 1’s. The machine code is nothing more but amorphous strings of 0’s and 1’s. At this level   the thing that is worked on (object) and the thing that works on it (subject) are indistinguishable. There is no subject and object at this level. What distinguishes then is the context.
Over this layer we have the Operating System (OS) which I would like to refer to as the mind of the computer. The OS is managing many things all at once much like the mind has complete control over sense organs which receive external input. So the OS manages processes, memory, devices and CPU (resources)
As humans, we like to pride ourselves that we have consciousness. Rather than going into any metaphysical discussion on what consciousness is or isn’t it is clear that the OS keeps the computer completely conscious of the state of all its resources.  So just like we react to data received through our sense organs the computer reacts to input received through devices (mouse, keyboard) or its memory etc. So does the computer have consciousness?
You say human beings are capable of thought. So what is thought but some sensible evaluation of known concepts? In a way the OS is also constant churning in the background trying to make sense of the state of the CPU, the memory or the disk.
Not to give I can hear you say “But human beings understand choice”. Really! So here is my program for a human being
If human_being provoked
Get angry
If human_being insulted
Get hurt
If human_being ego stoked
Go mad with joy
Just kidding! Anyway the recent advances in cognitive computing now show it is possible to have computers choose the best alternative. IBM’s Watson is capable of evaluation alternative choices.
Over the OS we have compilers and above that we have several applications.
The computer truly represents layers and layers of solidified human thought. Whether it is the precise hardware circuitry, the OS, compilers, or any application they are all result of human thought and they are constantly working in the computer.
So if your initial attempt to perform something useful did not quite work out, you must understand that you are working with decades of human thought embodied in the computer. So your instructions should be precise and logical. Otherwise your attempts will be thwarted.
computer1
So whether it’s the computer, the mobile or your car, we should look and appreciate the deep beauty that resides in these modern conveniences, gadgets or machinery.

Wednesday, January 16, 2013

Blob with an attitude (stiffness) in Android


DSC00044This post is an enhanced version of my earlier blob post Creating a blob in Android using Box2D physics engine and AndEngine. To introduce stiffness to the overall blob structure I used revoluteJoint between adjacent bodies as follows
}
// Create a revoluteJoint between adjacent bodies - Lacks stiffness
forint i = 1; i < nBodies; i++ ) {
final RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
revoluteJointDef.initialize(circleBody[i], circleBody[i-1], circleBody[i].getWorldCenter());
revoluteJointDef.enableMotor = false;
revoluteJointDef.motorSpeed = 0;
revoluteJointDef.maxMotorTorque = 0;
this.mPhysicsWorld.createJoint(revoluteJointDef);
}
// Create a revolute joint between first and last bodies
final RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
revoluteJointDef.initialize(circleBody[0], circleBody[19], circleBody[0].getWorldCenter());
revoluteJointDef.enableMotor = false;
revoluteJointDef.motorSpeed = 0;
revoluteJointDef.maxMotorTorque = 0;
this.mPhysicsWorld.createJoint(revoluteJointDef);
The motorSpeed, maxMotorTorque is set to 0 and the enableMotor is set to false. However I found that this joint still lacks stiffness.
So I replaced the revoluteJoint with the weldJoint which is probably more appropriate
// Create a weldJoint between adjacent bodies - Weld Joint has more stiffness
forint i = 1; i < nBodies; i++ ) {
final WeldJointDef weldJointDef = new WeldJointDef();
weldJointDef.initialize(circleBody[i], circleBody[i-1], circleBody[i].getWorldCenter());
this.mPhysicsWorld.createJoint(weldJointDef);
}
// Create a weld joint between first and last bodies
final WeldJointDef weldJointDef = new WeldJointDef();
weldJointDef.initialize(circleBody[0], circleBody[19], circleBody[0].getWorldCenter());
this.mPhysicsWorld.createJoint(weldJointDef);
Here are clips of the the Blob with more attitude
You can clone the project from Github at Blob_v1

Tuesday, January 15, 2013

Creating a Blob in Android using Box2D physics engine & AndEngine


DSC00037Here is a short post on my attempt to create a Blob using Box2D physics engine and AndEngine. This demo tries to recreate the Blob Joint at GwtBox2D Showcase. This Blob Joint demo in Java uses a ConstantVolume Joint for creating the Blob. For my blob I use a distanceJoint for maintaining the shape of the Blob.
A Blob is created in the initial shape of an ellipse as follows
// Add 20 circle bodies around an ellipse
for (int i=0; iFIXTURE_DEF = PhysicsFactory.createFixtureDef(30f, 0.5f, 0.5f)
Vector2 v1 = new Vector2(x1,y1);
final VertexBufferObjectManager vb = this.getVertexBufferObjectManager();
circle[i] = new AnimatedSprite(x1, y1, this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager());
circleBody[i] = PhysicsFactory.createCircleBody(this.mPhysicsWorld, circle[i], BodyType.DynamicBody, FIXTURE_DEF);
...
}
A distance Joint is created between every body as follows
// Create a distanceJoint between every other day
for(int i= 0;i < nBodies-1; i++) {
for(int j=i+1; j 0) {
connectionLine[i] = new Line(centers[i][0],centers[i][1],centers[i-1][0],centers[i-1][1],lineWidth,this.getVertexBufferObjectManager());
connectionLine[i].setColor(0.0f,0.0f,1.0f);
this.mScene.attachChild(connectionLine[i]);
}
// Join the first body with the last body
if(i == 19){
connectionLine[0] = new Line(centers[0][0],centers[0][1],centers[19][0],centers[19][1],lineWidth,this.getVertexBufferObjectManager());
connectionLine[0].setColor(.0f,.0f,1.0f);
this.mScene.attachChild(connectionLine[0]);
}
The connecting lines move along with the moving shapes as below
// Update connection line so that the line moves along with the body
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(circle[i], circleBody[i], true, true) {
@Override
public void onUpdate(final float pSecondsElapsed) {
super.onUpdate(pSecondsElapsed);
for(int i=1;i < nBodies;i++) {
connectionLine[i].setPosition(circle[i].getX(),circle[i].getY(),circle[i-1].getX(),circle[i-1].getY());
} connectionLine[0].setPosition(circle[0].getX(),circle[0].getY(),circle[19].getX(),circle[19].getY());
}
}
);
So here is the clip of the blob in action : Blob clip
You can clone the project from Github from the Blob code

Friday, January 11, 2013

Bull in a china shop – Behind the scenes in Android


DSC00032Here are the details about how I constructed the “Bull in a china shop” demo. For this demo I used Box2D physics engine and AndEngine to make the demo. I decided to use sprites for the china shop and picked up images of glasses, wine glasses, bottles etc from www.openclipart.org.
Be extremely careful when creating the TextureRegion using BitmapTextureAtlas. If you don't get the co-ordinates right the display can be weird.
Here is a snippet of this
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 556, 246, TextureOptions.BILINEAR);
this.mTumblerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlasthis"tumblr.png", 0, 0);
this.mBitmapTextureAtlas.load();
this.mBottleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlasthis"bottle.png",20, 29);
this.mBitmapTextureAtlas.load();
this.mGlassTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlasthis"glass.png",36, 69);
this.mBitmapTextureAtlas.load();
this.mVaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlasthis"vase.png",56, 89);
this.mBitmapTextureAtlas.load();
...
...
This is very important to get right otherwise you are setting yourself for a lot of grief.
Superficially the demo looks real easy. It appears that creating a pyramid stack should be a breeze as long as you get the coordinates right. Wrong! Building a pyramid using Box2D with the effect of gravity can be a real challenge as I found out. I would build two stacks and the stack would become unstable and collapse.
Anyway here are the findings
  1. Each row is not placed directly over the object below. I leave a gap of 2 px between them The reason is the object below exerts a force 'F' upward. The object above exerts a force 'mg” below and the physics engine tries to resolve this difference in forces and causes instability in the structure. So the key is a to leave a small gap in between
  2. Now that there is a gap between 2 rows the coefficient of restitution 'e' is made 0. Even a value as small as 0.1f can make the objects jitter and cause instability.
  3. The friction between the platform and the objects or the objects themselves is made maximum equal to 1.0f to prevent sliding of the objects.
// Add tumblers
for(int i=0; i < 21; i++) {
tumbler = new Sprite(80 + i * 25, 450, this.mTumblerTextureRegionthis.getVertexBufferObjectManager());
FIXTURE_DEF = PhysicsFactory.createFixtureDef(1f, 0.0f, 1f);
tumblerBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, tumbler, BodyType.DynamicBodyFIXTURE_DEF);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(tumbler, tumblerBody, truetrue));
this.mScene.attachChild(tumbler);
}
// Add glasses
for(int i=0; i < 14; i++) {
glass = new Sprite(130 + i * 25, 428, this.mGlassTextureRegionthis.getVertexBufferObjectManager());
FIXTURE_DEF = PhysicsFactory.createFixtureDef(1f, 0.0f, 1f);
glassBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, glass, BodyType.DynamicBodyFIXTURE_DEF);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(glass, glassBody, truetrue));
this.mScene.attachChild(glass);
}
...
...
Hopefully if you get everything right you should have a stable structure. I had to do this through trial and error before I got it finally right. Whew!
Once you get a stable structure with the proper sprites in place most of the problem is solved. For the last part I add a bull sprite and set it off at a velocity from the point of touch.
bull = new Sprite(pX, pY, this.mBullTextureRegionthis.getVertexBufferObjectManager());
FIXTURE_DEF = PhysicsFactory.createFixtureDef(25f, 0.0f, 1f);
Log.d("here","here");
bullBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, bull, BodyType.DynamicBodyFIXTURE_DEF);
if(pX > 360)
bullBody.setLinearVelocity(-5,-5);
else
bullBody.setLinearVelocity(5,5);
DSC00031
Note: Since the sprites are not regular shapes I had to use a box shape. So the collisions are not pixel perfect.
Make sure you set up your project properly in Eclipse. The important settings are
  1. Project->Properties->Android->Android4.2
  2. Project->Properties->Java Compiler: Check the “Enable project specific setting” and also set compiler compliance level to 1.6
  3. Finally click Project->Properties->Android: Under Library click 'Add' and add AndEngine and AndEnginePhysicsBox2DExtension
and you are good to go.
Here are 2 clips of Bull in a China Shop demo
You can clone the project from Github from Bulldozed
Have fun...