Hello, I am new (everyone says it but I am saying it anyway. Deal with it )
I am writing a code that involves objects hitting each other. I looked through different discussions, but all of them were too complex and was too specific for my taste.
My question is:
What is a simple code that could detect when 2 objects are touching?
(If you need to look at the code, just ask. Just a reminder though, the code is simple!)
-- all of this info can be found in the online manual
-- see the physics tab
function setup()
-- creates a circle that doesn't move
c1 = physics.body(CIRCLE,100) -- create body for 1st circle
c1.type=STATIC -- static means the circle doesn't move
c1.x=WIDTH/2 -- starting x,y values
c1.y=300
-- creates a circle that is influenced by gravity
c2 = physics.body(CIRCLE,50) -- create body for 2nd circle
c2.x=WIDTH/2 -- starting x,y values
c2.y=800
c2.restitution=1 -- how much the circle bounces when collided
end
function draw()
background(40,40,50)
stroke(255)
strokeWidth(1)
noFill()
ellipse(c1.x,c1.y,200,200) -- draw the 1st circle position
ellipse(c2.x,c2.y,100,100) -- draw the 2nd circle position
end
function collide(c)
if c.state==BEGAN then -- do this when the collision begins
print("\
collision")
print(c.id)
print("radius of body A ",c.bodyA.radius)
print("radius of body B ",c.bodyB.radius)
print("position of body A ",c.bodyA.x)
print("position of body A ",c.bodyA.y)
-- see physics.contact and physics.body in the reference manual
-- for other info that's available during a collision
end
end
@tyguy2 Can you be a little more specific. Do you want the first object to collide with another object, then return back to itâs original starting point. Does the second object move randomly on the screen. Iâm sure whatever you want to happen can be done, but I want to keep it simple so you can expand on the code.
There are going to be many ways of doing this. The correct way will depend on what else you want to happen.
displayMode(FULLSCREEN)
function setup()
val=1 -- value and direction to move c1
start=vec2(500,100) -- starting position of c1
c1=physics.body(CIRCLE,50) --create circle c1
c1.x=start.x
c1.y=start.y
c1.gravityScale=0
c2=physics.body(CIRCLE,50) -- create circle c2
c2.x=100
c2.y=600
c2.gravityScale=0
end
function draw()
background(40, 40, 50)
fill(255)
ellipse(c1.x,c1.y,100) -- draw circle c1
ellipse(c2.x,c2.y,100) -- draw circle c2
c1.y=c1.y+val -- move c1 up or down the screen
c2.x=c2.x+1 -- move c2 across the screen
if c1.y<=start.y then -- c1 back at starting position
val=1 -- set value to move c1 up the screen
c2.x=100 -- reset c2 back to it starting position
c2.y=600
end
end
function collide(c)
if c.state==BEGAN then -- check for collision
val=-1 -- reverse direction
end
end
Here, this isnât working. Iâll send the code I have currently:
--main--
function setup()
parameter.integer("Speed", 1, 100, 6)
parameter.integer("Gravity",1, 100, 5) -- Not really "gravity." Just a force you work against
Tower= Tower() -- "point b" goal
Runner= Runner()-- player
spx = 100
spy = 100
watchstuff()
end
function draw()
background(0,0,0)
sprite("Documents:background", 400, 100, 2000,2000)
spy = spy - Gravity
Tower: draw()
Runner: draw()
sprite("Documents:yellow", 300, 800, 2000, 5)
end
function touched (touch)
spy = spy + Speed
end
function watchstuff()
parameter.watch("spx"). --To watch where runner is
parameter.watch("spy")
end
--Tower--
Tower = class()
function Tower:init(x)
self.x = x
end
function Tower:draw()
sprite("Documents:Tower", 400,900, 1024/2,768/2)
end
function Tower:touched(touch)
end
--Runner--
Runner = class()
function Runner:init(x)
self.x = x
end
function Runner:draw()
sprite("Documents:Runner", 400, spy, 768/8,1024/8)
end
function Runner:touched(touch)
end
I want the runner to move to point b and instantly move back to point A( beginning location).
@tyguy2 I believe this does what you want,
I pointed out changes with comments
Most notably I changed the positions and sizes of tower and runner to be self.variables, and changed spx and spy to be runner.x and runner.y
Also, I changed the sprites so that I could see what was happening, youâll need to change them back
--# Main
function setup()
parameter.integer("Speed", 1, 100, 6)
parameter.integer("force",1, 100, 5) -- Just a force you work against
tower = Tower(400,900) -- "point b" goal
runner = Runner(400, 100)-- player
parameter.watch("runner.x") -- change
parameter.watch("runner.y") -- change
end
function draw()
background(0,0,0)
sprite("SpaceCute:Background", 400, 100, 2000,2000)
runner.y = runner.y - force -- change
tower:draw()
runner:draw()
sprite("Planet Cute:Grass Block", 300, 800, 2000, 5)
end
function touched(touch)
runner.y = runner.y + Speed -- change
end
--Tower--
Tower = class()
function Tower:init(x, y)
self.x = x -- set position
self.y = y
self.width = WIDTH/2 -- set size
self.height = HEIGHT/2
end
function Tower:draw()
sprite("Small World:Church", self.x,self.y, self.width,self.height)
end
function Tower:touched(touch)
end
--Runner--
Runner = class()
function Runner:init(x, y)
self.x = x -- set position
self.y = y
self.initialY = y -- set starting position as variable
end
function Runner:draw()
if self.y >= tower.y - tower.height/2 then -- if reached tower
self.y = self.initialY -- set position back to starting position
end
sprite("Planet Cute:Character Boy", self.x, self.y, HEIGHT/8,WIDTH/8) -- change
end
function Runner:touched(touch)
end
1.) Everything is smushed. I am trying to fix it as we speak. If you can help, the runnerâs dimensions are as follows:
768 * 1024
The towerâs are:
1024 * 768
2.) When the runner returns to point A, is it possible to put something down for him to âstandâ on. I continually have to problem of him falling of screen.
function setup()
parameter.integer("Speed", 1, 100, 6)
parameter.integer("force",1, 100, 5) -- Just a force you work against
tower = Tower(400,900) -- "point b" goal
runner = Runner(400, 100)-- player
parameter.watch("runner.x") -- change
parameter.watch("runner.y") -- change
end
function draw()
background(0,0,0)
sprite("SpaceCute:Background", 400, 100, 2000,2000)
runner.y = runner.y - force -- change
runner.y = math.max(runner.y, runner.height/2)
tower:draw()
runner:draw()
sprite("Planet Cute:Grass Block", 300, 800, 2000, 5)
end
function touched(touch)
runner.y = runner.y + Speed -- change
end
--Tower--
Tower = class()
function Tower:init(x, y)
self.x = x -- set position
self.y = y
self.width = WIDTH/2 -- set size
self.height = HEIGHT/2
end
function Tower:draw()
sprite("Small World:Church", self.x,self.y, self.width,self.height)
end
function Tower:touched(touch)
end
--Runner--
Runner = class()
function Runner:init(x, y)
self.x = x -- set position
self.y = y
self.initialY = y -- set starting position as variable
self.width = HEIGHT/8 -- set runner size
self.height = HEIGHT/8
end
function Runner:draw()
if self.y >= tower.y - tower.height/2 then -- if reached tower
self.y = self.initialY -- set position back to starting position
end
sprite("Planet Cute:Character Boy", self.x, self.y, self.width,self.height) -- change
end
function Runner:touched(touch)
end
To size the tower and runner correctly, simply change the width and height in each of their init functions