I am making a game, but have run into an issue. I want to be able to buck up the character, but if you let go I wan it to fall. I also need to know how to revise this code to keep the character a physics body at all times so it will not phase through walls. Good luck and thanks for taking a crack at it!
function touched(t) -- if C is touched then pick up (not a physocs body, but I want it to be one.)
if t.x>c.x-30 and t.x<c.x+30 and t.y>c.y-30 and t.y<c.y+30 then
c.x=t.x
c.y=t.y
elseif t == false then -- if C is let go, fall and be a physics body
end
Firstly, think of a better name for your physics body than c.
Keep it as a dynamic physics body, then, instead of setting its coordinates directly (a bad idea with dynamic physics body), try using applyForce with touch.deltaX and .deltaY
@yojimbo2000 thanks for the advice, but I am not sure how to use this, I have read the reference but don’t understand how to combine these two segments to work together, please explaine more
displayMode(FULLSCREEN)
function setup()
e1=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
e2=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
e3=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
e4=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
p1=physics.body(CIRCLE,50)
p1.x=WIDTH/2
p1.y=HEIGHT-50
p1.restitution=.5
physics.continuous=true
end
function draw()
background(40, 40, 50)
ellipse(p1.x,p1.y,100)
end
function touched(t)
if t.state==MOVING then
dx=t.deltaX
dy=t.deltaY
p1.linearVelocity=vec2(dx*50,dy*50)
end
end
Yes @dave1707 but only to an exten. I want a character to move and when I let go, just react to gravity. Your example has helped but sadly Imstill need to experiment and hope to learn from this. Thank you!
displayMode(FULLSCREEN)
function setup()
e1=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
e2=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
e3=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
e4=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
p1=physics.body(CIRCLE,50)
p1.x=WIDTH/2
p1.y=HEIGHT-50
p1.restitution=1
physics.continuous=true
end
function draw()
background(40, 40, 50)
sprite("Planet Cute:Character Horn Girl",p1.x,p1.y)
end
function touched(t)
if t.state==BEGAN or t.state==MOVING then
if (t.x-p1.x)^2/50^2+(t.y-p1.y)^2/50^2 <= 1 then
p1.type=KINEMATIC
p1.linearVelocity=vec2(0,0)
p1.x=t.x
p1.y=t.y
end
end
if t.state==ENDED then
p1.type=DYNAMIC
end
end