Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Does the physics engine handle the squish of a physics body?
Started by troylyndon Mar 16 2017 11:34 AM

16 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

troylyndon

[GLOBAL: userInfoPane.html]
troylyndon
  • Contributor

  • 514 posts
  • Corona SDK

The following code is a basic drop of a ball. But the ball does not squish before bouncing back up. First, does the Corona physics engine have the ability to do the squish, and Secondly, what physics body properties need to be set to make this happen? Can someone add to my code to make this work?

--hide status background
display.setStatusBar(display.HiddenStatusBar)

--ball
local h=display.contentHeight
local circl = display.newCircle( h*.05,h*.05,h*.06,h*.06)
circl:setFillColor( .1,.8,.5 ) 
circl.anchorX = 0; circl.anchorY = 0

--floor
local rect = display.newRect( 0,h-(h*.05),display.contentWidth,h)
rect:setFillColor( .8,.5,.1 ) 
rect.anchorX = 0; rect.anchorY = 0

-- physics
local physics = require("physics")
physics.start()
physics.setGravity(0,9.8)
physics.addBody(circl, {bounce = 0.7})
physics.addBody(rect,"static",{bounce = 0.2})


[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,975 posts
  • Corona SDK

No.  Box2D (which Corona uses) is a rigid body engine.

 

However, there are physics particles that may deform.  I'm not sure about this though as I have never used them except to toy with.

 

 

Note: You can achieve this affect visually, but making the object a animated sprite and playing the right sequence at collision time.


Edited by roaminggamer, 16 March 2017 - 11:55 AM.


[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,975 posts
  • Corona SDK

Alternately, you can use scaling to squish the image.  This won't affect the body shape.



[TOPIC: post.html]
#4

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 409 posts
  • Corona SDK

You can't deform the physics object as, once set, its size and scale remain constant but you can alter the display object.

 

Here is a quick and dirty "squish" effect.  I added a yScale transition that adjusts to the y-velocity of the circle.  I decreased slightly the radius of the circle's physics definition so it "settles" slightly into the floor.

 

Sprites would be another solution and if you are feeling ambitious, maybe you can see if meshes will do anything for you.  Also, if you use a graphic instead of a display circle, the transition will probably look smoother.

 

 

--hide status background
display.setStatusBar(display.HiddenStatusBar)
 
--ball
local h=display.contentHeight
local circl = display.newCircle( h*.05,h*.05,h*.06,h*.06)
circl:setFillColor( .1,.8,.5 ) 
circl.anchorX = 0; circl.anchorY = 0
 
--floor
local rect = display.newRect( 0,h-(h*.05),display.contentWidth,h)
rect:setFillColor( .8,.5,.1 ) 
rect.anchorX = 0; rect.anchorY = 0
 
-- physics
local physics = require("physics")
physics.start()
physics.setGravity(0,9.8)
physics.addBody(circl, {bounce = 0.7, radius = h*0.058})
physics.addBody(rect,"static",{bounce = 0.2})
 
--physics.setDrawMode( "hybrid" )
 
 
 
local function squish( e )
    local c = e.target
    if (e.phase == "began") then
        local vx, vy = c:getLinearVelocity()
        print( "squish time " .. " ".. vy )
        transition.to( c, {yScale = 1 - vy/1000, time = vy/10, onComplete = function()
            transition.to( c, {yScale = 1, time = vy/10} )
        end} )
    end
end
 
circl:addEventListener( "collision", squish )


[TOPIC: post.html]
#5

troylyndon

[GLOBAL: userInfoPane.html]
troylyndon
  • Contributor

  • 514 posts
  • Corona SDK

@roaminggamer - thank you for the clarification on the issue

@sharp100 - thank you for trying to make this work - doesn't seem to for me.

 

I don't know if the display will be fast enough, but it seems to me that the only solution is to make athe physics.circle object have an alpha=.001 and use another display.newCircle object that I can resize and change based upon the movement or collisions of the physics body. Not sure I have the time to give it a try, though.



[TOPIC: post.html]
#6

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,975 posts
  • Corona SDK

precollision event may be part of the solution.  i.e. start to squish before collision.



[TOPIC: post.html]
#7

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,975 posts
  • Corona SDK

In fact, make the body slightly smaller than the shape.

 

 

precollision -- start to squish

collision  - stop squish

postcollision - start expansion, run past, rebound to normal.



[TOPIC: post.html]
#8

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 409 posts
  • Corona SDK

@roamming gamer

 

Yes!  That's why I slightly shrunk the radius of the physics circle.  

precollison and postcollision are great ideas too.

 

Animation would be the best solution (combined with pre and postcollision).



[TOPIC: post.html]
#9

troylyndon

[GLOBAL: userInfoPane.html]
troylyndon
  • Contributor

  • 514 posts
  • Corona SDK

Does anyone know of someone else who has tried this sort of thing?

Animation would always look best, but its not practical for a game with numerous balls that can bounce in various positions, against other objects, and more than one sometimes.



[TOPIC: post.html]
#10

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

@troylyndon,

A "squish"-able body might be able to be constructed using a LiquidFun.

 

https://docs.coronalabs.com/api/type/ParticleSystem/createGroup.html

 

Best regards,

Brent



[TOPIC: post.html]
#11

troylyndon

[GLOBAL: userInfoPane.html]
troylyndon
  • Contributor

  • 514 posts
  • Corona SDK

@Brent - Good idea!



[TOPIC: post.html]
#12

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 409 posts
  • Corona SDK

@troylyndon - Have you see this article ?  It's a  process to unify your liquid fun bodies into a single element visually using snapshots and filters.  It's super cool and could be a good solution for distorting your physics objects due to collisions.  However, I have found it to be processor-intensive so if you have a lot of other things going on, it might be a lot to ask of the CPU.



[TOPIC: post.html]
#13

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 1,852 posts
  • Corona SDK

Pre-rendering is the way forward here I think.... just have an image sheet containing the ball in various stages of squish and switch the animation on when it collides.  You could say have 10 frames (for the squish) and then just reverse the animation to make 20 frames.  If you make the image sheet grey scale you can tint it whatever colour you want.

 

It is then just a question of timing the switch from static animation (i.e. only showing the first frame) to dynamic animation (for the squish) and back to static again.



[TOPIC: post.html]
#14

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 409 posts
  • Corona SDK

That's a good solution but it doesn't cover multiple object collisions ; say one ball getting hit on 2 or 3 sides simultaneously which I think was @troylyndon's reason for not liking animations for the task.



[TOPIC: post.html]
#15

troylyndon

[GLOBAL: userInfoPane.html]
troylyndon
  • Contributor

  • 514 posts
  • Corona SDK

I think the best solution would be for Corona to offer a secondary physics engine that handles non-rigid bodies, too - which could handle squishing many balls into themselves. When Corona was originally developed, phone and tablet CPU speeds could not handle it - but now, they can.

 

For Corona to remain the best 2D engine for games, supporting non-rigid bodies is a must, IMHO.



[TOPIC: post.html]
#16

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 409 posts
  • Corona SDK

@troylyndon  Are there certain engines you have in mind?



[TOPIC: post.html]
#17

troylyndon

[GLOBAL: userInfoPane.html]
troylyndon
  • Contributor

  • 514 posts
  • Corona SDK

@sporkfin, it seems someone else has already asked this question at: https://stackoverflow.com/questions/3269212/non-rigid-body-2d-physics-engines-in-c

 

There are several packages/engines out there that support deformable/soft bodies. If you want something free you can for example check out Phyz, SOFA or Bullet.

There is a detailed listing on wikipedia. Most of these are 3D-based but you can adapt them to a 2D model by setting up the scene as a plane.




[topic_controls]
 
[/topic_controls]