简体   繁体   English

libgdx在朝向矩形的方向上施加力

[英]libgdx apply force in direction rectangle is facing

I'm just starting out with box2d an I'm trying to make a rocket(rectangle for now) fly, it just stands vertically when the game starts and I want to apply force from the bottom part of the rectangle, because that's where the engine would be, and also in the direction the rectangle is facing. 我只是从box2d开始,我正在尝试使火箭(现在为矩形)飞行,当游戏开始时它只是垂直站立,我想从矩形的底部施加力,因为那是引擎也将是矩形引擎,并且也将朝向矩形。 I tried doing this 我尝试这样做

    body.applyLinearImpulse(getUserData().getBoosterLinearImpulse(), new Vector2(body.getWorldCenter().x, body.getWorldCenter().y - Constants.ROCKET_HEIGHT), true);

This is a method that gets executed when the bottom right of the screen is pressed. 当按下屏幕右下角时,将执行此方法。 It only works when the rectangle is standing still. 它仅在矩形静止不动时起作用。 I obviously don't really know what I'm doing. 我显然不知道自己在做什么。 I also have another question: what's the difference between applyLineairForce and applyForce, and how do I best learn to work with box2d as I don't find it easy (which is not a problem)? 我还有一个问题:applyLineairForce和applyForce有什么区别?如何最好地学习与box2d一起工作,因为我觉得这并不容易(这不是问题)?

If I'm understanding this right you want to know the difference between a applyLinearImpulse and a applyForce. 如果我了解这项权利,则想知道applyLinearImpulse和applyForce之间的区别。 An impulse is a one-off application of force which would generally be used for jumps in a game whereas a force would be something applied every frame say to increase the speed of a car. 冲动是一次施加的力,通常用于游戏中的跳跃,而力是指每帧施加的力,以提高汽车的速度。

In order for you to apply force to your rocket, you could use something like this: 为了使您向火箭施加力,可以使用以下方法:

       // gets x force based on angle
        float x = (float)Math.sin(body.getAngle() - Math.PI); // minus PI as objects start off facing right
        // gets y force based on angle
        float y = (float)Math.cos(body.getAngle());

        //apply force to center (applies force to middle so no rotation )
        //body.applyForceToCenter( new Vector2(
        //      body.getMass()* (x * 12),
        //      body.getMass()*(y*12)), true);

        //NOTE: bodies must be set to .fixedRotation = false in order to rotate;

        //apply force to a point on body (will create rotational force ) 
        body.applyForce( new Vector2(
                body.getMass()* (x * 12),//x force to apply
                body.getMass()* (y * 12)), //y force to apply
                // apply force to body at 0.5f(halfway for 1f wide object) x and -5 y
                body.getWorldPoint(new Vector2(0.5f,-5)),true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM