מפגש 44: חלקיקים, בד Verlet, משחק החיים ולהקות Boids.Session 44: Particles, Verlet cloth, the Game of Life and Boids flocks.
חלקיקים, בד Verlet, משחק החיים ולהקות Boids. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Particles, Verlet cloth, the Game of Life and Boids flocks. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
וקטוריםVectors
כוחותForces
זריקהThrow
קפיץSpring
מסלולOrbit
מעבדהLab
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
normalize() = כיוון באורך 1normalize() = a direction of length 1
שורה אחת במקום x ו-y בנפרדOne line instead of x and y separately
עד עכשיו כתבנו כל דבר פעמיים: x += vx ו-y += vy. וקטור מחזיק את שניהם, ו-pygame נותן pygame.math.Vector2: מחברים וקטורים, כופלים במספר, מודדים אורך, ומנרמלים לכיוון באורך 1. מעבדת הפיזיקה שלנו בנויה עליו:Until now we wrote everything twice: x += vx and y += vy. A vector holds both, and pygame provides pygame.math.Vector2: add vectors, multiply by a number, measure length, and normalise to a direction of length 1. Our physics lab is built on it:
אנלוגיהAnalogy
וקטור הוא כמו חץ על מפה: יש לו כיוון ויש לו אורך, ואפשר לשרשר חצים: "3 צפונה ואז 4 מזרחה" הוא חץ אחד באורך 5.A vector is like an arrow on a map: it has a direction and a length, and arrows chain: "3 north then 4 east" is one arrow of length 5.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
טעות נפוצהCommon mistake
vectors.py
from pygame.math import Vector2
print(Vector2(0, 0).normalize())
מה Python עונהWhat Python says
ValueError: Can't normalize Vector of length zero
ValueError: Can't normalize Vector of length zero: אין כיוון לחץ באורך 0. בודקים if v.length() > 0: לפני.ValueError: Can't normalize Vector of length zero: an arrow of length 0 has no direction. Check if v.length() > 0: first.
מנרמלים וקטור באורך אפס.Normalising a zero-length vector.
נושאTopic 2
כוחותForces
כוחות ותאוצהForces and acceleration
כוח → תאוצה: a = F / mForce → acceleration: a = F / m
אוילר: v += a*dt, p += v*dtEuler: v += a*dt, p += v*dt
כמה כוחות = מחברים וקטוריםSeveral forces = add the vectors
כל סימולציה בנויה על אותה לולאה: מחברים את כל הכוחות (כבידה, רוח, דחיפה) לוקטור אחד, מחלקים במסה ומקבלים תאוצה, ואז בשילוב אוילר מעדכנים מהירות ואז מיקום. כאן גוף בכבידה ורוח צד, עם dt של 1/60 שנייה:Every simulation is built on the same loop: add all the forces (gravity, wind, a push) into one vector, divide by mass to get acceleration, then with Euler integration update velocity and then position. Here a body in gravity and a side wind, with dt of 1/60 second:
אנלוגיהAnalogy
סימולציה היא כמו לצעוד במגפיים בחושך: בכל צעד מסתכלים לאן הכוחות מושכים, מתקנים כיוון קצת, וצועדים שוב.A simulation is like walking in boots in the dark: every step you feel where the forces pull, adjust a little, and step again.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 3
זריקהThrow
זריקה ומסלולThrowing and trajectory
מהירות התחלתית לפי זוויתInitial velocity from an angle
סימולציה מול נוסחהSimulation vs formula
dt קטן = מדויק יותרSmaller dt = more accurate
זורקים כדור בזווית: המהירות ההתחלתית היא וקטור שאורכו המהירות וכיוונו הזווית (Vector2.from_polar). כאן מריצים סימולציה עם שני ערכי dt ומשווים לנוסחה המדויקת של טווח זריקה, v² × sin(2θ) / g. הגרף מראה את המסלולים:Throw a ball at an angle: the initial velocity is a vector whose length is the speed and whose direction is the angle (Vector2.from_polar). Here we run the simulation with two dt values and compare to the exact range formula, v² × sin(2θ) / g. The chart shows the paths:
אנלוגיהAnalogy
dt הוא כמו מספר התמונות בסרטון האטה: מעט תמונות = קפיצות ושגיאות, הרבה תמונות = תנועה חלקה ומדויקת.dt is like the frame count in a slow-motion video: few frames = jumps and errors, many frames = smooth, accurate motion.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 4
קפיץSpring
קפיץ וריסוןA spring and damping
חוק הוק: F = -k × מתיחהHooke's law: F = -k × stretch
קפיץ מושך חזרה בכוח שגדל ככל שמותחים אותו: F = -k × (מרחק - אורך מנוחה). בלי חיכוך הוא מתנדנד לנצח. ריסון קטן, כפל של המהירות ב-0.98 בכל צעד, גורם לתנודה לדעוך כמו במציאות. אותו קוד מזיז מצלמה "רכה" במשחקים:A spring pulls back with a force that grows the more you stretch it: F = -k × (distance - rest length). Without friction it oscillates forever. A little damping, multiplying velocity by 0.98 each step, makes the oscillation die down like in reality. The same code moves a "soft" camera in games:
אנלוגיהAnalogy
ריסון הוא כמו נדנדה בגן: אם אף אחד לא דוחף, היא מתנדנדת פחות ופחות עד שהיא נעצרת.Damping is like a playground swing: if nobody pushes, it swings less and less until it stops.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 5
מסלולOrbit
מסלול כוכב לכתA planet's orbit
כבידה בין שני גופים: G·M / r²Gravity between two bodies: G·M / r²
כבידה בין שני גופים חלשה עם המרחק: G·M / r², וכיוונה אל השמש. אם נותנים לכוכב הלכת מהירות צידה בדיוק נכונה, הוא לא נופל ולא בורח, אלא מקיף. כאן מציירים את המסלול בחלון pygame. מדד טוב לבדיקת סימולציה: האנרגיה הכוללת צריכה להישאר כמעט קבועה:Gravity between two bodies weakens with distance: G·M / r², pointing toward the sun. Give the planet exactly the right sideways speed and it neither falls in nor escapes, but orbits. Here we draw the orbit in a pygame window. A good measure for checking a simulation: total energy should stay nearly constant:
אנלוגיהAnalogy
מסלול הוא כמו לזרוק כדור כל כך חזק לצד, שבזמן שהוא נופל, האדמה מתעקלת מתחתיו, והוא אף פעם לא מגיע אליה.An orbit is like throwing a ball so hard sideways that while it falls, the Earth curves away beneath it, and it never lands.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 6
מעבדהLab
Physics Lab: מעבדה בחלוןPhysics Lab: a lab in a window
כדורים עם כבידה, קירות והתנגשויותBalls with gravity, walls and collisions
צבע לפי מהירותColour by speed
מוני אנרגיה על המסךEnergy readouts on screen
המעבדה מחברת הכול: עשרות כדורים עם כבידה, ריסון קל, הקפצה מהקירות (מהמפגש הראשון של חלק זה), והתנגשויות בין כדורים (האתגר של מפגש 37). כל כדור נצבע לפי המהירות שלו, מכחול איטי לאדום מהיר, ובפינה מוצגת האנרגיה הכוללת:The lab ties it all together: dozens of balls with gravity, light damping, wall bounces (from this part's first session), and ball-to-ball collisions (session 37's challenge). Each ball is coloured by its speed, from slow blue to fast red, and the total energy is shown in the corner:
אנלוגיהAnalogy
המעבדה היא כמו אקווריום: קובעים חוקים פשוטים לכל דג, ומקבלים התנהגות קבוצתית מורכבת שאף אחד לא תכנת במפורש.The lab is like an aquarium: set simple rules for each fish, and get complex group behaviour nobody programmed directly.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.