נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
JavaJava

התנגשויות ו-spritesCollisions & sprites

מפגש 45: מלבנים ועיגולים שמתנגשים, sprites מתמונות, אנימציה מפריימים וניקוד.Session 45: Rectangles and circles that collide, sprites from images, frame animation and scoring.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • התנגשות, מצוירתA collision, drawn
  • AABB בלי ספריותAABB with no libraries
  • איזה פריים עכשיו?Which frame now?
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגשThis session's terms

  • AABB: התנגשות בין שני מלבנים ישרים:AABB: A collision between two upright rectangles:
  • sprite: תמונה קטנה של דמות או עצם במשחק.sprite: A small picture of a character or object in a game.
  • אנימציה מפריימים: מחליפים פריים כל כמה מאיות שנייה:frame animation: Switch the frame every few hundredths of a second:
  • broad phase: שלב סינון לפני בדיקת התנגשות מדויקת:broad phase: A filtering step before exact collision checks:
נושאTopic 2

התנגשות, מצוירתA collision, drawn

התנגשות, מצוירתA collision, drawn

  • Rectangle.intersects בודק חפיפה.Rectangle.intersects checks for overlap.
  • הכוכב שנוגע במשוט מצויר באדום.The star touching the paddle is drawn in red.
  • התמונה נוצרה באמת בזמן בניית האתר.The picture was really made while building the site.
נושאTopic 3

AABB בלי ספריותAABB with no libraries

AABB בלי ספריותAABB with no libraries

  • שני מלבנים חופפים אם כל אחד מתחיל לפני שהשני נגמר, בשני הצירים.Two rectangles overlap if each starts before the other ends, on both axes.
  • בסימולציה רואים את ארבע ההשוואות.In the simulation you see the four comparisons.
נושאTopic 4

איזה פריים עכשיו?Which frame now?

איזה פריים עכשיו?Which frame now?

  • 4 פריימים, כל אחד 0.1 שנייה.4 frames, each 0.1 seconds.
  • הזמן קובע את הפריים, לא מספר הסיבובים של הלולאה, כך שהאנימציה באותה מהירות בכל מחשב.The time decides the frame, not the number of loop rounds, so the animation runs at the same speed on every computer.
נושאTopic 5

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

  • משחק 3: לתפוס כוכביםGame 3: catch the stars

AABB: התנגשות בין שני מלבנים ישרים:AABB: A collision between two upright rectangles:

sprite: תמונה קטנה של דמות או עצם במשחק.sprite: A small picture of a character or object in a game.

אנימציה מפריימים: מחליפים פריים כל כמה מאיות שנייה:frame animation: Switch the frame every few hundredths of a second:

broad phase: שלב סינון לפני בדיקת התנגשות מדויקת:broad phase: A filtering step before exact collision checks:

Rectangle.intersects בודק חפיפה.Rectangle.intersects checks for overlap.

הכוכב שנוגע במשוט מצויר באדום.The star touching the paddle is drawn in red.

התמונה נוצרה באמת בזמן בניית האתר.The picture was really made while building the site.

שני מלבנים חופפים אם כל אחד מתחיל לפני שהשני נגמר, בשני הצירים.Two rectangles overlap if each starts before the other ends, on both axes.

בסימולציה רואים את ארבע ההשוואות.In the simulation you see the four comparisons.

4 פריימים, כל אחד 0.1 שנייה.4 frames, each 0.1 seconds.

הזמן קובע את הפריים, לא מספר הסיבובים של הלולאה, כך שהאנימציה באותה מהירות בכל מחשב.The time decides the frame, not the number of loop rounds, so the animation runs at the same speed on every computer.

משחק 3: לתפוס כוכביםGame 3: catch the stars

שני מלבנים חופפים בציר x אבל לא בציר y. מתנגשים?Two rectangles overlap on x but not on y. Do they collide?

  • לא: צריך חפיפה בשני הציריםNo: they must overlap on both axes
  • כןYes
  • תלוי בגודלIt depends on size
  • רק אם הם ריבועיםOnly if they're squares

לא: צריך חפיפה בשני הציריםNo: they must overlap on both axes

למה משווים dx*dx + dy*dy ל-r*r ולא מחשבים שורש?Why compare dx*dx + dy*dy with r*r instead of taking a square root?

  • זה נותן אותה תשובה ובלי שורש איטיIt gives the same answer without a slow square root
  • כי שורש לא קיים ב-JavaBecause Java has no square root
  • זה מדויק יותרIt's more exact
  • סתם הרגלJust habit

זה נותן אותה תשובה ובלי שורש איטיIt gives the same answer without a slow square root

מוחקים מרשימה בזמן לולאה עם אינדקס. מאיזה כיוון כדאי לרוץ?Removing from a list in an index loop. Which direction should you run?

  • מהסוף להתחלהFrom the end to the start
  • מההתחלה לסוףFrom the start to the end
  • לא משנהDoesn't matter
  • אסור למחוקYou can't remove

מהסוף להתחלהFrom the end to the start

מלבניםRectangles

  • עם Box.intersects מהתזכורת:With Box.intersects from the refresher:

עיגוליםCircles

  • circles(x1, y1, r1, x2, y2, r2):circles(x1, y1, r1, x2, y2, r2):

פריים לפי זמןA frame by time

  • 3 פריימים של 0.2 שנייה.3 frames of 0.2 seconds.

צייד באגים: נגיעה בקצהBug hunter: touching at the edge

  • שני מלבנים שרק נוגעים בקצה (אחד נגמר ב-10 והשני מתחיל ב-10) נחשבים כאן התנגשות, כי הבדיקה משתמשת ב-<=.Two rectangles that only touch at the edge (one ends at 10, the other starts at 10) count as a collision here, because the check uses <=.

משחק 3: לתפוס כוכביםGame 3: catch the stars

  • שלושה כוכבים נופלים 10 בכל טיק.Three stars fall 10 per tick.

תא ב-sprite sheetA cell in a sprite sheet

  • sprite sheet עם 4 עמודות של 32 פיקסלים.A sprite sheet with 4 columns of 32 pixels.

רשת חוסכת בדיקותA grid saves checks

  • 5 עצמים.5 objects.

השחקן נגמר ב-x=30; (30,30) מתחיל בדיוק שם, ולכן לא חופף.The player ends at x=30; (30,30) starts exactly there, so no overlap.

בלי Math.sqrt: משווים ריבועים.No Math.sqrt: compare squares.

(int) (time / frameSeconds) % frames(int) (time / frameSeconds) % frames

מה למדנוWhat we learned

  • AABB: התנגשות בין שני מלבנים ישרים:AABB: A collision between two upright rectangles:
  • Rectangle.intersects בודק חפיפה.Rectangle.intersects checks for overlap.
  • שני מלבנים חופפים אם כל אחד מתחיל לפני שהשני נגמר, בשני הצירים.Two rectangles overlap if each starts before the other ends, on both axes.
  • 4 פריימים, כל אחד 0.1 שנייה.4 frames, each 0.1 seconds.
  • משחק 3: לתפוס כוכביםGame 3: catch the stars

עכשיו לתרגולNow, practice

5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

נבנה על ידיBuilt by Ariel Yohimovich