angularVelocity bug ?

Hi all,

I’m new to Codea and was trying to code a simple pool game to learn physics, but when a ball touches an edge, its angular velocity gets very high without any reason… Is it a bug in Codea physics engine or did I code something wrong ? Here is my code :

displayMode(FULLSCREEN)

function setup()

    strokeWidth(2)
    stroke(0)
    noFill()
    ellipseMode(CENTER)

    top = physics.body(EDGE, vec2(0, HEIGHT), vec2(WIDTH, HEIGHT))
    bottom = physics.body(EDGE, vec2(0, 0), vec2(WIDTH, 0))
    left = physics.body(EDGE, vec2(0, 0), vec2(0, HEIGHT))
    right = physics.body(EDGE, vec2(WIDTH, 0), vec2(WIDTH, HEIGHT))

    ball = physics.body(CIRCLE, 40)
    ball.position = vec2(WIDTH/2, HEIGHT/2)
    ball.linearVelocity = vec2(200, 200)
    ball.restitution = 0.8
    ball.friction = 1

    --physics.gravity(0, 0)
    ball.gravityScale = 0

end


function draw()

    background(128)
    ellipse(ball.x, ball.y, 80)
    line(ball.x, ball.y, ball.x + 38 * math.cos(ball.angle), ball.y + 38 * math.sin(ball.angle))

end

I draw a line in the circle to see angular velocity, and the ball rotates quickly when touching edges.
Any help would be appreciated :slight_smile:

The friction is at an very high level for the circle. It should be closer to 0.0001, not 1!

Also, you have no form of angular drag, so it’ll never slow down after spinning,

@logoliv Here’s an example I pulled out of my projects that shows angular rotation. The bounce with rotation looks realistic, so I don’t think there’s a problem with the physics engine. Also, this doesn’t change the default friction value.


displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    w=WIDTH
    w2=WIDTH/2
    h=100
    e1=physics.body(EDGE,vec2(0,h),vec2(w2,0))
    e2=physics.body(EDGE,vec2(w2,0),vec2(w,h))
    e3=physics.body(EDGE,vec2(w,0),vec2(w,HEIGHT))
    
    r1=physics.body(CIRCLE,40)
    r1.x=0
    r1.y=500
    r1.restitution=.8
    r1.linearVelocity=vec2(210,0)
end

function draw()
    background(40, 40, 50)
    noFill()
    stroke(255)
    strokeWidth(2)   
    line(0,h,w2,0)
    line(w2,0,w,h)
    line(w,0,w,HEIGHT)
    
    pushMatrix()
    translate(r1.x,r1.y)
    xx=r1.angle
    rotate(xx)
    ellipse(0,0,80)
    line(0,0,0,38)
    popMatrix()
end

@logoliv Change the draw function to this. You’re not converting degrees to radians. Ball.angle is in degrees, but math.sin and cos use radians.


function draw()
    background(128)
    ellipse(ball.x, ball.y, 80)
    r=math.rad(ball.angle)
    line(ball.x, ball.y, ball.x+38*math.cos(r), ball.y + 38 * math.sin(r))
end

@dave1707 thanks ! I have found the solution after your first post and I came here to explain how… when I just found your last post. By the way I did the brutal way because I didn’t know the math.rad function :slight_smile:

rad = deg / 180 * math.pi

It may seem strange that an object gain some rotation when it collide straight against a wall… but we can verify that it is because of its friction with the wall : setting ball.friction = 0 and no rotation occurs.
I’m now reassured about the quality of Codea physics engine :slight_smile:

If you want more detailed info, search up Box2d; it is the physics engine codea uses.

Also I feel super dumb for thinking friction was the issue!

@causeless no problem, by the way you were right about the second part : i needed some linearDamping and angularDamping to make my simulation more realistic :wink:
also thanks for the info (i am surely not the first to think that Codea reference is a little light)