Is there any way to animate two physics objects when they collide in 2D? For example, if I want a circle to hit another circle in the scene, is there any way to somehow make them bounce back like tweening or something?
@Creator27 When using physics objects, it’s best to let them do what the physics code makes them do. Don’t try to override it and make them go in a direction you want. Unless you use physics commands to alter their speed or direction.
if you want hit detection you can use physics body with sensor=true, this makes them detect collisions but not respond to them, then you can fire off a tween on the sprite to do your own bounce animation
otherwise you are stuck to the physics world, which does bouncing and collisions normally, and you can draw the physics bodies just check out Examples Physics Lab and Basic Physics
you can also check out my experiment project here - https://codea.io/talk/discussion/11504/physics#latest
@skar Thanks for the info. I never knew about the “sensor” variable. I probably looked at it a long time ago as I looked thru the physics stuff, but never tried it. I’ve always just let the physics code do it’s stuff.
One more question @skar. What would I write in as the subject for the tweening?
@Creator27 Not sure why you want to override the physics code, but here’s an example of overriding the code. After the top circle hits the bottom one, it uses the path tween to move around the screen. It then repeats the process.
viewer.mode=FULLSCREEN
function setup()
c1 = physics.body(CIRCLE,50)
c1.x=WIDTH/2
c1.y=HEIGHT-50
c1.sensor=true
c2 = physics.body(CIRCLE,50)
c2.x=WIDTH/2
c2.y=100
c2.type=STATIC
stroke(255)
strokeWidth(1)
noFill()
end
function draw()
background(30, 30, 30, 25)
ellipse(c1.x,c1.y,100)
ellipse(c2.x,c2.y,100)
end
function collide(contact)
if contact.state == BEGAN then
p1={x=c1.x,y=c1.y}
p2={x=100,y=600}
p3={x=WIDTH,y=HEIGHT-50}
p4={x=WIDTH/2,y=HEIGHT-50}
physics.gravity(0,0)
tween.path(10,c1,{p1,p2,p3,p4})
end
end
function collide(contact)
if contact.state == BEGAN then
p1={x=c1.x,y=c1.y}
p2={x=100,y=600}
p3={x=WIDTH,y=HEIGHT-50}
p4={x=WIDTH/2,y=HEIGHT-50}
--physics.gravity(0,0)
c1.type = STATIC
tween.path(10,c1,{p1,p2,p3,p4}, nil, function() c1.type = DYNAMIC end)
end
end