התנגשויות ו-spritesCollisions & sprites
מלבנים ועיגולים שמתנגשים, sprites מתמונות, אנימציה מפריימים וניקוד.
Rectangles and circles that collide, sprites from images, frame animation and scoring.
01המונחים של המפגשThis session's terms
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.
Before the code, the words. Every term here is also linked in the course glossary.
- AABB: התנגשות בין שני מלבנים ישרים: הם חופפים אם הם חופפים גם בציר x וגם בציר y.
Rectangle.intersectsעושה בדיוק את זה.AABB: A collision between two upright rectangles: they overlap if they overlap on both the x axis and the y axis.Rectangle.intersectsdoes exactly that. - sprite: תמונה קטנה של דמות או עצם במשחק. נטענת עם
ImageIO.readומצוירת עםg.drawImage. sprite sheet: הרבה פריימים בתמונה אחת, ברשת.sprite: A small picture of a character or object in a game. Loaded withImageIO.readand drawn withg.drawImage. A sprite sheet: many frames in one picture, on a grid. - אנימציה מפריימים: מחליפים פריים כל כמה מאיות שנייה:
frame = (int) (time / frameSeconds) % frames.frame animation: Switch the frame every few hundredths of a second:frame = (int) (time / frameSeconds) % frames. - broad phase: שלב סינון לפני בדיקת התנגשות מדויקת: מחלקים את העולם לתאים ובודקים רק עצמים באותו תא, במקום כל זוג.broad phase: A filtering step before exact collision checks: split the world into cells and check only objects in the same cell, instead of every pair.
02התנגשות, מצוירת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.
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class Main {
public static void main(String[] args) throws Exception {
BufferedImage img = new BufferedImage(400, 200, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(new Color(0xF4F1EA));
g.fillRect(0, 0, 400, 200);
Rectangle player = new Rectangle(160, 150, 80, 20);
Rectangle[] stars = {new Rectangle(60, 40, 20, 20), new Rectangle(190, 140, 20, 20), new Rectangle(320, 90, 20, 20)};
g.setColor(new Color(0x2D5BD0));
g.fill(player);
int hits = 0;
for (Rectangle s : stars) {
boolean hit = s.intersects(player);
if (hit) {
hits++;
}
g.setColor(new Color(hit ? 0xD7263D : 0xF08C00));
g.fillOval(s.x, s.y, s.width, s.height);
}
g.setColor(new Color(0x1B1F2A));
g.drawString("hits: " + hits + " (red = touching the paddle)", 12, 20);
g.dispose();
ImageIO.write(img, "png", new File("frame.png"));
System.out.println("hits " + hits);
}
}hits 1
03AABB בלי ספריות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.
public class Main {
public static void main(String[] args) {
Box player = new Box(10, 10, 20, 20);
Box star = new Box(25, 25, 10, 10);
Box far = new Box(50, 10, 10, 10);
System.out.println(player.intersects(star) + " " + player.intersects(far));
}
}
record Box(int x, int y, int w, int h) {
boolean intersects(Box o) {
return x < o.x + o.w && o.x < x + w && y < o.y + o.h && o.y < y + h;
}
}true false
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
04איזה פריים עכשיו?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.
public class Main {
static int frame(double time, double frameSeconds, int frames) {
return (int) (time / frameSeconds) % frames;
}
public static void main(String[] args) {
for (double t = 0; t < 1.0; t += 0.25) {
System.out.print(frame(t, 0.1, 4) + " ");
}
System.out.println();
}
}0 2 1 3
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
05הצעד של היום בפרויקט מדףToday's step in the Shelf project
שלושה כוכבים נופלים 10 בכל טיק. כוכב שנוגע במשוט (40,90,30,5) נותן 10 נקודות ונעלם; כוכב שעבר את 100 נחשב פספוס ונעלם. אחרי 12 טיקים: score 20, missed 1, left 0.
Three stars fall 10 per tick. A star touching the paddle (40,90,30,5) gives 10 points and disappears; a star past 100 counts as missed and disappears. After 12 ticks: score 20, missed 1, left 0.
את הצעד הזה כותבים בעמוד התרגול (התרגיל המסומן כצעד בפרויקט). בעמוד הפתרונות יש פתרון מלא עם סימולציה שמריצה אותו.
You write this step on the practice page (the exercise marked as a project step). The solutions page has a full solution with a simulation that runs it.
סיימתם את ההרצאה!Lecture complete!
עכשיו המצגת לחזרה מהירה, או ישר לתרגול.
Next: the slides for a quick review, or straight to practice.