简体   繁体   中英

physics.addBody() with custom shapes in Corona SDK

Does the physics.addBody() only work with Circles and Rectangles? Or is it possible to physics.addBody() on vector shapes or sprites or with masks or something?

It is possible to do with sprites and shapes, see here .

local physics=require("physics")
physics.start()
physics.setDrawMode("hybrid")
local obj=display.newRect(160,600,50,50)
physics.addBody(obj,"static", {density=0.1,friction=0.1} )

local obj=display.newCircle(90,500,50)
physics.addBody(obj, "static", {density=0.1,friction=0.1,radius=50} )

--equilateral triangle physics object
local rad=30/360*2*math.pi
local xHalfLen=80*math.sin(rad)
local yLen=80*math.cos(rad)

local obj=display.newLine(100,100, 100+xHalfLen,100+yLen)
obj:append(100-xHalfLen,100+yLen, 100,100)
obj.width=3
obj:setColor(255,120,120)

local triangleShape={0,0, xHalfLen,yLen, -xHalfLen,yLen}    
--center of object is at top of triangle
physics.addBody(obj, {density=0.1,friction=0.1,shape=triangleShape} )

the shapes must be convex and consists of less than 8 sides.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM