Rotating Two Sprites in place

I’m almost sure that this discussion has already been made, but I have not been able to find it. I’ve been trying to equip a tilt physics vehicle with wheels that spin when it moves to make it look more realistic. For this, I would need to have two circles following the physics body’s position with a rotation that is based on the amount of linear velocity applied to the body itself. This is so that the wheels will spin as they move from side to side. But from what I understand, you can only apply one center of rotation that every single object would rotate around. Obviously I can’t have this happen because I need both wheels to spin in place, and I want those wheels to be the only things that will spin when the screen is tilted. How can I do this?

You can rotate a sprite in place like this:

    translate(WIDTH / 2, HEIGHT / 2)
    rotate(ElapsedTime * 45)
    sprite("Planet Cute:Character Boy")

(Put it in draw())

Of course, for multiple sprites, you’ll want to push/pop the matrices so it works correctly for the next sprite.

But I want both sprites to be under the vehicle while they rotate and I want it to be rotating according to the vehicles movement. So if the vehicle is moving slowly to the right, I want the wheels to be moving at that same speed continuously to the right.

@Paintcannon Are you after something like this. Tilt the iPad left or right to move the wheels.


displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    physics.continuous=true
    tab={}
    table.insert(tab,circ(300,315,0))
    table.insert(tab,circ(375,310,0))
    e1=physics.body(EDGE,vec2(0,300),vec2(WIDTH,300)) 
end

function draw()
    background(40, 40, 50)
    line(0,300,WIDTH,300) 
    for a,b in pairs(tab) do
        b:draw()
    end
end

circ = class()

function circ:init(x,y,t)
    self.a=physics.body(CIRCLE,25)
    self.a.x=x
    self.a.y=y
    self.a.angle=0
    self.a.friction=1
end

function circ:draw()
    pushMatrix() 
    translate(self.a.x,self.a.y)  
    stroke(255)
    strokeWidth(2)
    noFill()
    self.a.linearVelocity=vec2(Gravity.x*200,0)
    ellipse(0,0,50,50) 
    rotate(self.a.angle)
    line(0,-23,0,23)
    popMatrix()
end  

@Paintcannon Also, see test6 of the Physics lab example.

@dave1707 I am pretty much after that only I want it two be two wheel SPRITES that are spinning. Not actual physics bodies.

@Paintcannon How about this.


displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    physics.continuous=true
    w1=physics.body(CIRCLE,25)
    w1.x=300
    w1.y=315
    w2=physics.body(CIRCLE,25)
    w2.x=400
    w2.y=315
    e1=physics.body(EDGE,vec2(0,300),vec2(WIDTH,300)) 
end

function draw()
    background(40, 40, 50)
    stroke(255)
    strokeWidth(2)
    line(0,300,WIDTH,300) 
    rect(w1.x-50,325,200,50)
    
    pushMatrix() 
    translate(w1.x,w1.y)  
    w1.linearVelocity=vec2(Gravity.x*200,Gravity.y*200)
    rotate(w1.angle)
    sprite("Space Art:UFO",0,0,50)
    popMatrix()
    
    pushMatrix()
    translate(w2.x,w2.y)  
    w2.linearVelocity=vec2(Gravity.x*200,Gravity.y*200)
    rotate(w2.angle)
    sprite("Space Art:UFO",0,0,50)
    popMatrix()
end

@Paintcannon If that isn’t what your after either, then the next thing is to calculate the x movement left or right and calculate the angle of rotation that will match the distance based on the diameter of the wheel.

@Paintcannon Here’s another version that doesn’t use physics objects. I rotate the sprites bases on the x movement.


displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    x1=300
    x2=400
    a=0
end

function draw()
    background(40, 40, 50)
    stroke(255)
    strokeWidth(2)
    line(0,300,WIDTH,300)
    rect(x1-50,325,200,50)
    
    pushMatrix()
    translate(x1,325)
    rotate(a)
    sprite("Space Art:UFO",0,0,50)
    g=Gravity.x*20
    x1=x1+g
    a=a-g
    popMatrix()
    
    pushMatrix()
    translate(x2,325)
    rotate(a)
    sprite("Space Art:UFO",0,0,50)
    g=Gravity.x*20
    x2=x2+g
    a=a-g
    popMatrix()
end

@dave1707 Yes this is what I needed. Thanks.

Here’s a version that calculates the correct angle of rotation based on the size of the wheel. This is based on the sprite size of the UFO. Other circular sprites might vary.


displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    x1=300
    x2=400
    a=0
    size=50
    c=math.pi*size  -- circumference of wheel
end

function draw()
    background(40, 40, 50)
    stroke(255)
    strokeWidth(2)
    line(0,300,WIDTH,300)
    rect(x1-50,325,200,50) 
    g=Gravity.x*20  
    a=a-g*360/c
    
    pushMatrix()
    translate(x1,325)
    rotate(a)
    sprite("Space Art:UFO",0,0,size)
    x1=x1+g
    popMatrix()
    
    pushMatrix()
    translate(x2,325)
    rotate(a)
    sprite("Space Art:UFO",0,0,size)
    x2=x2+g
    popMatrix()
end

@Paintcannon Not using physics objects made it a lot easier on a level path. But if the ground you want the vehicle to roll on has hills and valleys, then using physics objects will make that a lot easier.

Made with dave1707 code

openURL(“http://youtu.be/kf7Jjgc04-0”)

@matox Nice job, looks real.

@matox Just a note - is your wheel’s image slightly off-center? It appears the wheel is bobbing up and down slightly.

@matox Great job, but in your video you leave no specific references to @dave1707, his code, or any credits in general. If I were to go through YouTube looking for an answer to my problem instead of making this discussion, I would most likely see your video and I would find there to be no references or instructions and I would think: What’s the point of this guy posting this video if it will not help me figure out how he did this? And I would continue to be lost. I would recommend posting the code for your example or at least a reference to @dave1707 in your description box so that this video can actually help others.