Note that there are some explanatory texts on larger screens.

plurals
  1. PODrawing using textureRegion
    primarykey
    data
    text
    <p>I'm trying to make a movable car in java, using libgdx and box2d; I made a car, m_car, with position set in middle of the car. In every step, i take take the current position of the m_car, decrese that values with the cars width/height, so i have the left bottom corner. When is on a straight ground, it shows well, the texture is where is should be, but when the ground have an angle, the texture position its under, or upper the car. I don't understand how TextureRegion works, i think that when the angle is not 0, i dont take the point of drawing well, and i dont know how to solve it. help me pls :) Ty.</p> <p>Edit:</p> <p>my code is a mess, its not an actual project, its an ideea, and i took pieces, and put them down to see how they work, so when il start the project, i would know what to do, this is my car declaration: </p> <pre><code>private void car2(){ m_hz = 5.0f; m_zeta = 0.7f; m_speed = 50.0f; BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; part1(bd); part2(bd); part3(bd); wheels(bd); m_car.setAwake(true); } private void part3(BodyDef bd) { PolygonShape shape = new PolygonShape(); shape.setAsBox(0.01f, 0.35f,new Vector2(1.01f - 3, 4.85f - 4), 0); m_car.createFixture(shape, 1.0f); } private void part1(BodyDef bd){ PolygonShape chassis = new PolygonShape(); Vector2 vertices[] = new Vector2[6]; vertices[0] = new Vector2(-2, -0.5f); vertices[1] = new Vector2(-2, 0.5f); vertices[2] = new Vector2(1.25f, 0.5f); vertices[3] = new Vector2(2.5f, 0.15f); vertices[4] = new Vector2(2.5f, -0.5f); vertices[5] = new Vector2(-2, -0.5f); chassis.set(vertices); bd.position.set(new Vector2(3, 4)); m_car = m_world.createBody(bd); m_car.createFixture(chassis, 1.0f); } private void part2(BodyDef bd){ PolygonShape chassis = new PolygonShape(); Vector2 vertices[] = new Vector2[5]; vertices[0] = new Vector2(3, 4.5f); vertices[1] = new Vector2(3, 5.25f); vertices[2] = new Vector2(3.80f, 5.25f); vertices[3] = new Vector2(4.25f, 4.5f); vertices[4] = new Vector2(3, 4.5f); for (int i = 0; i &lt; 5; ++ i){ vertices[i].x -= 3; vertices[i].y -= 4; } chassis.set(vertices); //m_car = m_world.createBody(bd); m_car.createFixture(chassis, 1.0f); } private void wheels(BodyDef bd){ CircleShape circle = new CircleShape(); circle.setRadius(0.4f); FixtureDef fd = new FixtureDef(); fd.shape = circle; fd.density = 1.0f; fd.friction = 0.9f; bd.position.set(2f, 3.5f); m_wheel1 = m_world.createBody(bd); m_wheel1.createFixture(fd); bd.position.set(4.5f, 3.5f); m_wheel2 = m_world.createBody(bd); m_wheel2.createFixture(fd); WheelJointDef jd = new WheelJointDef(); Vector2 axis = new Vector2(1.0f, 0.5f); jd.initialize(m_car, m_wheel1, m_wheel1.getPosition(), axis); jd.motorSpeed = 0.0f; jd.maxMotorTorque = 20.0f; jd.enableMotor = true; jd.frequencyHz = m_hz; jd.dampingRatio = m_zeta; m_spring1 = (WheelJoint) m_world.createJoint(jd); jd.initialize(m_car, m_wheel2, m_wheel2.getPosition(), axis); jd.motorSpeed = 0.0f; jd.maxMotorTorque = 10.0f; jd.enableMotor = false; jd.frequencyHz = m_hz; jd.dampingRatio = m_zeta; m_spring2 = (WheelJoint) m_world.createJoint(jd); } </code></pre> <p>some explenation: why i have for for vertices to decreese 3 and 4 ? i didnt know that if i set position to the bodydef, the vertices consider that point to be 0, 0, when i found out, was easyer to me to just decrese (cuz its just for test, to see how it works)</p> <p>and this is how i draw:</p> <pre><code> float angle = (float) Math.toDegrees(m_car.getAngle()); batch.draw(textureRegion, x, y, 3f, 4f, 4.5f, 2.75f, 1f, 0.61f, angle); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload