Homework1 Physics Engine (Due 6/23 @ 11:59 p.m.) Time Estimate: 15 hours Video Physics Engine Overview Introduction Computers do not understand physics, yet there are many applications where we need a...

Scala programming lanaguage


Homework1 Physics Engine (Due 6/23 @ 11:59 p.m.) Time Estimate: 15 hours Video Physics Engine Overview Introduction Computers do not understand physics, yet there are many applications where we need a physics simulation to improve the user experience. In this assignment, you will teach your computer how to simulate physics. Your physics engine will work with both static and dynamic objects and update them linearly over a given time interval (You won’t use physics to compute exact movements). When we develop apps to use this engine, the time interval between updates (dt) will be small enough that we will have an accurate [enough] simulation of physics. In our world only the following laws apply: ● The world is 3-dimensional with dimensions labeled x, y, and z ● The world has a constant acceleration due to gravity in the negative z-direction (positive z is up) ● The world contains lists of static and dynamic objects each represented by a rectangular cuboid parallel to the axes ● The ground is the plane at z=0. No objects can fall below the ground ● An object on the ground will have its velocity in the z-direction set to zero If you don’t have a great understanding of physics, here’s a sheet (last page) that might help you understand the math equations. Project Structure Repository: https://github.com/anbaccar/JumperNoPhysics 1. Clone the starter code from the repository into an IntelliJ project 2. Feel free to look through all the provided code, though you are not expected to understand most of it at this time. This repo contains a 2D vertical scrolling platformer game that can be played by running the view.JumperApp object. However, this game has no physics engine! You’re goal is to add physics to this game 3. Look through the code in the physics package and explore what has been provided for you. You are expected to understand the code in this package and will use the provided classes and methods to complete your physics engine 4. Only edit the PhysicsEngine class. Any changes in the other classes may interfere with grading code since it is expecting these exact interfaces 5. You may add helper methods to the PhysicsEngines beyond what is required if it helps you organize your code Objectives Testing Note ● You may add more methods to the classes in the project structure to complete the objectives, but do not call these methods in your unit tests. Your tests are ran against complete submissions on the server which will only contain the classes and methods defined by the assignment. Objective 1: Collision Detection In the PhysicsEngine object, write a method named isCollision that takes two GameObjects as parameters and returns a Boolean. The method returns true if the rectangular cuboids representing the objects overlap (ie. collide) and false otherwise. Study the GameObject class for details about how the rectangular cuboids are represented. Note: Objects do not collide if they are touching, but not intersecting (ie. make comparisons with strict inequalities instead of <=,>=). In the tests package, complete the test suite named TestCollisions to test this method. Feedback Note ● Many homework objectives in this course will ask you to test your method with a test suite. When grading these objectives, your test suite will run against your own code, a correct solution, and a variety of incorrect solutions. The results (pass/fail) of your test suite on each solution will be provided ● You should ensure that your tests distinguish between all the correct and incorrect solutions before moving forward, but this does not always mean that your code is completely correct since there may be an error in your code that is not made by an incorrect solution, nor tested by your test suite. Additional testing may still be needed to complete the objective. The feedback in AutoLab is meant to help you, but it is not comprehensive ● If there is an error in any of your numbered objectives, even while correctly testing the correct/incorrect solutions, it will be difficult and frustrating to complete the primary objective. Be sure to thoroughly test your code and do not rely on AutoLab to test it for you Objective 2: Updating Objects In the PhysicsEngine object, write a method named updateObject that takes a DynamicObject, a Double representing the time elapsed since the last update, and a Double representing the magnitude of gravity in the negative z-direction (this value is the magnitude and will be positive) and returns Unit. The three parameters must be provided in this order. This method will apply physics to the object over the amount of time provided. All calculations should be linear (ie. don’t use calculus). This method will: 1. First, update the object’s velocity by applying gravity in the negative z direction 2. Next, store the object’s current location as its previous location a. Warning: Be careful not to copy this value by reference. If you try: obj.previousLocation = obj.location These two variables will refer to the same object in memory and your engine will not accurately react to collisions. You should set the x, y, and z coordinates of the previous location separately to preserve the references. 3. Finally, update the object’s location by advancing the position based on the velocity. This should be computed linearly (ie. multiply by time and add). Do not simulate the exact movement using calculus or other equations. a. If the object is now on or below the ground (z <= 0.0), set the z component of the location and velocity to 0.0 and call the object’s onground() method to notify it of this fact. in the tests package write a test suite named testobjectupdate that tests this method. note that since this method returns unit you will test it by checking the state of a dynamic object after the method is called. since the method modifies the locations and velocity of the object, you can check these variables after the method call and ensure that they are set to the values you would expect based on our simulated physics. objective 3: static collision in the physicsengine object, write a method named checkstaticcollision that takes a staticobject and a dynamicobject as parameters and returns unit. this method will: 1. check if these objects collide (you can/should call your method from objective 1 to check for collisions) 2. if they collide a. call collidewithstaticobject on the dynamic object with the static object as an argument b. call collidewithdynamicobject on the static object with the dynamic object and the face as arguments. the face is the face of the static object with which the dynamic object collided. this lets the static object know which of the six faces were hit by the dynamic object allowing it to react differently to collisions on different sides. review the face object for the variable names of all the options. c. you may assume that only 1 face collision occurs during an update and you should only test for these cases (ex. if the collision occurs near an edge, or corner, computing the correct face becomes more difficult) i. specifically, you may assume that the previous location “overlaps” with the static object in at least 2 dimensions d. if the objects were already colliding, based on the dynamic object’s previous location, the colliding face should be “internal.” in the tests package write a test suite named teststaticcollision that tests this method. note: this method will determine the behavior of walls and platforms in the jumper game. if the player (dynamicobject) collides with the top of a platform (staticobject) the player will land on that platform. if the player collides with any other face, it will move through the platform. walls (staticobjects) will stop the player when colliding in the negativex or positivex directions. primary objective in the physicsengine object, write a method named updateworld which takes a world and a double, representing the time elapsed since the last update, as parameters and returns unit. call all your methods to update the world’s objects by this amount of time. in this method you should: 1. update all of the dynamic objects in the world to compute their new locations/velocities while storing their previous locations in case they make an invalid move. (ie. call updateobject for each dynamicobject) 2. handle static collisions for each dynamicobject using your checkstaticcollision method (ie. call checkstaticcollision for each combination of static and dynamic object). you should write tests for this objective, though you will not receive feedback regarding your tests for the primary objective. note: the updateworld method is the only way the jumper game interacts with your physics engine. as you complete this method, play the game and see what your engine can do. note 2: if you’ve completed the previous 3 objectives, the primary objective should not be very difficult. this objective asks you to combine all your previous work to be able to update the objects in a game world. useful references: https://www.youtube.com/watch?v=guwif87cmbg https://www.real-world-physics-problems.com/elastic-collision.html https://en.wikipedia.org/wiki/vector_projection#vector_projection_2 for clarification on the problem statement, here are two example collisions with the expected resulting velocities. example 1 object 1 location: (-0.1, -1.0, 0.0) dimensions: (1.0, 1.0, 1.0) velocity: (2.0, 1.5, 0.0) mass: 2.0 object 2 location: (0.1, -1.0, 0.0) dimensions: (1.0, 1.0, 1.0) velocity: (-2.0, 1.0, 0.0) mass: 4.0 * update world by 0.1 seconds * object 1 velocity: (-3.176470588235294, 0.20588235294117496, 0.0) object 2 velocity: (0.5882352941176473, 1.6470588235294126, 0.0) example 2 object 1 location: (-5.0, 1.0, 0.0) dimensions: (4.0, 1.0, 2.0) velocity: (1.5, 0.0, 0.0) mass: 10.0 object 2 location: (0.0, 0.0, 0.0) dimensions: (1.0, 2.0, 5.0) velocity: (0.0, 0.0, 0.0) mass: 20.0 * update world by 1.0 seconds * object 1 velocity: (0.26923076923076905, 0.3076923076923077, -0.9230769230769232) object 2 velocity: (0.6153846153846154, -0.15384615384615385, 0.4615384615384616) 0.0),="" set="" the="" z="" component="" of="" the="" location="" and="" velocity="" to="" 0.0="" and="" call="" the="" object’s="" onground()="" method="" to="" notify="" it="" of="" this="" fact.="" in="" the="" tests="" package="" write="" a="" test="" suite="" named="" testobjectupdate="" that="" tests="" this="" method.="" note="" that="" since="" this="" method="" returns="" unit="" you="" will="" test="" it="" by="" checking="" the="" state="" of="" a="" dynamic="" object="" after="" the="" method="" is="" called.="" since="" the="" method="" modifies="" the="" locations="" and="" velocity="" of="" the="" object,="" you="" can="" check="" these="" variables="" after="" the="" method="" call="" and="" ensure="" that="" they="" are="" set="" to="" the="" values="" you="" would="" expect="" based="" on="" our="" simulated="" physics.="" objective="" 3:="" static="" collision="" in="" the="" physicsengine="" object,="" write="" a="" method="" named="" checkstaticcollision="" that="" takes="" a="" staticobject="" and="" a="" dynamicobject="" as="" parameters="" and="" returns="" unit.="" this="" method="" will:="" 1.="" check="" if="" these="" objects="" collide="" (you="" can/should="" call="" your="" method="" from="" objective="" 1="" to="" check="" for="" collisions)="" 2.="" if="" they="" collide="" a.="" call="" collidewithstaticobject="" on="" the="" dynamic="" object="" with="" the="" static="" object="" as="" an="" argument="" b.="" call="" collidewithdynamicobject="" on="" the="" static="" object="" with="" the="" dynamic="" object="" and="" the="" face="" as="" arguments.="" the="" face="" is="" the="" face="" of="" the="" static="" object="" with="" which="" the="" dynamic="" object="" collided.="" this="" lets="" the="" static="" object="" know="" which="" of="" the="" six="" faces="" were="" hit="" by="" the="" dynamic="" object="" allowing="" it="" to="" react="" differently="" to="" collisions="" on="" different="" sides.="" review="" the="" face="" object="" for="" the="" variable="" names="" of="" all="" the="" options.="" c.="" you="" may="" assume="" that="" only="" 1="" face="" collision="" occurs="" during="" an="" update="" and="" you="" should="" only="" test="" for="" these="" cases="" (ex.="" if="" the="" collision="" occurs="" near="" an="" edge,="" or="" corner,="" computing="" the="" correct="" face="" becomes="" more="" difficult)="" i.="" specifically,="" you="" may="" assume="" that="" the="" previous="" location="" “overlaps”="" with="" the="" static="" object="" in="" at="" least="" 2="" dimensions="" d.="" if="" the="" objects="" were="" already="" colliding,="" based="" on="" the="" dynamic="" object’s="" previous="" location,="" the="" colliding="" face="" should="" be="" “internal.”="" in="" the="" tests="" package="" write="" a="" test="" suite="" named="" teststaticcollision="" that="" tests="" this="" method.="" note:="" this="" method="" will="" determine="" the="" behavior="" of="" walls="" and="" platforms="" in="" the="" jumper="" game.="" if="" the="" player="" (dynamicobject)="" collides="" with="" the="" top="" of="" a="" platform="" (staticobject)="" the="" player="" will="" land="" on="" that="" platform.="" if="" the="" player="" collides="" with="" any="" other="" face,="" it="" will="" move="" through="" the="" platform.="" walls="" (staticobjects)="" will="" stop="" the="" player="" when="" colliding="" in="" the="" negativex="" or="" positivex="" directions.="" primary="" objective="" in="" the="" physicsengine="" object,="" write="" a="" method="" named="" updateworld="" which="" takes="" a="" world="" and="" a="" double,="" representing="" the="" time="" elapsed="" since="" the="" last="" update,="" as="" parameters="" and="" returns="" unit.="" call="" all="" your="" methods="" to="" update="" the="" world’s="" objects="" by="" this="" amount="" of="" time.="" in="" this="" method="" you="" should:="" 1.="" update="" all="" of="" the="" dynamic="" objects="" in="" the="" world="" to="" compute="" their="" new="" locations/velocities="" while="" storing="" their="" previous="" locations="" in="" case="" they="" make="" an="" invalid="" move.="" (ie.="" call="" updateobject="" for="" each="" dynamicobject)="" 2.="" handle="" static="" collisions="" for="" each="" dynamicobject="" using="" your="" checkstaticcollision="" method="" (ie.="" call="" checkstaticcollision="" for="" each="" combination="" of="" static="" and="" dynamic="" object).="" you="" should="" write="" tests="" for="" this="" objective,="" though="" you="" will="" not="" receive="" feedback="" regarding="" your="" tests="" for="" the="" primary="" objective.="" note:="" the="" updateworld="" method="" is="" the="" only="" way="" the="" jumper="" game="" interacts="" with="" your="" physics="" engine.="" as="" you="" complete="" this="" method,="" play="" the="" game="" and="" see="" what="" your="" engine="" can="" do.="" note="" 2:="" if="" you’ve="" completed="" the="" previous="" 3="" objectives,="" the="" primary="" objective="" should="" not="" be="" very="" difficult.="" this="" objective="" asks="" you="" to="" combine="" all="" your="" previous="" work="" to="" be="" able="" to="" update="" the="" objects="" in="" a="" game="" world.="" useful="" references:="" https://www.youtube.com/watch?v="guWIF87CmBg" https://www.real-world-physics-problems.com/elastic-collision.html="" https://en.wikipedia.org/wiki/vector_projection#vector_projection_2="" for="" clarification="" on="" the="" problem="" statement,="" here="" are="" two="" example="" collisions="" with="" the="" expected="" resulting="" velocities.="" example="" 1="" object="" 1="" location:="" (-0.1,="" -1.0,="" 0.0)="" dimensions:="" (1.0,="" 1.0,="" 1.0)="" velocity:="" (2.0,="" 1.5,="" 0.0)="" mass:="" 2.0="" object="" 2="" location:="" (0.1,="" -1.0,="" 0.0)="" dimensions:="" (1.0,="" 1.0,="" 1.0)="" velocity:="" (-2.0,="" 1.0,="" 0.0)="" mass:="" 4.0="" *="" update="" world="" by="" 0.1="" seconds="" *="" object="" 1="" velocity:="" (-3.176470588235294,="" 0.20588235294117496,="" 0.0)="" object="" 2="" velocity:="" (0.5882352941176473,="" 1.6470588235294126,="" 0.0)="" example="" 2="" object="" 1="" location:="" (-5.0,="" 1.0,="" 0.0)="" dimensions:="" (4.0,="" 1.0,="" 2.0)="" velocity:="" (1.5,="" 0.0,="" 0.0)="" mass:="" 10.0="" object="" 2="" location:="" (0.0,="" 0.0,="" 0.0)="" dimensions:="" (1.0,="" 2.0,="" 5.0)="" velocity:="" (0.0,="" 0.0,="" 0.0)="" mass:="" 20.0="" *="" update="" world="" by="" 1.0="" seconds="" *="" object="" 1="" velocity:="" (0.26923076923076905,="" 0.3076923076923077,="" -0.9230769230769232)="" object="" 2="" velocity:="" (0.6153846153846154,="" -0.15384615384615385,="">
Jun 24, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here