displayMode(FULLSCREEN)
function setup()
rot=false
a=0
end
function draw()
background(40, 40, 50)
fill(255)
text("tap screen to start/stop rotation",WIDTH/2,HEIGHT-50)
sprite("Planet Cute:Character Boy",WIDTH/2,HEIGHT/2)
pushMatrix()
translate(WIDTH/2,HEIGHT/2)
rotate(a)
sprite("Planet Cute:Star",0,200)
popMatrix()
if rot then
a=a+1
end
end
function touched(t)
if t.state==BEGAN then
rot=not rot
end
end
Edit: I have tried and it works
But is there a way, that you can move the first sprite around the second one, by dragging it with your finger.
Hope you understand, but I don’t know how to explain
@Leon Here’s an example to move a sprite with touch.
displayMode(FULLSCREEN)
function setup()
dx=400
dy=400
end
function draw()
background(40, 40, 50)
fill(255)
text("Drag finger around the screen to move the star",WIDTH/2,HEIGHT-50)
sprite("Planet Cute:Character Boy",WIDTH/2,HEIGHT/2)
sprite("Planet Cute:Star",dx,dy)
end
function touched(t)
if t.state==MOVING then
dx=dx+t.deltaX
dy=dy+t.deltaY
end
end
Thanks :), but I know how to move a sprite with touch.
What I want to do is the same as before, but the sprite, wich rotates around the other, should rotate to the angle of the current touch x and y.
So if you touch the object, you can move it around the other object (but it’s x and y coordinates are always the same).
@Leon Sorry, but I’m more confused at what you want. You want the star to rotate around the boy as in the first example. But you want to be able to touch the star and move it around but you want it’s x,y coordinates to be the same. I don’t follow what you want. Can you try explaining again, referring to the star and the boy and what you want each object to do when you touch the screen and when you’re not touching the screen.
Is this more along the lines of what you were thinking? (Touch the blue one)
function setup()
spritedist = 120
spritesize = 30
ellsize = 30
ellipseloc = vec2(WIDTH/2,HEIGHT/2)
spriteloc = ellipseloc+vec2(0,spritedist)
tid = nil
col = color(0,0,255)
end
function draw()
background(40, 40, 50)
fill(255,0,0)
ellipse(ellipseloc.x,ellipseloc.y,ellsize)
fill(col)
ellipse(spriteloc.x,spriteloc.y,spritesize)
end
function touched(t)
if spriteloc:dist(vec2(t.x,t.y)) < spritesize/2+5 and t.state == BEGAN then
tid = t.id
col = color(0,255,0)
elseif t.state == MOVING and t.id == tid then
local ang = math.atan2(t.y-ellipseloc.y,t.x-ellipseloc.x)
spriteloc = vec2(0,spritedist):rotate(ang-(math.pi/2))+ellipseloc
elseif t.state == ENDED and t.id == tid then
tid = nil
col = color(0,0,255)
end
end
-- Dial
function setup()
pivot=vec2(WIDTH/2,HEIGHT/2)
tpos=vec2(0,0)
end
function draw()
angle=anglebetween(pivot,tpos)
background(0)
pushMatrix()
translate(pivot.x,pivot.y)
rotate(angle)
sprite("SpaceCute:Planet",0,0)
popMatrix()
end
function touched(touch)
tpos=vec2(touch.x,touch.y)
end
function anglebetween(ptA,ptB)
local angle=math.deg(math.atan2(ptA.y-ptB.y,ptA.x-ptB.x))
angle = angle + 90
return angle
end
```