# Rect 本遊戲核心將物件的碰撞檢定區域設定為一個矩形,以AABB碰撞作為遊戲物件間的碰撞檢定,由於遊戲設計過程中經常會使用矩形來進行邏輯運算或是畫面渲染,因此獨立出一個類別專門處理與矩形相關的問題。 *** 以下為Rect程式碼說明: ```java= //(x,y)作為矩形左上角生成(寬為width ,高為height)的矩形 public Rect(final double x, final double y, final double width, final double height) { this.position = new Vector.Builder(x, y).gen(); this.width = width; this.height = height; } public Rect(final Vector position, final double width, final double height) { this.position = position; this.width = width; this.height = height; } public Rect(final Rect rect) { this.position = new Vector.Builder(rect.left(), rect.top()).gen(); this.width = rect.width(); this.height = rect.height(); } //以中心點(x,y開始生成) public static Rect genWithCenter(final double x, final double y, final double width, final double height) { return new Rect(x - width / 2, y - height / 2, width, height); } ``` 2.外部可取得的Rect資料 | function | 功能 | 回傳資料型態 | |:----------:| -------------------- | --- | | position() | 取得該矩形左上點座標 | Vector | | left() | 取得矩形左側座標位置 | double | | top() | 取得矩形上方座標位置 | double | | right() | 取得矩形右側座標位置 | double | | bottom() | 取得矩形下方座標位置 | double | | intLeft() | 取得矩形左側座標位置(整數) | int | | intTop() | 取得矩形上方座標位置(整數) | int | | intRight() | 取得矩形右側座標位置(整數) | int | | intBottom() | 取得矩形下方座標位置(整數) | int | | width() | 取得矩形的寬 | double | | height() | 取得矩形的高 | double | | intWidth() | 取得矩形的寬(整數) | int | | intHeight() | 取得矩形的高(整數) | int | | centerX() | 取得矩形x軸的中心點位置 | double | | centerY() | 取得矩形y軸的中心點位置 | double | | intCenterX() | 取得矩形x軸的中心點位置(整數) | int | | intCenterY() | 取得矩形y軸的中心點位置(整數) | int | 3.設定Rect的位置 | function | 功能 | |:---------------------------:| ------------------------------ | | offset(Vector vector) | 移動Rect位置(根據輸入向量移動) | | offset(double x, double y) | 移動Rect位置(水平方向移動x,垂直方向移動y) |offsetX(double x)| 以水平方向移動Rect位置(位移量為x) |offsetY(double x)| 以垂直方向移動Rect位置(位移量為y) |setLeft(double left)| 將Rect左側設定在left |setTop(double top)| 將Rect上方設定在top |setRight(double right)| 將Rect右側設定在right |setBottom(double bottom)| 將Rect下方設定在bottom |setXY(double x, double y)| 將Rect設定在(x,y) |setX(double x)| 將Rect的x座標設定在x |setY(double y)| 將Rect的y座標設定在y