.. Copyright (c) 2018-2024 William Emerison Six Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Relative Objects - Demo 11 ========================== Purpose ^^^^^^^ Introduce relative objects, by making a small blue square that is defined relative to the left paddle, but offset some in the x direction. When the paddle on the left moves or rotates, the blue square moves with it, because it is defined relative to it. .. figure:: static/screenshots/demo11.png :align: center :alt: Demo 11 :figclass: align-center Demo 11 How to Execute ^^^^^^^^^^^^^^ On Linux or on MacOS, in a shell, type "python src/demo11/demo.py". On Windows, in a command prompt, type "python src\\demo11\\demo.py". Move the Paddles using the Keyboard ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ============== ============================================== Keyboard Input Action ============== ============================================== *w* Move Left Paddle Up *s* Move Left Paddle Down *k* Move Right Paddle Down *i* Move Right Paddle Up *d* Increase Left Paddle's Rotation *a* Decrease Left Paddle's Rotation *l* Increase Right Paddle's Rotation *j* Decrease Right Paddle's Rotation *UP* Move the camera up, moving the objects down *DOWN* Move the camera down, moving the objects up *LEFT* Move the camera left, moving the objects right *RIGHT* Move the camera right, moving the objects left ============== ============================================== Description ^^^^^^^^^^^ Cayley Graph ^^^^^^^^^^^^ In the graph below, all we have added is "Square space", relative to paddle 1 space. .. figure:: static/demo11.png :align: center :alt: Demo 11 :figclass: align-center Demo 11 In the picture below, in 3D space, we see that the square has its own modelspace (as evidenced by the 3 arrows), and we are going to define its position and orientation relative to paddle 1. .. figure:: static/screenshots/coordinateframes.png :align: center :alt: Coordinate Frames :figclass: align-center Coordinate Frames Code ^^^^ .. We in looking at the code for the three objects we are drawing you might notice that we are duplicating a lot of code, and that if we decide to change paddle1's transformations, that we would need to change the code in multiple places. Later demos will show how to handle this problem more gracefully. Define the geometry of the square in it's own modelspace. .. literalinclude:: ../src/demo11/demo.py :language: python :start-after: doc-region-begin 3bb2c05b4cd66636ad8b8fc18c26f7a64af06b7c :end-before: doc-region-end 3bb2c05b4cd66636ad8b8fc18c26f7a64af06b7c Event Loop ~~~~~~~~~~ .. literalinclude:: ../src/demo11/demo.py :language: python :start-after: doc-region-begin 67ffd7b7adc42d01ca93bacdef858c0d4b678e38 :end-before: doc-region-end 67ffd7b7adc42d01ca93bacdef858c0d4b678e38 :: ... Draw paddle 1, just as before. .. literalinclude:: ../src/demo11/demo.py :language: python :start-after: doc-region-begin 7dab1135450b265814f775c59807f77b44273a4e :end-before: doc-region-end 7dab1135450b265814f775c59807f77b44273a4e As a refresher, the author recommends reading the code from modelspace to worldspace from the bottom up, and from worldspace to NDC from top down. * Read from modelspace to world space, bottom up * Reset the coordinate system * Read from world space to camera space, knowing that camera transformations are implemented as the inverse of placing the camera space in world space. * Reset the coordinate system * Read cameraspace to NDC New part! Draw the square relative to the first paddle! Translate the square to the right by 20 units. We are dealing with a -100 to 100 world space, which later gets scaled down to NDC. .. literalinclude:: ../src/demo11/demo.py :language: python :start-after: doc-region-begin 9a0ba48a086f35a4515bf32b4a856888c178b0e8 :end-before: doc-region-end 9a0ba48a086f35a4515bf32b4a856888c178b0e8 Towards that, we need to do all of the transformations to the square that we would to the paddle, and then do any extra transformations afterwards. As such, read * Read paddle1space to world space, from bottom up If we were to plot the square now, it would be in paddle 1's space. We don't want that, we want in to be moved in the X direction some units. Therefore * Read modelspace to paddle1space, from bottom up * Reset the coordinate system. Now the square's geometry will be in it's own space! * Read from worldspace to cameraspace, knowing that camera transformations are implemented as the inverse of placing the camera space in world space. * Reset the coordinate system * Read cameraspace to NDC Draw paddle 2 just like before. .. literalinclude:: ../src/demo11/demo.py :language: python :start-after: doc-region-begin 12cd2bedfe522c8c192106296c12e8344f1594d4 :end-before: doc-region-end 12cd2bedfe522c8c192106296c12e8344f1594d4