I cannot figure out why the gravity is not working, yet I have it already implemented
Help appreciated
code
function setup()
physics.gravity(vec2(0,-1000))
touch=1
b1 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
b2 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
b3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
b4 = physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
block=physics.body(CIRCLE,40)
block.x=WIDTH/2
block.y=40
blockx=block.x
blocky=block.y
end
function draw()
background(255, 255, 255, 255)
block.linearVelocity=(vec2(CurrentTouch.x - block.x, CurrentTouch.y - block.y))
if touch> WIDTH/2 then
blockx=blockx+4
end
sprite("Documents:basketball",blockx,blocky,70,70)
blockx=block.x
blocky=block.y
sprite("Documents:hoopx2",0,5,700,1200)
end
Anytime you do anything with a physics object, you shouldn’t alter its velocity directly as you’re doing with blockx. You should only use linearVelocity.
I got rid of all the access code from failed attempts
Since I don’t have access to your sprites, it’s hard to tell what’s happening. Instead of using CurrentTouch, try to use the touched() function. The calculation with CurrentTouch isn’t allowing the physics object (block) to act the way it should.
@Conor_R12 Here’s your code using the function touched().
function setup()
b1 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
b2 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
b3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
b4 = physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
block=physics.body(CIRCLE,40)
block.x=WIDTH/2
block.y=800
block.restitution=.8
end
function draw()
background(255, 255, 255, 255)
sprite("Documents:basketball",block.x,block.y,70,70)
sprite("Documents:hoopx2",0,5,700,1200)
end
function touched(t)
if t.state==BEGAN then
block.linearVelocity=vec2(t.x-block.x,t.y-block.y)
end
end
Try using
block:applyForce(vec2(CurrentTouch.x - block.x, CurrentTouch.y - block.y))
Instead of
block.linearVelocity = vec2(CurrentTouch.x - block.x, CurrentTouch.y - block.y)
@SkyTheCoder Did you try running your suggestion. I get a nil value for block:applyLinearVelocity.
Just leave gravity alone, it is preset to a realistic amount. Is your circle lying on the ground right away when you launch? If so try to just not declare gravity at the top of your code. Just erase the part of your code that says “physics.gravity(vec2(0,-1000))”
@dave1707 Sorry, I forgot - it’s applyForce, not applyLinearVelocity.
The problem with using CurrentTouch and linearVelocity is the ball tends to go to the point that was touched and then stops at that point. Using CurrentTouch and applyForce, the ball now oscillates around the point being touched. The intent of the question is for the ball to go towards the point being touched and then continue under the influence of gravity and fall. That is achieved in my code above using the touched function. The problem with using CurrentTouch is the touch values remain in .x and .y and the velocity is constantly being re-calculated in the draw function, not allowing gravity to control the velocity of the ball.