Wednesday, July 26, 2017

Getting Started With Matter.js: The Body Module

In the previous tutorial of the series, you learned about the World and Engine modules in Matter.js. The methods available in these two modules are meant to control the behavior of multiple bodies or the whole world at once. However, at some point it will become necessary to have control over the properties of individual bodies in your world. 

For example, you may want to apply some force on a particular object or change its friction coefficient. In such cases, the Body module in Matter.js can be of great help. This module contains a lot of methods and properties to let you specify values for all kinds of physical attributes, from mass to coefficient of restitution. In this tutorial, you will learn about all these methods and properties and how to use them properly.

Scaling, Rotating and Translating a Body

You can rotate any rigid body in the Matter.js world by using the rotate(body, rotation) method. The rotation is relative to the current angle of the body, and it will not impart any angular velocity to it. The rotation angle is specified in radians.

You can also scale a body by using the scale(body, scaleX, scaleY, [point]) method. The parameters scaleX and scaleY specify the amount of scaling in the horizontal and vertical directions respectively. Keep in mind that any such scaling will also update the physical properties of the body like its mass, area, and inertia. The fourth parameter specifies the point around which the scaling occurs. When not specified, the default value of the scaling point is assumed to be the center of the body.

It is possible to move a body by a given vector relative to its current position using the translate(body, translation) method. The translation parameter specifies the new position of the object relative to its current position. Here is the portion of code from the demo that scales, rotates and moves the box around.

Setting Velocities and Applying Forces

You can also impart linear velocity to an object using the setVelocity(body, velocity) method. Applying a velocity in this manner does not change the angle, applied force or position of the concerned object. The position of the object or its angle will probably change, but the library does not specifically set them to any value. Their values are determined by other forces that are acting on the object, like friction. 

Just like the linear velocity, you can also change the angular velocity of an object using the setAngularVelocity(body, velocity) method. In this case, too, the position, angle and force applied on the object remain unchanged.

One thing that you should keep in mind is that the velocity is a vector in setVelocity() and a number in setAngularVelocity().

In addition to imparting velocities to objects, you can also apply a force vector on them. The applyForce(body, position, force) method can be used to apply a force vector on a body from a given position. This force may or may not result in the application of torque on a given body. 

The following code applies a force right at the center of the body. The force vector is {x: 0, y: -0.05}. This means that the applied force will be purely vertical and in an upward direction. You should keep in mind that vertical forces in an upward direction have a negative sign in Matter.js. Another thing worth noticing is how small the number that specifies the vertical force is. The gravitation force itself has a value of just 1 in Matter.js.

The motion of the ball after applying the forces seems natural as long as that ball does not collide with any of the walls or the floor. Normally, when things collide with something, we expect them to bounce back. The energy with which an object bounces back is determined by the coefficient of restitution. 

In Matter.js, its value is set to zero by default. This means that any object which has restitution set to zero and collides with something else will not bounce back at all. A value of 1 will mean that the object will bounce back with kinetic energy equal to what it had before collision. A value like 0.5 means that the object will bounce back only with 50% of its previous kinetic energy. The value of restitution for an object can be controlled using the restitution key.

In certain simulations, it might become necessary for you to change the friction between different bodies. This can be achieved using the friction, frictionAir and frictionStatic keys.

  • The friction key specifies the value of kinetic friction for a body. It can have a value between 0 and 1. A value of 0 implies that a body may keep moving indefinitely once it has been set in motion. The only way to stop it will be to apply some other external force. The final value of friction between two objects is determined using the formula Math.min(bodyA.friction, bodyB.friction)
  • The frictionStatic key specifies the value of friction when a body is at rest. The default value for static friction is 0.5. A higher value means that a larger amount of force will be required to get the body moving.
  • The frictionAir key is used to specify the value of friction between a body and the surrounding air. A higher value means that the body will slow down very quickly when moving through the air. The effect of air friction is non-linear.

Control the Rendering of Bodies

Up to this point, we have not specified the color, outline width or stroke style to use when rendering a body. All these properties are nested inside the render key. The fillStyle property accepts a string to specify the fill style rendering the body. The lineWidth property accepts a number that defines the line width to use when creating the outline of a body. 

A value of zero means that no line will be rendered at all. The strokeStyle property can be used to specify the stroke style to use when rendering the body outline. You can prevent a body from rendering at all by setting the visible key to false. The opacity of the body that you want to render can be controlled using the opacity key.

You can also use an image instead of simple colors and outlines to render a body. The parameters for rendering a body using sprites are specified using a different set of properties. The texture property defines the path of the image that should be used as a sprite texture. 

The xOffset and yOffset properties can be used to define the offset in the respective axes for the sprite. Similarly, you can use the xScale and yScale properties to define the scaling in the x-axis and y-axis for the sprite. Here is some code that replaces the light blue background of our ball with a soccer sprite from the Open Game Art website.

Changing Physical Properties

You have already seen how to specify the friction or coefficient of restitution for an object in Matter.js. There are a lot of other properties whose values can be specified in the same manner. On the other hand, there are properties which are read-only and cannot be changed by you.

You can set the position of a body in the world by using the position key, which accepts a vector as its value. You can also specify the mass of a body using the mass property, but then you will also have to set a value for the inverseMass property, which is calculated using 1/mass. A better way of controlling the mass of a body is with the help of the density property. 

Once you change the density of a body, its mass will be calculated automatically based on its area. This way you can also differentiate between different objects based on their density. For example, a body that uses a rock as its sprite should have higher density than a body of the same size that uses a soccer ball as its sprite.

Some properties like speed, velocity and angularVelocity are read-only, but their values can be set using appropriate methods like setAngularVelocity() and setVelocity(). You can read more about different properties of the Body module in the documentation.

Conclusion

In this tutorial, you have learned about all the important methods and properties in the Body module of the Matter.js library. Knowing about these different properties and what they do can help you create more realistic simulations of real-life physics. In the next and final tutorial of the series, you will learn about the Composite module in Matter.js.

If you have any questions related to this tutorial or some tips for using the library, please share them with us.


by Monty Shokeen via Envato Tuts+ Code

BASIC Brandbeats

Brandbeats, is an agency podcast by BASIC. The new website provides more information around each episode, including a full summary, list of guest speakers, and interactive show notes.
by via Awwwards - Sites of the day

#168: Progressive Web Apps Are A Toolkit, Not A Recipe

Mobile Web Weekly July 26, 2017   #168
Brian Rinaldi recommends
Enhancing CSS Layout: From Floats To Flexbox To Grid — Manuel Matuzović thinks Grid is “one of the most exciting developments since responsive design” adding that we should “get the best out of it as soon as possible”.
Smashing Magazine
Brian Rinaldi recommends
Can Cordova Fit Your Target Industry? — Looks at what types of apps the author believes are good candidates for Cordova development.
Anastasia Yaskevich
Sponsored
Set Up Real-Time Crash Reporting For Your Web and Mobile Apps — Join thousands of software development teams who discover and reproduce errors and crashes affecting their users with greater speed and accuracy. Learn more...
RAYGUN

Holly Schinsky recommends
We Now Have A Community-Approved Progressive Web Apps Logo — Maxim Salnikov initiated a call for a “community-approved” PWA logo last month. Now we have one.
Peter O'Shaughnessy
Holly Schinsky recommends
Progressive Web Apps Are A Toolkit, Not A Recipe — Many think of the term ‘Progressive Web App’ as a recipe of features that must be combined together but devs shouldn’t be looking at it as an all or nothing approach.
Peter O'Shaughnessy
Brian Rinaldi recommends
Using Viewport Units to Scale Fixed Layouts — Sebastian Eberlein explores ‘edge cases’ where scaling a fixed layout with viewport units can be a viable solution.
Hacker Noon
Holly Schinsky recommends
Ionic Dev Survey Says: There’s No Better Time to Be A Web Developer — The results from the first-ever Ionic Developer Survey, with input from more than 13,000 community members who shared what tools and technology they use, and what types of apps they’re building.
Ionic
Chris Brandrick recommends
Optimizing Performance of A-Frame Scenes for Mobile Devices — Using benchmarking to figure out the level of 3D complexity a mobile phone is capable of, and determining which performance metrics are required for such a benchmark.
Mozilla Hacks
Brian Rinaldi recommends
User Agent Parsing: How It Works and How It Can Be Used — An in-depth look at user agent parsing, the pros and cons, and is it useful for device detection.
Martin Clancy
Holly Schinsky recommends
A Beginner's Guide to HTML5 Cross-Browser Polyfills — Oliver Williams shows how polyfills let you write modern code that will work everywhere.
SitePoint
Peter Cooper recommends
Implementing AMP Forms from Start to Finish with amp-form — How to implement forms in AMP using practical examples such as submitting a search on a blog and collecting newsletter subscribers.
Paul Shapiro
Brian Rinaldi recommends
How to Use AMP with WordPress — Have you used Google’s Accelerated Mobile Pages to display your content to mobile audiences? Here’s some pointers on using AMP with WordPress.
SitePoint
Brian Rinaldi recommends
How To Implement In-App Ratings For iOS Ionic Apps
Ashteya Biharisingh
Holly Schinsky recommends
How to Get Started with Ionic Framework 3 on Mac and Windows — The first post in a series covering building hybrid apps for both iOS and Android with Ionic Framework 3.
Nikola Brežnjak
Holly Schinsky recommends
A Progressive Web Application with Vue.js, Webpack + Material Design — The third part in a series that aims to build a basic but complete Progressive Web Application with VueJs, Webpack and Material Design.
Sicara
Sponsored
Building a realtime feed with Node.js and AMP — How to develop a feed with pagination using the amp-live-list component.
Pusher

Holly Schinsky recommends
5 Ways to Ensure A Great UX for Progressive Web Apps — Do these five things to ensure that your PWA is performant, usable, and takes advantage of new web standards.
Poli Dey
Holly Schinsky recommends
ionic-twitter-pwa: mobile.twitter.com PWA Rewritten with Ionic — A sample app showing the mobile.twitter.com Progressive Web App rewritten with Ionic.
Julien Renaux


by via Mobile Web Weekly

How to Track Dark Social Traffic in Google Analytics

Are links to your content shared via private messages on social media? Wondering how to identify and measure that traffic? Dark social traffic comes from sources such as Facebook Messenger, Twitter DMs, and even email. Being able to accurately trace this traffic will give you a more complete picture of how your content is performing. [...]

This post How to Track Dark Social Traffic in Google Analytics first appeared on .
- Your Guide to the Social Media Jungle


by Rachel Moore via

BIRKENSTOCK

The brief was to give the BIRKENSTOCK online store a conceptual and visual overhaul that makes the brand emotionally resonant and communicates the BIRKENSTOCK values and product lines. The new online store was given a responsive design right across


by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery

h5Validate – HTML5 Form Validation Plugin for jQuery

h5Validate is an HTML5 form validation plugin for jQuery. It works on all major browsers, both new and old. Implements inline, realtime validation best practices.


by via jQuery-Plugins.net RSS Feed

Tuesday, July 25, 2017

18 Reasons Your Web-Based Business Should Be Using Gmail (infographic)

Email is a stayer. While new consumer messaging apps, team collaboration tools and social communication customs come and go, on a professional level email is as hardy as the traditional letter: it has a fundamental simplicity and versatility that makes it the bedrock from which these other...

[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Irfan Ahmad via Digital Information World