Total Pageviews

Tuesday, May 27, 2014

នេះជា Type Body របស់ Corona SDK (1)

នេះជា Type Body របស់ Corona SDK
វាជា body objects ដែលមាន interact ជាមួយនឹង physics world នៅក្នុង Corona SDK វាភ្ជាប់ជាមួយនឹង display objects ហើយត្រូវបានបង្កើតដោយប្រើជាមួយនឹង​ physics.addBody()
Properties (inherit from object)
object.angularDamping
វាជាតំលៃនៃចំនួនលេខថាតើចំនួន body’s rotation ដែលត្រូវ dampeddefault គឺសូន្យ វាគ្មានតំលៃតូចឬធំនោះទេ អាចដាក់លេខតាមបាន
សូមមើលកូដះ
--Create a rectangle
local myRect = display.newRect(0, 0, 100, 100)
--Add a body to the rectangle
physics.addBody(myRect, "kinematic", {isSensor = false})
--Set the rectangle's angular damping
myRect.angularDamping = 5
-- (Optional) assign a local variable to hold the angular damping value
local d = myRect.angularDamping

object.angularVelocity
វាជាចំនួនលេខនៃតំលៃរបស់ angular (rotational) velocity, ដែលគិតជាដឺក្រែក្នុងមួយវិនាទី។ អាចដាក់ចំនួនបានតាមចិត្ត
សូមមើលកូដះ
--Create a rectangle
local myRect = display.newRect(0, 0, 100, 100)
--Add a body to the rectangle
physics.addBody(myRect, "kinematic", {isSensor = false})
--Set the rectangle's angular velocity
myRect.angularVelocity = 50
-- (Optional) assign a local variable to hold the angular velocity value
local d = myRect.angularVelocity

object.bodyType
វាជា String value សំរាប់ប្រភេទនៃ physical body ដែលត្រូវ simulated។ តំលៃរបស់វាអាចមានះ
Dynamic:​ សំរាប់ body type មួយនេះត្រូវបាន fully simulated។ វាអាចធ្វើចលនាបាននៅក្នុងកូដ ប៉ុន្តែវាអាស្រ័យលើកំលាំងដូចជា gravity or reactionary collision forces។ ហើយវាក៏ជា default body type សំរាប់ physics body នៅក្នុង Box2D។ សំរាប់ dynamic bodies វាអាច collide ជាមួយនឹងគ្រប់ទាំង body types
Static: វាមិនត្រូវបានធ្វើចលនានៅក្រោម simulation ហើយវាធ្វើដូចជា បើសិនជាមាន infinite massstatic bodies អាចធ្វើចលនាបានដោយសារអ្នកប្រើប្រាស់ ប៉ុន្តែវាមិនទទួលនៅ application របស់ velocity នោះទេ។ សំរាប់ static bodies collide ជាមួយនឹងតែ dynamic bodies ទេ ដោយវាមិន collide ជាមួយនឹង static or kinematic bodies នោះទេ
Kinemetric:​វាធ្វើចលនានៅក្រោម simulation តាមរយះ velocity។ ហើយវាមិនមានការឆ្លើយតប់នឹងកំលាំង gravity ទេ។ វាអាចធ្វើចលនាបានតាមរយះអ្នកប្រើប្រាស់ ប៉ុន្តែទូទៅត្រូវបានធ្វើចលនាដោយការបញ្ចូលនៅ velocities។ សំរាប់ kinematic bodies វា collide តែជាមួយនឹង dynamics body ទេ
ចំណាំះ
មិនអាចប្តូរនៅប្រភេទ physics body type បានទេនៅពេលដែល collision event វាអាចបានលុះត្រាតែប្រើ slight (un-noteable) delay សូមមើលកូដះ
-- Collision event
local function delay()
    -- Change the body's type to static
    body.bodyType = "static"
end
timer.performWithDelay( 10, delay )

Example
 --Create a rectangle
local myRect = display.newRect( 0, 0, 100, 100 )
--Add a body to the rectangle
physics.addBody( myRect, { isSensor = false } )
--Set the body's type to kinematic
myRect.bodyType = "kinematic"
-- (Optional) store the body's type in a local variable
local currentType = myRect.bodyType


object.gravityScale
គេប្រើវាសំរាប់ adjust នៅ gravity នៅលើ single body, ex: បញ្ចូលវាទៅសូន្យដើម្បីធ្វើឪ្យវាអណ្តែត​(no gravity) តំលៃដើមគឺ 1.0 មានន័យថា normal gravity។ សូមចាំទុកថាះ បន្ថែម gravity វាអាច decrease stability
សូមមើលកូដះ
local physics = require( "physics" )
physics.start()
local rect = display.newRect( 0, 0, 50, 50 )
physics.addBody( rect, "dynamic" )
rect.gravityScale = 0.2

object.isAwake
វាជា Boolean សំរាប់ bodies ក្នុងការ awake state។ ដោយតំលៃដើម គ្រប់ទាំង bodies aumatically “go to sleep” នៅពេលគ្មានអ្វី interacts ជាមួយនឹងពួកវាសំរាប់ a couple seconds ហើយបន្ទាប់មកពួកវាបានឈប់ simulated រហូតទាល់តែមាន collide ដែលធ្វើឪ្យពួកវា wake up។ សំរាប់ property របស់វាអាចជា fetch their current state or forcibly wake them up
សូមមើលកូដះ
--Create a rectangle
local myRect = display.newRect(0, 0, 100, 100)
--Add a body to the rectangle
physics.addBody(myRect, "kinematic", {isSensor = false})
--Set the rectangle's awake state to true
myRect.isAwake = true
-- (Optional) assign a local variable to hold the awake state
local state = myBody.isAwake

object.isBodyActive
ប្រើសំរាប់បញ្ចូលឬទទួលនៅ current bodies ដើម្បី  active state។ សំរាប់ inactive bodies មិនត្រូវបានបំផ្លាញ់ទេ ប៉ុន្តែត្រូវបាន​ removed ចេញពី simulation & cease ក្នុងការ interact ជាមួយនឹង other bodies
ចំណាំះ មិនអាចប្តូរនៅ body’s active state ក្នុងអំឡុងពេលដែល collision event ទេ។ តែអាចប្តូរនៅ body’s active state អំឡុងពេលដែល collision event បើសិនជាប្រើនៅ slight delay
 សូមមើលកូដះ
--Collision event
local function delay()
    --Change the body's active state to false
    body.isBodyActive = false
end
timer.performWithDelay( 10, delay )

Example
 --Create a rectangle
local myRect = display.newRect(0, 0, 100, 100)

--Add a body to the rectangle
physics.addBody(myRect, "kinematic", {isSensor = false})
--Set the rectangle's body active state
myRect.isBodyActive = true
-- (Optional) assign a local variable to hold the angular damping value
local value = myRect.isBodyActive

object.isBullet
វាជា Boolean ថាតើ body ត្រូវបាន treated ជា “bullet” សំរាប់ bullets គឺជា subject ក្នុងការបន្តនៅ collision detection ជាជាងអំឡុងពេល collision detection at world timesteps។ វានឹងការពារនៅការធ្វើចលនា fast moving objects ចេញពី passing through solid barriers។ តំលៃដើមគឺ false
សូមមើលកូដះ
local physics = require( "physics" )
physics.start()
local bullet = display.newCircle( 100, 100, 10 )
physics.addBody( bullet, "dynamic", { radius=10 } )
bullet.gravityScale = 0
-- Make the object a "bullet" type object
bullet.isBullet = true
bullet:setLinearVelocity( 800,0 )
object.isFixedRotation
វាជា Boolean ថាតើរ rotation របស់ body ត្រូវ locked បើសិនជា body គឺនៅក្រោម load or subjected ទៅកាន់ off-center forces។ តំលៃដើមគឺ false
សូមមើលកូដះ
myBody.isFixedRotation = true
local state = myBody.isFixedRotation






No comments:

Post a Comment