Created a video to help people get started with HexHoot

Image
A few days ago, I decided to make version release for HexHoot on GitHub, as I think it is important to differentiate something that is more stable and something that is under development. Having versions that people can download seem to be the best move to do the same. Following the same, I shared it with people over LinkedIn, Facebook and Instagram. One of the feedbacks I received over private messages was asking me to provide more information on what it exactly is and how to use it. Although all these information are written in the documentation and the website, it appears to be that people like to watch videos explaining the same. Therefore, in the past couple of days, I have been performing screen recording and doing voice overs. Interestingly, during this process, I found a couple of issues with the software, and I fixed those. The following is the video. Please do provide your feedback on this project. It would be lovely if any of you are interested in contributing to the same. Y

Programming bouncing ball in JavaScript

While studying Newton's laws of motion during my schooldays, I found some of the questions related to bouncing ball quite fascinating. The sheer number of different kinds of questions that one could come up with just a ball bouncing on the ground is quite a lot.

My attempt is to create the example as simple as possible. I would not be defining additional classes; I would be using the position value which is already present in the DOM Element and adding velocity as a parameter to the DOM Element.

First, let us define the HTML and CSS to get the visual appearance of the ball and the floor right.

<html>
<style>
#ball{
    position: absolute;
    left: 50%;
    top: 0;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: #005eff;
}
#floor{
    position: absolute;
    bottom: 2px;
    background: #000;
    width: 100%;
    height: 20px;
}
</style>
<body>
    <div id="ball"></div>
    <div id="floor"></div>
</body>
</html>
Now, let the fun begin! Let's start with gravity as a global variable (just in case I feel like adding more balls in the future).

Let us tackle the problem statement at hand in a discrete time way. Let us observe how smooth this is going to be. Adding the following JavaScript to the HTML/CSS shown above.

<script>
// Globals
var gravity = 0.01;
var deltaT = 1; // Updated at every time step
var restitutionCoefficient = 0.8; 
var ball = document.getElementById("ball");
var floor = document.getElementById("floor") 
ball.v = 0; // Initial velocity
setInterval(function(){
    ball.style.top = ball.offsetTop + ball.v * deltaT + 0.5 * gravity * deltaT * deltaT;
    ball.v = ball.v + gravity * deltaT;
    if(ball.offsetTop + ball.offsetHeight > floor.offsetTop){
        ball.v = - ball.v * restitutionCoefficient;
    }
}, deltaT);
</script>

Well, the ball does bounce with this, but it just does not look smooth for some reason. Well, let's try something else.

The issue may be caused by too much computation involved at such a short time step. First of all, the half accelleration times time squared term is insignificant. Let us remove that. After that, divide gravity by a factor of 10 and multiply deltaT by a factor of 10. Observe how things smoothen up.


If you understand this post, it is not very far fetched to think that you can create your own JavaScript game using a few keyboard callbacks. Enjoy!

Comments

Popular posts from this blog

Regarding a Covid-19 related project that I worked on a few months ago

P2P Zero-Knowledge-Proof based Opensource Social Network - HexHoot

Went into the University for the first time