zoukankan      html  css  js  c++  java
  • Torque2D MIT 脚本阅读(4) ChainToy

    创建几条带链球的锁链,并模拟之间的物理碰撞.

    代码片段

      创建一条锁链需要分四步:

      1. 创建一个静态对象作为整条链子的锚点,这个对象是固定不动的     

    // 创建一个精灵
    %fixedObject = new Sprite();
    // 设置为静态固定类型
    %fixedObject.BodyType = static;
    // 图像资源
    %fixedObject.Image = "ToyAssets:chain";
    // 位置设定
    %fixedObject.setPosition( %posX, %posY );
    // 尺寸
    %fixedObject.setSize( %linkWidth, %linkHeight );
    // 加入场景
    SandboxScene.add( %fixedObject );
    

       2. 创建一条链子,整条链接链由数量众多的小对象组成

        // 接上面的代码,因为设置链接的时候需要用到两个相连接的对象
        %lastLinkObj = %fixedObject;
        // 创建链条节点
        for ( %n = 1; %n <= %this.ChainLinks; %n++ )
        {
            // Create the link object.
            %obj = new Sprite();
            %obj.setImage( "ToyAssets:chain" );
            %obj.setPosition( %posX, %posY - (%n*%linkHeight) );
            %obj.setSize( %linkWidth, %linkHeight );
            %obj.setDefaultDensity( 20 );  // 碰撞回复系数
            %obj.setDefaultFriction( 0.2 ); // 摩擦力系数       
            %obj.createPolygonBoxCollisionShape( %linkWidth, %linkHeight ); // 碰撞边界创建
            %obj.setAngularDamping( 0.1 ); // 角度阻尼
            %obj.setLinearDamping( 0.1 ); // 线性阻尼
            SandboxScene.add( %obj );   
    
            // 与上一个节点创建连接关系
            SandboxScene.createRevoluteJoint( %lastLinkObj, %obj, 0, -%halfLinkHeight, 0, %halfLinkHeight, false );
    
            // 标记为上一个节点,为下次连接做准备
            %lastLinkObj = %obj;
        }    
    

       3. 创建链球,作为重力体

        // Create the weight.
        %weight = new Sprite();
        %weight.setImage( "ToyAssets:whitesphere" );
        %weight.BlendColor = DarkGreen;
        %weight.setSize( %weightSize );
        %weight.setPosition( %posX, %posY - %pivotDistance - %weightHalfSize );
        %weight.setDefaultFriction( 0.2 );
        %weight.setDefaultDensity( 1 );
        %weight.createCircleCollisionShape( %weightHalfSize );
        SandboxScene.add( %weight );
    
        // Create a revolute joint from the last link to the weight.
        SandboxScene.createRevoluteJoint( %lastLinkObj, %weight, 0, -%halfLinkHeight, 0, %halfLinkHeight, false );
    

       4. 设定链子的作用范围

        // If the chain limit is on then create a rope join from the fixed pivot to the weight.
        if ( %this.ChainLimit )
            SandboxScene.createRopeJoint(%fixedObject, %weight, 0, 0, 0, %weightHalfSize, %pivotDistance, false);
    

     细节说明

      这个例子中有两个重要的新方法,分别是:  

      转动关节创建方法.

    ConsoleMethod(Scene, createRevoluteJoint, S32, 4, 9,    "(sceneObjectA, sceneObjectB, [localAnchorA X/Y], [localAnchorB X/Y], [collideConnected]) Creates a revolute joint.\n"
                                                                    "@param sceneObjectA The first scene object to connect to the joint.  Use an empty string to indicate the Scene ground body.\n"
                                                                    "@param sceneObjectB The second scene object to connect to the joint.  Use an empty string to indicate the Scene ground body.\n"
                                                                    "@param localAnchorA The local point of the first scene object where the joint connects.\n"
                                                                    "@param localAnchorB The local point of the second scene object where the joint connects.\n"
                                                                    "@param collideConnected Whether the scene objects can collide with each other while connected with this joint.\n"
                                                                    "@return The joint Id (-1 if error).")
    

       绳关节创建方法.

    ConsoleMethod(Scene, createRopeJoint, S32, 4, 10,       "(sceneObjectA, sceneObjectB, [localAnchorA X/Y], [localAnchorB X/Y], [maxLength], [collideConnected]) Creates a rope joint.\n"
                                                                    "@param sceneObjectA The first scene object to connect to the joint.  Use an empty string to indicate the Scene ground body.\n"
                                                                    "@param sceneObjectB The second scene object to connect to the joint.  Use an empty string to indicate the Scene ground body.\n"
                                                                    "@param localAnchorA The local point of the first scene object where the joint connects.\n"
                                                                    "@param localAnchorB The local point of the second scene object where the joint connects.\n"
                                                                    "@param maxLength The maximum rigid length of the rope.\n"
                                                                    "@param collideConnected Whether the scene objects can collide with each other while connected with this joint.\n"
                                                                    "@return The joint Id (-1 if error).")
    

     效果图:

  • 相关阅读:
    红楼【建筑位置】
    红楼【人物关系】
    jenkins【shared-libraries】
    linux 安装mysql8.0 tar.xz
    MySQL 输入字符串对日期进行模糊查询
    解决kali linux 和 win10 双系统时间不一致问题
    linux idea桌面图标
    linux卸载openjdk11
    tar.xz解压
    pandas模块高性能使用方法总结
  • 原文地址:https://www.cnblogs.com/KevinYuen/p/2945591.html
Copyright © 2011-2022 走看看