在Processing/box2D中更改电机的速度

在Processing/box2D中更改电机的速度,processing,box2d,Processing,Box2d,我一直在使用processing和box2D制作一些简单的游戏,将代码的性质作为一种资源 我的问题是,我已经到了一个临界点,这些风车根据电机的速度顺时针/逆时针运转(我使用的是PI/2和-PI*2)。我想让它,以便用户可以改变这个速度从积极和消极的按下一个键或鼠标按钮。环顾四周,人们和box2D文档都说要使用函数void SetMotorSpeed(浮动速度),但是我不知道如何实现这一点。我尝试了一些我能思考的方法,但没有运气 目前,我的主文件中有以下内容(“s”是风车实例的名称): 我在风车的

我一直在使用processing和box2D制作一些简单的游戏,将代码的性质作为一种资源

我的问题是,我已经到了一个临界点,这些风车根据电机的速度顺时针/逆时针运转(我使用的是
PI/2
-PI*2
)。我想让它,以便用户可以改变这个速度从积极和消极的按下一个键或鼠标按钮。环顾四周,人们和box2D文档都说要使用函数
void SetMotorSpeed(浮动速度),但是我不知道如何实现这一点。我尝试了一些我能思考的方法,但没有运气

目前,我的主文件中有以下内容(“s”是风车实例的名称):

我在风车的档案里有这个:

// Click the mouse to switch speed of motor
void mousePressed() {
    s.SetMotorSpeed(s.speed);
}   
//Set Motor Speed
void SetMotorSpeed(float speed){
     speed = speed * -1;
}
class Seesaw {

    // object is two boxes and one joint
    
    RevoluteJoint joint;
    // float speed = PI*2;
    Box box1;
    Box box2;
    float speed = PI*2;

    Seesaw(float x, float y) {
        // Initialize locations of two boxes
        box1 = new Box(x, y-20, 120, 10, false); 
        box2 = new Box(x, y, 10, 40, true); 

        // Define joint as between two bodies
        RevoluteJointDef rjd = new RevoluteJointDef();
        Vec2 offset = box2d.vectorPixelsToWorld(new Vec2(0, 60));
        rjd.initialize(box1.body, box2.body, box1.body.getWorldCenter());

        // Turning on a motor (optional)
        rjd.motorSpeed = PI*2;       // how fast?
        rjd.maxMotorTorque = 1000.0; // how powerful?
        rjd.enableMotor = true;      // is it on?
        // Create joint
        joint = (RevoluteJoint) box2d.world.createJoint(rjd);
    }

    // Turn the motor on or off
    void toggleMotor() {
        joint.enableMotor(!joint.isMotorEnabled());
    }

    boolean motorOn() {
        return joint.isMotorEnabled();
    }
    
    void SetMotorSpeed(float speed){
        speed = -speed;
    }

    void display() {
        box2.display();
        box1.display();

        // Draw anchor just for debug
        Vec2 anchor = box2d.coordWorldToPixels(box1.body.getWorldCenter());
        fill(255, 0, 0);
        stroke(0);
        ellipse(anchor.x, anchor.y, 4, 4);
    }
}
但这不起作用

我对编码相当陌生,这是我第一篇关于堆栈溢出的文章,所以如果我在提问或提出这个问题时做错了什么,我深表歉意。我愿意接受关于规范和礼仪方面的建议

以下是整个风车的代码:

// Click the mouse to switch speed of motor
void mousePressed() {
    s.SetMotorSpeed(s.speed);
}   
//Set Motor Speed
void SetMotorSpeed(float speed){
     speed = speed * -1;
}
class Seesaw {

    // object is two boxes and one joint
    
    RevoluteJoint joint;
    // float speed = PI*2;
    Box box1;
    Box box2;
    float speed = PI*2;

    Seesaw(float x, float y) {
        // Initialize locations of two boxes
        box1 = new Box(x, y-20, 120, 10, false); 
        box2 = new Box(x, y, 10, 40, true); 

        // Define joint as between two bodies
        RevoluteJointDef rjd = new RevoluteJointDef();
        Vec2 offset = box2d.vectorPixelsToWorld(new Vec2(0, 60));
        rjd.initialize(box1.body, box2.body, box1.body.getWorldCenter());

        // Turning on a motor (optional)
        rjd.motorSpeed = PI*2;       // how fast?
        rjd.maxMotorTorque = 1000.0; // how powerful?
        rjd.enableMotor = true;      // is it on?
        // Create joint
        joint = (RevoluteJoint) box2d.world.createJoint(rjd);
    }

    // Turn the motor on or off
    void toggleMotor() {
        joint.enableMotor(!joint.isMotorEnabled());
    }

    boolean motorOn() {
        return joint.isMotorEnabled();
    }
    
    void SetMotorSpeed(float speed){
        speed = -speed;
    }

    void display() {
        box2.display();
        box1.display();

        // Draw anchor just for debug
        Vec2 anchor = box2d.coordWorldToPixels(box1.body.getWorldCenter());
        fill(255, 0, 0);
        stroke(0);
        ellipse(anchor.x, anchor.y, 4, 4);
    }
}

应将速度变化告知接头。试试这个:

void SetMotorSpeed(float speed) {
     s.joint.setMotorSpeed(speed); // edited, processing/java uses camel-case
}
在命名变量时,您可能还需要更加小心。在最初的帖子中,您对局部变量和成员变量使用了相同的名称,这并没有想要的效果。大多数人对成员变量使用一些命名约定来避免这种非常常见的错误。例如,让所有成员变量都以“\u1”开头


你能给我们举个有效的例子吗?当我们将其复制到IDE中时,它会显示您的意思。理想情况下,需要最少的代码来避免这篇文章和我们的思想混乱。顺便说一句,欢迎来到SO!你的帖子很好,但是缺少关于代码本身的信息,因为你的实现可以是关于任何东西的,我们无法猜测(我的意思是,我可以在代码的本质上追求它,但它会让我代替你工作)。太棒了,谢谢你的回复!我编辑了这个问题以包含跷跷板对象的代码。不确定这是否足以继续下去,请告诉我。我不想只是把整个项目粘贴到那里,然后把它变成一个巨大的帖子,除非我应该这么做。稍微调整一下,这就成功了。!我试图使用rjd.SetMotorSpeed(),而我本应该使用joint.SetMotorSpeed()。我还不太明白为什么在构造器中你要用rjd设置速度。但若要改变它,你们可以使用关节,但至少它是有效的,我可以从中学习。我使用了你的建议,但仍然在努力,因为函数的第一个字母是大写的。当我第一次开始研究这个时,我觉得这很奇怪,但是文档中甚至有SetMotorSpeed(),但它是SetMotorSpeed()。感谢大家的建议/评论/编辑!您是正确的,它应该是小写的,因为它在Java中更常见,如下所述:。cpp中的原始box2d使用大写,我查看的文档也是如此。下面是更改rjd产生效果的原因:在构造函数中,关节定义被传递给世界对象以创建关节。世界对象不保留对关节定义的引用,而是将所有对象复制到其内部结构。这正是box2d的设计方式。你的想法其实还不错,其他图书馆也会这么做。