使用P2物理引擎制作物理小球-創(chuàng)新互聯(lián)

今天分享的內(nèi)容是:基于Egret使用P2物理引擎實(shí)現(xiàn)物理小球示例效果。

石鼓網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),石鼓網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為石鼓成百上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的石鼓做網(wǎng)站的公司定做!

了解更多信息,您可以查看P2物理引擎GitHub地址或者是EgretP2物理系統(tǒng)文檔。

* 第三方庫(kù)的引入
* 創(chuàng)建一個(gè)P2物理項(xiàng)目

一、第三方庫(kù)的引入

1.首先新建一個(gè)項(xiàng)目。

2.在GitHub上下載包括P2物理引擎庫(kù)的完整第三方庫(kù),解壓后按照路徑找到physics模塊。

使用P2物理引擎制作物理小球

3.將physics模塊放到新建項(xiàng)目根目錄的同級(jí)目錄。

4.修改egretProperties.json,modules數(shù)組里增加

{
"name":"physics",
"path":"../physics"
}

5.然后找到插件-Egret項(xiàng)目工具-編譯引擎編譯一下就成功引入P2庫(kù),如下圖。

使用P2物理引擎制作物理小球

二、創(chuàng)建一個(gè)P2物理項(xiàng)目

使用P2物理引擎創(chuàng)建物理應(yīng)用的過(guò)程大致分為5個(gè)步驟:

1.創(chuàng)建world世界
2.創(chuàng)建shape形狀
3.創(chuàng)建body剛體
4.實(shí)時(shí)調(diào)用step()函數(shù),更新物理模擬計(jì)算
5.基于形狀、剛體,使用Egret渲染,顯示物理模擬效果

**下面根據(jù)這5個(gè)步驟進(jìn)行代碼構(gòu)建。

1.打開(kāi)Main.ts,首先創(chuàng)建world世界**

//創(chuàng)建Word世界
private world:p2.World;
private CreateWorld(){
    this.world = new p2.World();
    //設(shè)置world為睡眠狀態(tài)
    this.world.sleepMode = p2.World.BODY_SLEEPING;
    this.world.gravity = [0,1]
}

gravity是一個(gè)Vector2向量對(duì)象,表示world世界中重力加速度,默認(rèn)為垂直向上的向量[0,-9.81],將gravity設(shè)置為[0,0]可以取消重力;gravity的x分量也是有意義的,將其設(shè)置為一個(gè)非0數(shù)值后,重力就會(huì)朝向量[x,y]方向。

2.創(chuàng)建地板Plane

//生成地板Plane
private planeBody:p2.Body;
private CreatePlane(){
    //創(chuàng)建一個(gè)shape形狀
    let planeShape:p2.Plane = new p2.Plane();
    //創(chuàng)建body剛體
    this.planeBody= new p2.Body({
        //剛體類型
        type:p2.Body.STATIC,
        //剛體的位置
        position:[0,this.stage.stageHeight]
    });
    this.planeBody.angle = Math.PI;
    this.planeBody.displays = [];
    this.planeBody.addShape(planeShape);
    this.world.addBody(this.planeBody);
}

Plane相當(dāng)于地面,默認(rèn)面向Y軸方向。 因?yàn)檫@個(gè)Y軸是P2的Y軸,而不是Egret的Y軸。P2和Egret的Y軸是相反的。所以將地面翻轉(zhuǎn)180度。
planeBody.angle = Math.PI

3.點(diǎn)擊創(chuàng)建足球或者矩形方塊

private shpeBody:p2.Body;
//貼圖顯示對(duì)象
private display:egret.DisplayObject;
private onButtonClick(e:egret.TouchEvent) {
    if(Math.random() >0.5){
        //添加方形剛體 
        var boxShape:p2.Shape = new p2.Box({width:140 ,height:80});
        this.shpeBody = new p2.Body({ mass: 1, position: [e.stageX, e.stageY], angularVelocity: 1});
        this.shpeBody.addShape(boxShape);
        this.world.addBody(this.shpeBody);
        this. display= this.createBitmapByName("rect_png");
        this.display.width = (<p2.Box>boxShape).width 
        this.display.height = (<p2.Box>boxShape).height                
    }
    else{
        //添加圓形剛體
        var circleShape:p2.Shape = new p2.Circle({radius:60});
        this.shpeBody = new p2.Body({ mass: 1, position: [e.stageX, e.stageY]});
        this.shpeBody.addShape(circleShape);
        this.world.addBody(this.shpeBody);
        this.display = this.createBitmapByName("circle_png");
        this.display.width = (<p2.Circle>circleShape).radius * 2 
        this.display.height = (<p2.Circle>circleShape).radius * 2
    }
        this.display.anchorOffsetX = this.display.width / 2
        this.display.anchorOffsetY = this.display.height / 2;
        this.display.x = -100;
        this.display.y = -100;
        this.display.rotation = 270
        this.shpeBody.displays = [this.display];
        this.addChild(this.display);    
}

上述代碼中先創(chuàng)建Box或者Circle形狀,并通過(guò)addShape()函數(shù),將其添加到剛體body中,最后通過(guò)world的addBody()將剛體添加到世界中,完成一個(gè)P2物理應(yīng)用創(chuàng)建。 注意:Egret中加載進(jìn)來(lái)的圖像,其原點(diǎn)默認(rèn)為左上角,而P2中剛體的原點(diǎn)處于其中心位置,如下圖(盜了一張圖)

使用P2物理引擎制作物理小球

所以需要根據(jù)剛體重心坐標(biāo)偏移量(offsetX,offsetY)設(shè)置圖像的anchorOffsetX ,anchorOffsetY 屬性。

4.幀函數(shù)實(shí)時(shí)調(diào)用step()函數(shù)

//幀事件,步函數(shù)
private update() {
    this.world.step(2.5);
    var l = this.world.bodies.length;
    for (var i:number = 0; i < l; i++) {
        var boxBody:p2.Body = this.world.bodies[i];
        var box:egret.DisplayObject = boxBody.displays[0];
        if (box) {
            //將剛體的坐標(biāo)和角度賦值給顯示對(duì)象
            box.x = boxBody.position[0];
            box.y = boxBody.position[1];
            box.rotation = boxBody.angle * 180 / Math.PI;
            //如果剛體當(dāng)前狀態(tài)為睡眠狀態(tài),將圖片alpha設(shè)為0.5,否則為1
            if (boxBody.sleepState == p2.Body.SLEEPING) {
                box.alpha = 0.5;
            }
            else {
                box.alpha = 1;
            }
        }
    }
}

world中所有的剛體都保存在屬性bodies數(shù)組中,通過(guò)數(shù)組的foreach()方法,可以遍歷其中的每一個(gè)body,然后拿到body的顯示對(duì)象,再將剛體的坐標(biāo)和角度屬性賦值給顯示對(duì)象,實(shí)時(shí)更新即可。

5.在createGameScene()中依次調(diào)用

protected createGameScene(): void {
    let img:egret.Bitmap = new egret.Bitmap();
    img = this.createBitmapByName("bg_jpg");
    img.width = this.stage.stageWidth;
    img.height = this.stage.stageHeight;
    this.addChild(img);
    this.CreateWorld();
    this.CreatePlane();

    this.addEventListener(egret.Event.ENTER_FRAME,this.update,this);
    this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.onButtonClick,this);
}

本教程所涉及的內(nèi)容,只是對(duì)P2物理引擎的初級(jí)了解和使用。然而物理引擎需要我們學(xué)習(xí)的知識(shí)還有很多,還有更強(qiáng)大更好玩的功能等待我們?nèi)ヌ剿鳎?/p>

附上GitHub源碼地址:https://github.com/duan003387...

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

標(biāo)題名稱:使用P2物理引擎制作物理小球-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://bm7419.com/article34/dssise.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站制作、建站公司商城網(wǎng)站、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)