Я пытаюсь реализовать Java-версию игры машина сверху вниз на основе Эмануэле Феронато "Два способа делать автомобили Box2D". Я знаю некоторые основы box2d и по большей части преобразовал код в Java как есть, за некоторыми исключениями.
Однако, когда я запускаю программу, моя машина вообще не двигается.
Если я сделаю все тело динамическим, все колеса (кроме левого переднего) начнут двигаться вперед и назад, раскидывая машину вперед и назад, но в итоге никуда не попадая. Передние два шарнира представляют собой вращающиеся шарниры с двигателем на каждом, а задние призматические, так что поправьте меня, если я ошибаюсь, но передние два должны быть единственными, которые «вращаются» / движутся. Я чувствую, что делаю что-то ужасно неправильное, но куда бы я ни посмотрел, это всегда в сценарии действия, поэтому я не уверен на 100%, что не так.
Я проверил, и все колеса находятся в правильном положении, а шарниры установлены на правильные колеса. Я проверил скорость двигателя, и он также работает. Моя компонента x "ldirection" и "rdirection" всегда равна 0, поэтому она убивает боковую скорость, а y всегда является значением. Так что на самом деле это должно двигаться вперед, верно?
Левое переднее колесо всегда остается на том же расстоянии от кузова, когда кузов перемещается вверх и вниз. Так что Левый Фронт, кажется, работает нормально. Я проверил весь свой код, чтобы убедиться, что правое переднее колесо сделано таким же, как левое.
Запуск автомобиля
При ускорении вперед только два правых колеса и заднее левое колесо двигаются вперед и назад.
Когда я начинаю поворачивать два передних колеса, два задних колеса все еще остаются на одной линии с автомобилем, но начинают двигаться несколько диагонально. В конце концов, когда передние колеса поворачиваются на 90 градусов, они начинают вращаться почти в центре шарнира.
Инициализация
this.world = new World(new Vector2(0, 0), false);
this.box2Drender = new Box2DDebugRenderer();
this.LeftPJointDef = new PrismaticJointDef();
this.RightPJointDef = new PrismaticJointDef();
this.RightJointDef = new RevoluteJointDef();
this.LeftJointDef = new RevoluteJointDef();
this.CarBody = new PolygonShape();
this.RightRWheelShape = new PolygonShape();
this.RightFWheelShape = new PolygonShape();
this.LeftRWheelShape = new PolygonShape();
this.LeftFWheelShape = new PolygonShape();
this.LeftRWheelDef = new BodyDef();
this.RightRWheelDef = new BodyDef();
this.RightFWheelDef = new BodyDef();
this.LeftFWheelDef = new BodyDef();
this.bodyD = new BodyDef();
this.CarFixDef = new FixtureDef();
this.x = x;
this.y = y;
this.Cpos = new Vector2(x,y);
this.RRW = new Vector2((this.x + (this.x * XPrc)), (this.y + (this.y * -YPrc)));
this.RLW = new Vector2((this.x + (this.x * -XPrc)), (this.y + (this.y * -YPrc)));
this.FRW = new Vector2((this.x + (this.x * XPrc)), (this.y + (this.y * YPrc)));
this.FLW = new Vector2((this.x + (this.x * -XPrc)), (this.y + (this.y * YPrc)));
this.WheelSizeX = this.width * 0.25f;
this.WheelSizeY = this.length * 0.30f;
//setting bodyDef damping
bodyD.linearDamping = 0.5f;
bodyD.angularDamping = 0.5f;
//Adding bodyDef to the world and setting type as Dynamic
body = world.createBody(bodyD);
body.setType(BodyDef.BodyType.DynamicBody);
//setting the body position in the world using the Vector given.
body.setTransform(this.Cpos, (float) ((Math.PI) / 2));
//Adding the calculated Position vecotrs of the wheel's to each wheel def.
RightFWheelDef.position.add(FRW);
LeftFWheelDef.position.add(FLW);
RightRWheelDef.position.add(RRW);
LeftRWheelDef.position.add(RLW);
//Adding the wheels to the world using the Wheel Defs.
RightFWheel = world.createBody(RightFWheelDef);
LeftFWheel = world.createBody(LeftFWheelDef);
RightRWheel = world.createBody(RightRWheelDef);
LeftRWheel = world.createBody(LeftRWheelDef);
RightFWheel.setType(BodyDef.BodyType.DynamicBody);
RightRWheel.setType(BodyDef.BodyType.DynamicBody);
LeftFWheel.setType(BodyDef.BodyType.DynamicBody);
LeftRWheel.setType(BodyDef.BodyType.DynamicBody);
//Setting the car(box) and wheel size
CarBody.setAsBox(this.length, this.width);
LeftFWheelShape.setAsBox(WheelSizeX, WheelSizeY);
LeftRWheelShape.setAsBox(WheelSizeX, WheelSizeY);
RightRWheelShape.setAsBox(WheelSizeX, WheelSizeY);
RightFWheelShape.setAsBox(WheelSizeX, WheelSizeY);
CarFixDef.shape = CarBody;
RightFWheel.createFixture(RightFWheelShape, 1);
RightRWheel.createFixture(RightRWheelShape, 1);
LeftFWheel.createFixture(LeftFWheelShape, 1);
LeftRWheel.createFixture(LeftRWheelShape, 1);
body.createFixture(CarFixDef);
LeftJointDef.enableMotor = true;
RightJointDef.enableMotor = true;
LeftJointDef.maxMotorTorque = 500;
RightJointDef.maxMotorTorque = 500;
//Setting Front Wheel joints in respects to the wheels and body
LeftJointDef.initialize(body, LeftFWheel, LeftFWheel.getWorldCenter());
RightJointDef.initialize(body, RightFWheel, RightFWheel.getWorldCenter());
this.LeftJoint = (RevoluteJoint) world.createJoint(LeftJointDef);
this.RightJoint = (RevoluteJoint) world.createJoint(RightJointDef);
LeftPJointDef.enableLimit = true;
RightPJointDef.enableLimit = true;
//Translation Limit
LeftPJointDef.lowerTranslation = 0;
LeftPJointDef.upperTranslation = 0;
RightPJointDef.lowerTranslation = 0;
RightPJointDef.upperTranslation = 0;
//Setting Rear wheel joints in respects to wheel and body
LeftPJointDef.initialize(body, LeftRWheel, LeftRWheel.getWorldCenter(), new Vector2(1, 0));
RightPJointDef.initialize(body, RightRWheel, RightRWheel.getWorldCenter(), new Vector2(1, 0));
//adding the P Joints to the world.
this.LeftPJoint = (PrismaticJoint) world.createJoint(LeftPJointDef);
this.RightPJoint = (PrismaticJoint) world.createJoint(RightPJointDef);
Вот мой метод обновления.
KillOrthoVelocity(LeftFWheel);
KillOrthoVelocity(RightFWheel);
KillOrthoVelocity(LeftRWheel);
KillOrthoVelocity(RightRWheel);
//Driving
float r1 = LeftFWheel.getTransform().getRotation();
Vector2 ldirection = new Vector2((float) -Math.sin(r1), (float) Math.cos(r1));
ldirection.scl(enginespeed);
float r2 = RightFWheel.getTransform().getRotation();
Vector2 rdirection = new Vector2((float) -Math.sin(r2), (float) Math.cos(r2));
rdirection.scl(enginespeed);
LeftFWheel.applyForce(ldirection, LeftFWheel.getPosition(), true);
RightFWheel.applyForce(rdirection, RightFWheel.getPosition(), true);
//Steering
float movespeed;
movespeed = steerAng - LeftJoint.getJointAngle();
LeftJoint.setMotorSpeed(movespeed * AngleSpeed);
movespeed = steerAng - RightJoint.getJointAngle();
RightJoint.setMotorSpeed(movespeed * AngleSpeed);
world.step(dt, 6, 2);
KillOrthoVelocity похож на получение "ldirection"
Vector2 localP = new Vector2(0, 0);
Vector2 velocity = body.getLinearVelocityFromLocalPoint(localP);
float r = body.getTransform().getRotation();
Vector2 sideways = new Vector2((float) -Math.sin(r), (float) Math.cos(r));
sideways.scl(velocity.dot(sideways));
body.setLinearVelocity(sideways);
Любой совет будет очень признателен! Даже просто подсказка будет чрезвычайно полезна! Спасибо!