Log In  


Anyone got a nice function(s) for having one object rotate around single point?

I've done a little digging but all the Lua stuff points to the math library which appears to be limited in P8...

Appreciate any code or links.



Here you go:

direction = 0
anchor_sine = 0
anchor_x = 64

function _update()
  direction += .05
  anchor_sine += .01
  anchor_x = 64 + sin(anchor_sine) * 40
end

function _draw()
  cls()
  circfill(anchor_x, 64, 1.5, 7)
  circfill(anchor_x + cos(direction) * 10, 64 - sin(direction) * 10, 1.5, 8)
  circfill(anchor_x + cos(direction / 2) * 20, 64 - sin(direction / 2) * 20, 1.5, 9)
end
  • Blokatt

Awesome, thanks so much!


No problem!


Pasted it all in and worked great...wasn't even expecting the moving anchor, pretty sweet. But wasn't exactly sure what all the variables controlled, so I annotated it some more with extra variables. Thanks again!

anchor_sine=0
anchor_x=64
anchor_y=64
speed=.05
radius=20
ccw=false --counterclockwise?
direction=0 --leave at zero

function _update()
  anchor_sine += .01
  anchor_x = anchor_y + sin(anchor_sine) * 40 --this is speed/distance of anchor movement
end

function _draw()
  cls()

  --center point
  circfill(anchor_x, anchor_y, 1.5, 7)

  --get direction based on flag
  if (ccw) then 
  	direction -= speed --counterclockwise
  else
  	direction += speed --clockwise
  end

  --orbiting circle

  circfill(anchor_x + cos(direction / (radius/10)) * radius, anchor_y - sin(direction / (radius/10)) * radius, 1.5, 9)
end

By the way, you might want to use "+ sin" instead, since it's inverted in Lua.



[Please log in to post a comment]