Total Pageviews

Monday, April 21, 2014

physics.rayCast()

physics.rayCast()
វាជាហ្វាំងសិនដែលប្រើក្នុងស្វែងរក Object that collide ជាមួយនឹង​line ហើយនឹង the collision points ជាមួយនឹងបន្ទាត់។​ ទីតាំងវានឹងត្រឡប់នៅក្នុង content space។ ជាធម្មតាវាត្រឡប់ក្នុង locals-pace
ចំណាំះ
បើសិនជាចំនុចចាប់ផ្តើមនៅក្នុង Object, no hit will be registered for that object
Syntax:
local hits = physics.rayCast( from_x, from_y, to_x, to_y, "closest" )
from_x (required)
from_y (required)
to_x (required)
to_y (required)
behavior (optional) String. The collision test behavior.
In increasing order of performance cost:
"any" (fastest/least expensive) : Return one result, not necessarily the closest one.
"closest" (if unspecified, this is the default behavior) : Return only the closest hit from the starting point, if any.
"unsorted" : Return all results, in no particular order.
"sorted" (slowest/most expensive) : Return all results, sorted from closest to farthest.
Result properties
hits will be an array of elements containing these properties:
"object" - The DisplayObject colliding with the line.
"position.x" - The X collision position of "object", in content-space.
"position.y" - The Y collision position of "object", in content-space.
"normal.x" - The X component of the normal of the surface hit in "object", in local-space.
"normal.y" - The Y component of the normal of the surface hit in "object", in local-space.
"fraction" - The fraction (0..1), along the ray, where the hit is located. 0 is the start-point of the ray cast. 1 is the end-point of the ray cast. This can be used to sort the results from closest to farthest.

សូមមើលកូដះ
local hits = physics.rayCast( from_x, from_y, to_x, to_y, "closest" )
if ( hits ) then
    -- There's at least one hit.
    print( "Hit count: " .. tostring( #hits ) )
    -- Output all the results.
    for i,v in ipairs(hits)
    do
        print( "Hit: ", i, v.object, " Position: ", v.position.x, v.position.y, " Surface normal: ", v.normal.x, v.normal.y )
    end
    print( "The first object hit is: ", hits[1].object, " at position: ", hits[1].position.x, hits[1].position.y, " where the surface normal is: ", hits[1].normal.x, hits[1].normal.y, " and where the fraction along the ray is: ", hits[1].fraction )
else
    -- There's no hit.
End


No comments:

Post a Comment