1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.physics.box2d.joints; 18 19 import com.badlogic.gdx.math.Vector2; 20 import com.badlogic.gdx.physics.box2d.Joint; 21 import com.badlogic.gdx.physics.box2d.World; 22 23 /** A wheel joint. This joint provides two degrees of freedom: translation along an axis fixed in body1 and rotation in the plane. 24 * You can use a joint limit to restrict the range of motion and a joint motor to drive the rotation or to model rotational 25 * friction. This joint is designed for vehicle suspensions. */ 26 public class WheelJoint extends Joint { 27 // @off 28 /*JNI 29 #include <Box2D/Box2D.h> 30 */ 31 32 33 private final float[] tmp = new float[2]; 34 private final Vector2 localAnchorA = new Vector2(); 35 private final Vector2 localAnchorB = new Vector2(); 36 private final Vector2 localAxisA = new Vector2(); 37 38 public WheelJoint (World world, long addr) { 39 super(world, addr); 40 } 41 42 public Vector2 getLocalAnchorA () { 43 jniGetLocalAnchorA(addr, tmp); 44 localAnchorA.set(tmp[0], tmp[1]); 45 return localAnchorA; 46 } 47 48 private native void jniGetLocalAnchorA (long addr, float[] anchor); /* 49 b2WheelJoint* joint = (b2WheelJoint*)addr; 50 anchor[0] = joint->GetLocalAnchorA().x; 51 anchor[1] = joint->GetLocalAnchorA().y; 52 */ 53 54 public Vector2 getLocalAnchorB () { 55 jniGetLocalAnchorB(addr, tmp); 56 localAnchorB.set(tmp[0], tmp[1]); 57 return localAnchorB; 58 } 59 60 private native void jniGetLocalAnchorB (long addr, float[] anchor); /* 61 b2WheelJoint* joint = (b2WheelJoint*)addr; 62 anchor[0] = joint->GetLocalAnchorB().x; 63 anchor[1] = joint->GetLocalAnchorB().y; 64 */ 65 66 public Vector2 getLocalAxisA(){ 67 jniGetLocalAxisA(addr, tmp); 68 localAxisA.set(tmp[0], tmp[1]); 69 return localAxisA; 70 } 71 72 private native void jniGetLocalAxisA (long addr, float[] anchor); /* 73 b2WheelJoint* joint = (b2WheelJoint*)addr; 74 anchor[0] = joint->GetLocalAxisA().x; 75 anchor[1] = joint->GetLocalAxisA().y; 76 */ 77 78 /** Get the current joint translation, usually in meters. */ 79 public float getJointTranslation () { 80 return jniGetJointTranslation(addr); 81 } 82 83 private native float jniGetJointTranslation (long addr); /* 84 b2WheelJoint* joint = (b2WheelJoint*)addr; 85 return joint->GetJointTranslation(); 86 */ 87 88 /** Get the current joint translation speed, usually in meters per second. */ 89 public float getJointSpeed () { 90 return jniGetJointSpeed(addr); 91 } 92 93 private native float jniGetJointSpeed (long addr); /* 94 b2WheelJoint* joint = (b2WheelJoint*)addr; 95 return joint->GetJointSpeed(); 96 */ 97 98 /** Is the joint motor enabled? */ 99 public boolean isMotorEnabled () { 100 return jniIsMotorEnabled(addr); 101 } 102 103 private native boolean jniIsMotorEnabled (long addr); /* 104 b2WheelJoint* joint = (b2WheelJoint*)addr; 105 return joint->IsMotorEnabled(); 106 */ 107 108 /** Enable/disable the joint motor. */ 109 public void enableMotor (boolean flag) { 110 jniEnableMotor(addr, flag); 111 } 112 113 private native void jniEnableMotor (long addr, boolean flag); /* 114 b2WheelJoint* joint = (b2WheelJoint*)addr; 115 joint->EnableMotor(flag); 116 */ 117 118 /** Set the motor speed, usually in radians per second. */ 119 public void setMotorSpeed (float speed) { 120 jniSetMotorSpeed(addr, speed); 121 } 122 123 private native void jniSetMotorSpeed (long addr, float speed); /* 124 b2WheelJoint* joint = (b2WheelJoint*)addr; 125 joint->SetMotorSpeed(speed); 126 */ 127 128 /** Get the motor speed, usually in radians per second. */ 129 public float getMotorSpeed () { 130 return jniGetMotorSpeed(addr); 131 } 132 133 private native float jniGetMotorSpeed (long addr); /* 134 b2WheelJoint* joint = (b2WheelJoint*)addr; 135 return joint->GetMotorSpeed(); 136 */ 137 138 /** Set/Get the maximum motor force, usually in N-m. */ 139 public void setMaxMotorTorque (float torque) { 140 jniSetMaxMotorTorque(addr, torque); 141 } 142 143 private native void jniSetMaxMotorTorque (long addr, float torque); /* 144 b2WheelJoint* joint = (b2WheelJoint*)addr; 145 joint->SetMaxMotorTorque(torque); 146 */ 147 148 public float getMaxMotorTorque () { 149 return jniGetMaxMotorTorque(addr); 150 } 151 152 private native float jniGetMaxMotorTorque (long addr); /* 153 b2WheelJoint* joint = (b2WheelJoint*)addr; 154 return joint->GetMaxMotorTorque(); 155 */ 156 157 /** Get the current motor torque given the inverse time step, usually in N-m. */ 158 public float getMotorTorque (float invDt) { 159 return jniGetMotorTorque(addr, invDt); 160 } 161 162 private native float jniGetMotorTorque (long addr, float invDt); /* 163 b2WheelJoint* joint = (b2WheelJoint*)addr; 164 return joint->GetMotorTorque(invDt); 165 */ 166 167 /** Set/Get the spring frequency in hertz. Setting the frequency to zero disables the spring. */ 168 public void setSpringFrequencyHz (float hz) { 169 jniSetSpringFrequencyHz(addr, hz); 170 } 171 172 private native void jniSetSpringFrequencyHz (long addr, float hz); /* 173 b2WheelJoint* joint = (b2WheelJoint*)addr; 174 joint->SetSpringFrequencyHz(hz); 175 */ 176 177 public float getSpringFrequencyHz () { 178 return jniGetSpringFrequencyHz(addr); 179 } 180 181 private native float jniGetSpringFrequencyHz (long addr); /* 182 b2WheelJoint* joint = (b2WheelJoint*)addr; 183 return joint->GetSpringFrequencyHz(); 184 */ 185 186 /** Set/Get the spring damping ratio */ 187 public void setSpringDampingRatio (float ratio) { 188 jniSetSpringDampingRatio(addr, ratio); 189 } 190 191 private native void jniSetSpringDampingRatio (long addr, float ratio); /* 192 b2WheelJoint* joint = (b2WheelJoint*)addr; 193 joint->SetSpringDampingRatio(ratio); 194 */ 195 196 public float getSpringDampingRatio () { 197 return jniGetSpringDampingRatio(addr); 198 } 199 200 private native float jniGetSpringDampingRatio (long addr); /* 201 b2WheelJoint* joint = (b2WheelJoint*)addr; 202 return joint->GetSpringDampingRatio(); 203 */ 204 205 } 206