Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Bouncing within a circle

edited May 2012 in General Posts: 50

Hi all,

I'm quite stuck with my next bit of code:

displayMode(FULLSCREEN)


-- Use this function to perform your initial setup
function setup()
    pos = vec2(WIDTH/2, HEIGHT/2)
    speed = vec2(300, 134)
    time = 0
    center = vec2(WIDTH/2, HEIGHT/2)
    diameter = 750
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    
    ellipse(center.x, center.y, diameter)

    -- This sets the line thickness
    strokeWidth(5)
    sprite("Planet Cute:Star", pos.x, pos.y)
    
    
    time = DeltaTime
   
    pos.x = pos.x + time * speed.x
    pos.y = pos.y + time * speed.y   

    -- if the new position is outside or on the ellipse
    if pos:dist(center) >= diameter/2 then
        -- calculate the new speed direction
        angle = math.atan2(pos.x-center.x,pos.y-center.y)
        pcol=vec2(math.cos(angle)*375, math.sin(angle)*375)
        vecn = vec2(-pcol.x, -pcol.y):normalize()
        dotn=vecn:dot(vecn)
        dot=speed:dot(vecn)
        imp = dot/dotn
        speed.x = speed.x - imp * vecn.x * 2
        speed.y = speed.y - imp * vecn.y * 2
   
        -- update the new position
        pos.x = pcol.x + speed.x + center.x
        pos.y = pcol.y + speed.y + center.y
    end
end


I want the star to bounce within the circle. I can't get the math right. Anyone an idea what I'm doing wrong here?

Comments

  • Posts: 50

    I found the solution. The angle and new position, needed to be adapted.
    The new code works as expected. See code of future reference.


    displayMode(FULLSCREEN) -- Use this function to perform your initial setup function setup()     pos = vec2(WIDTH/2, HEIGHT/2-100)     speed = vec2(200, 150)     time = 0     center = vec2(WIDTH/2, HEIGHT/2)     diameter = 750 end -- This function gets called once every frame function draw()     -- This sets a dark background color      background(40, 40, 50)          ellipse(center.x, center.y, diameter)     -- This sets the line thickness     strokeWidth(5)     sprite("Planet Cute:Star", pos.x, pos.y)               time = DeltaTime         pos.x = pos.x + time * speed.x     pos.y = pos.y + time * speed.y        -- if the new position is outside or on the ellipse     if pos:dist(center) >= diameter/2 then         -- calculate the new speed direction         angle = math.atan2(pos.x-center.x,pos.y-center.y)         pcol=vec2(math.cos(0.5*math.pi-angle)*diameter/2,                  math.sin(0.5*math.pi-angle)*diameter/2)         vecn = vec2(-pcol.x, -pcol.y):normalize()         dotn=vecn:dot(vecn)         dot=speed:dot(vecn)*2         imp = dot/dotn         speed.x = speed.x - imp * vecn.x         speed.y = speed.y - imp * vecn.y              -- update the new position         pos.x = pcol.x + center.x + speed.x * time         pos.y = pcol.y + center.y + speed.y * time     end end
  • SimeonSimeon Admin Mod
    Posts: 5,449

    Thanks for sharing this @Doffer and I'm glad you found the solution.

  • Posts: 50

    Thanks @simeon. It has been far too long since the last time I've had to use trigonometry :)

Sign In or Register to comment.