<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.0">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2021-06-08T07:03:02+00:00</updated><id>/feed.xml</id><title type="html">My Physics Engine and Other Projects</title><subtitle>This is a site I've created using github pages and jekyll to to showcase some of my projects and code. It's purpose is also to highlight my overall process and sources I used while creating my own physics engine. I'll be focusing mainly on this physics engine and discussing the creation of physics engines in general.</subtitle><author><name>Sahil Bhalla</name></author><entry><title type="html">2D Rpg and This Website!</title><link href="/jekyll/update/2021/06/06/2D-Rpg-and-this-website.html" rel="alternate" type="text/html" title="2D Rpg and This Website!" /><published>2021-06-06T00:00:00+00:00</published><updated>2021-06-06T00:00:00+00:00</updated><id>/jekyll/update/2021/06/06/2D-Rpg-and-this-website</id><content type="html" xml:base="/jekyll/update/2021/06/06/2D-Rpg-and-this-website.html">&lt;p&gt;Hi readers!  &lt;br /&gt;
In this post, I’m going to discuss a bit about the 2D rpg I worked on as part of the game development club at UCSD and how I made this very website!&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#2d-rpg&quot;&gt;2D RPG&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#this-website&quot;&gt;This Website&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;2d-rpg&quot;&gt;2D RPG&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/Unity.jpg&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As part of the game development club at UCSD, I joined a team in the fall with the goal of making a 2D Rpg in Unity Game Engine. A 2D Rpg, is short for a two-dimensional role-playing game, like the Legend of Zelda, Final Fantasy, or Pokemon. Unity is a tool that has a bunch of helpful features when making your own game and is a very popular choice for those new to making games which was perfect for a team almost entirely new.&lt;/p&gt;

&lt;p&gt;Our first phase of the project was planning, and as we knew we might only have a quarter to work on the project, we decided to set priorities for which part of the game we wanted to complete first. We decided that at the very least we should get the character system, the party system, and the enemies complete in the context of a turn-based battle a la Octopath Traveller.&lt;/p&gt;

&lt;p&gt;I decided to work on the back end of the battle system for the game. The battle system handles the information about the attacks, damage, and user inputs behind the scenes. Going for an object-oriented approach we decided to have one static class that managed these. I was tasked with making and implementing this class along with the turn system for the battles. I decided to use a state-based system that would tell the rest of the components whose turn it was and whether to accept input or not.&lt;/p&gt;

&lt;h2 id=&quot;this-website&quot;&gt;This Website&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/Jekyll_(software)_Logo.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;To create this website I decided that instead of using WordPress, or another more common website creation service, I would use Github Pages, a service that lets you host almost whatever website you want for free. However I didn’t want to code all the Html for it myself, so I used Github Pages in conjunction with Jekyll which is a program that generates the Html files for Github based on a theme and other specification. This is different than other services because it can allow for all the customization you want as one has the option to directly modify the templates and themes that come included, and the option to get countless more themes. I decided this system was perfect for me as it wouldn’t take too much time, yet it would still allow me to be flexible and make changes if I needed to.&lt;/p&gt;

&lt;p&gt;Making this website turned out to be a very helpful experience as I learned how to make a website on Github Pages and Jekyll, and I feel confident that if I need to make one in the future I will know exactly what to do.&lt;/p&gt;</content><author><name>Sahil Bhalla</name></author><category term="jekyll" /><category term="update" /><summary type="html">Hi readers! In this post, I’m going to discuss a bit about the 2D rpg I worked on as part of the game development club at UCSD and how I made this very website!</summary></entry><entry><title type="html">My Physics Engine</title><link href="/jekyll/update/2021/06/06/My-Physics-Engine.html" rel="alternate" type="text/html" title="My Physics Engine" /><published>2021-06-06T00:00:00+00:00</published><updated>2021-06-06T00:00:00+00:00</updated><id>/jekyll/update/2021/06/06/My-Physics-Engine</id><content type="html" xml:base="/jekyll/update/2021/06/06/My-Physics-Engine.html">&lt;p&gt;Hi readers!  &lt;br /&gt;
In this post, I’m going to discuss the how I made my physics engine at both a high level and low level. I chose to use C++ because it is the language in which most modern physics engines are built, and it is optimized to be fast compared to a language like python.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#vector-math&quot;&gt;Vector Math&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#list&quot;&gt;List&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#the-world&quot;&gt;The World&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#game-objects&quot;&gt;Game Objects&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#testing&quot;&gt;Testing&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;vector-math&quot;&gt;Vector Math&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/Vector.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The first class I built is the vector math system as all the position and movement information for the objects in my physics engine is handled through three-dimensional vectors. The reason for this is that a vector is an excellent model for 2D physics as the most important characteristics about an object that can’t be represented as a constant, can be represented by vectors. These properties include force, velocity, acceleration, and position. It is also very easy to do basic operations like addition and subtraction with vectors as shown in the code. It can also compute the dot product, cross product, and the magnitude of any vector.&lt;/p&gt;

&lt;h2 id=&quot;list&quot;&gt;List&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/List.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The next backing class I built before getting started on the actual physics engine was a list class to store all the objects in the engine. I could have used the STL list class and saved myself the effort, however, I wanted to practice the construction of basic data structures. This list is a linked list which I chose because it has fast removal and addition compared to the array list which would be useful when adding and removing objects to the world. Its random lookup is poor but that is fine since I would mostly be iterating through all the objects rather than lookup up a random one.&lt;/p&gt;

&lt;h2 id=&quot;the-world&quot;&gt;The World&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/World.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Next, I built the overall world of the physics engine that would contain all the game objects, and relevant fields that should belong to the world like air resistance, the size of the time-step, and the function for the force field i.e. gravity. This is the class that calculates the basic forces on the objects, then moves them, then checks for and handles collisions. Each of these functions besides the force field actually delegates its actual operation to the game object or to another class so that the program preserves object-oriented design principles. In this way, each game object actually moves itself when given all the information by the world. I haven’t yet implemented collision detection or resolution yet, but I still have the methods in the structure so ideally all I need to do is fill in the body and it works.&lt;/p&gt;

&lt;h2 id=&quot;game-objects&quot;&gt;Game Objects&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/GameObject.png&quot; alt=&quot;image&quot; /&gt;   &lt;img src=&quot;/assets/images/GameObject2.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;These are what describe each of the “physical” objects in the physics engine and they contain all the information about the object such as its position, shape, and other physical quantities. Using the physical quantities a Game Object can also advance itself an arbitrary amount of time and move accordingly, although it has no knowledge of other Game Objects so can’t detect collisions. I decided to model GameObjects not as a limited set of simple shapes, but as any polygon by allowing them to have an array of any size to represent the vertices of the game object relative to its central position. This makes it really easy to move and rotate the game object since all I have to do is rotate the vertices relative to the center and translate the position vector to where I want it to go.&lt;/p&gt;

&lt;h2 id=&quot;testing&quot;&gt;Testing&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/Testing.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Finally, this is the main file that actually uses the physics engine. For now, it’s just running some tests but in the future, it could be a simulator or a full game that uses the methods in my physics engine to handles its physics.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;</content><author><name>Sahil Bhalla</name></author><category term="jekyll" /><category term="update" /><summary type="html">Hi readers! In this post, I’m going to discuss the how I made my physics engine at both a high level and low level. I chose to use C++ because it is the language in which most modern physics engines are built, and it is optimized to be fast compared to a language like python.</summary></entry><entry><title type="html">Souto’s Physics Engine Tutorial and Why it’s Great!</title><link href="/jekyll/update/2021/06/06/Souto's-Physics-Engine-Tutorial-and-why-it's-great.html" rel="alternate" type="text/html" title="Souto’s Physics Engine Tutorial and Why it’s Great!" /><published>2021-06-06T00:00:00+00:00</published><updated>2021-06-06T00:00:00+00:00</updated><id>/jekyll/update/2021/06/06/Souto's-Physics-Engine-Tutorial-and-why-it's-great</id><content type="html" xml:base="/jekyll/update/2021/06/06/Souto's-Physics-Engine-Tutorial-and-why-it's-great.html">&lt;p&gt;Hi readers!  &lt;br /&gt;
In this post, I’m going to discuss the biggest help in creating my physics engine out of all my research, and why it was so effective at teaching the concepts of physics engines. Check out the &lt;a href=&quot;https://wanderingvagrant.github.io/Links/&quot;&gt;link’s page&lt;/a&gt; if you want to check it out for yourself.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#motivation&quot;&gt;Motivation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#images&quot;&gt;Images&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#information-layout&quot;&gt;Information Layout&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;motivation&quot;&gt;Motivation&lt;/h2&gt;

&lt;p&gt;This tutorial is great at showing exactly why the information it gives you is useful for a physics engine and it doesn’t wait too much time before getting into exactly what needs to be. For a reader who was searching around and finding really vague, theoretical guides and tutorials this was a breath of fresh air, and really help a reader stay engaged and interested in learning more. Furthermore, alongside the equations and conceptual explanations we are given helpful code snippets that we can run on our own computers and edit so we we can actually see how its implemented as we’re reading. There’s enough information that you can see in general hwo it could work, but not so much that you end up just copying and not learning anything.&lt;/p&gt;

&lt;h2 id=&quot;images&quot;&gt;Images&lt;/h2&gt;

&lt;p&gt;The entire tutorial is surrounded by extremely helpful and eye-catching images and schematics that helpfully illustrate whatever point is being made. Some of the concepts explained are unintuitive and complex, so having images to clarify what is happening is very useful. They also do a good job of making the website look professional and establish ethos so that the reader trusts what the tutorial has to say.&lt;/p&gt;

&lt;h2 id=&quot;information-layout&quot;&gt;Information Layout&lt;/h2&gt;

&lt;p&gt;Finally what I found most unique and interesting about this tutorial was how cleverly the information was presented and laid out. First off the tutorial is in three sections, Dynamics, Collision Detection, and Constrained Simulation aka Collision resolution. This is smart because not only are these components usually implemented separately, so wouldn’t need to be grouped together, but are ordered in increasing order of complexity. This makes sense because someone just wanting to learn how movement works wouldn’t have to scroll past information about collisions, and someone who wanted to make a full physics engine get the information in easily digestible chunks, and not all at once. This principle is also applied within each section as well with a basic version of the concept being explained initially, and it getting more advanced as you read further, until the end where it is very complex. Once again this makes the tutorial appeal to many people as those looking to make physics engines of all levels of complexity can learn a lot here.&lt;/p&gt;</content><author><name>Sahil Bhalla</name></author><category term="jekyll" /><category term="update" /><summary type="html">Hi readers! In this post, I’m going to discuss the biggest help in creating my physics engine out of all my research, and why it was so effective at teaching the concepts of physics engines. Check out the link’s page if you want to check it out for yourself.</summary></entry><entry><title type="html">What is a Physics Engine</title><link href="/jekyll/update/2021/06/06/What-is-a-engine.html" rel="alternate" type="text/html" title="What is a Physics Engine" /><published>2021-06-06T00:00:00+00:00</published><updated>2021-06-06T00:00:00+00:00</updated><id>/jekyll/update/2021/06/06/What-is-a-engine</id><content type="html" xml:base="/jekyll/update/2021/06/06/What-is-a-engine.html">&lt;p&gt;Hi readers!  &lt;br /&gt;
In this post, I’m going to discuss the basics of a physics engine, and give a basic overview different ways in which they are built.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#the-absolute-basics&quot;&gt;The Absolute Basics&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#the-core-loop&quot;&gt;The Core Loop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#rigid-bodies-vs-soft-bodies&quot;&gt;Rigid Bodies vs Soft Bodies&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#movement&quot;&gt;Movement&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#collision-detection&quot;&gt;Collision Detection&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#collision-resolution&quot;&gt;Collision Resolution&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;the-absolute-basics&quot;&gt;The Absolute Basics&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/WorldObject.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A physics engine is what it sounds like, the part of a program that simulates real-life physics. They can be used in many different contexts, from video games, to biomedical research, to CGI in the most popular movies. But why would someone need a physics engine, and what exactly can they do?&lt;/p&gt;

&lt;p&gt;One use most of you would have experienced already is in animating advanced CGI graphics in modern movies. Detailed 3d cloth and fluid moving is a very difficult thing to animate, and very time-consuming to do by hand, but with the help of a physics engine it can be done well and in less time.&lt;/p&gt;

&lt;p&gt;Another use, and the one my physics engine is based on, is for approximating the movement of objects in video games according to real-life physics. Figuring out the trajectory of a bullet in a shooter and determining the movement of the main character in a platformer is all the work of a physics engine.&lt;/p&gt;

&lt;h2 id=&quot;the-core-loop&quot;&gt;The Core Loop&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/PhysLoop.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;But how exactly does a video-game physics engine work? The most commonly used paradigm is by setting up a world where physics happens. Then you add some global constants like gravity or air resistance, and then you add in all the entities to populate the world and do physics on those objects.&lt;/p&gt;

&lt;p&gt;The actual core loop of a physics engine looks somewhat like this on all the objects in the world. Calculate the new positions of all objects based on their current velocities, then detect collisions, and resolve those collisions with a change in momentum or force. However, each of these steps has few different widely used methods.dd&lt;/p&gt;

&lt;h2 id=&quot;rigid-bodies-vs-soft-bodies&quot;&gt;Rigid Bodies vs Soft Bodies&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/RigidvsSoft.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;For the first step in the loop, calculating the positions of the objects depends a lot on what those objects are, and they can generally be classified into two types. A rigid body, or a soft body. A rigid body is generally the type of object most used in physics classes.&lt;/p&gt;

&lt;p&gt;They are incompressible objects made up of a set of shapes connected by unchangeable joints. Some examples would be a pendulum, a simple stick figure, and most objects in 2d simulation. Rigid bodies are the more simplified and unrealistic type of object since most real-life objects are compressible.&lt;/p&gt;

&lt;p&gt;A soft body on the other hand is the opposite, and consists of components like springs or elastic materials and generally involve much more complex math to calculate the position and forces for. Some examples would be hair, clothes, and most objects in very realistic physics simulations.&lt;/p&gt;

&lt;p&gt;This is why I will elaborate only on rigid body dynamics.&lt;/p&gt;

&lt;h2 id=&quot;movement&quot;&gt;Movement&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/Movement.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;For rigid bodies, the main types of motion that we need to account for are rotation and translation. Translation is simply moving the whole body in some direction along the center of mass and can be calculated by the simple equation from the velocity, d = vt.&lt;/p&gt;

&lt;p&gt;However, there are many methods of different precisions for calculating the linear velocity as often the movement equations boil down to a series of differential equations that can’t be solved directly quickly. Most physics engines use approximations like Euler’s method or more accurate approximations like the Runge-Kutta methods.&lt;/p&gt;

&lt;p&gt;Rotation however occurs whenever a force or impulse is applied at an angle relative to the center of rotation. Based on this angle, and the distance of the force to the center of rotation, and the moment of inertia, we can calculate the angular velocity and therefore the rotation.&lt;/p&gt;

&lt;p&gt;Rotating in 2D as I implemented in my own physics engine is relatively simple as you just rotate something around a central point, but in 3d it becomes much harder. Different methods exist for 3D rotation from the simple Euler Angles that were initially devised, to the complex quaternions that are used in today’s modern physics engines.&lt;/p&gt;

&lt;h2 id=&quot;collision-detection&quot;&gt;Collision Detection&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/BoradvsNarrow.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A collision is when two objects touch each other or overlap, and in most games it provokes some kind of response, whether it gives you a power-up, or the car deforms spectacularly in a car crash simulation.&lt;/p&gt;

&lt;p&gt;Detecting the collisions is a surprising challenge, however. One obvious method is to check if all possible pairs of objects are colliding, but this method is extremely inefficient because the time it takes increases with the square of the number of objects in the world. This is why most physics engines divide the task into two parts, broad phase and narrow phase.&lt;/p&gt;

&lt;p&gt;In the broad phase, the focus is eliminating all objects that have no chance of colliding with each other such as those extremely far from each other.&lt;/p&gt;

&lt;p&gt;Then the narrow phase checks all the pairs of objects that actually have a chance of colliding.&lt;/p&gt;

&lt;h2 id=&quot;collision-resolution&quot;&gt;Collision Resolution&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/Constraints.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The final step is collision resolution, what do we do now that we know objects are colliding.? Well in rigid bodies the basic answer is simple, they need to bounce off of each other. There are a few ways of calculating this, but all of them generally use the concept of constraints.&lt;/p&gt;

&lt;p&gt;Constraints are limitations to an object’s degrees of freedom, or how it can move. They can be as simple as forcing two objects to remain a minimum distance apart, or more complicated like the limits to the motion of a human arm. A physics engine can use linear algebra with force or impulse to solve these constraints and find the final positions of the colliding objects.&lt;/p&gt;

&lt;p&gt;That ends this basic overview of how physics engines work.&lt;/p&gt;</content><author><name>Sahil Bhalla</name></author><category term="jekyll" /><category term="update" /><summary type="html">Hi readers! In this post, I’m going to discuss the basics of a physics engine, and give a basic overview different ways in which they are built.</summary></entry></feed>