Converting thick line into rectangular body

Sorry if this has been asked before (I've been absent...), but I have a little code which lets me draw a straight line, with width=25.

I'd like to convert what looks like a rectangle being stretched out into a rectangular physics body.

Has anyone done this?

Of course, I'm working on the problem, so once I get something usable, I'll post it...

Thanks,

Matt.

There i a nice code from gerald..
maybe this will help you..
http://developer.anscamobile.com/code/gerald-anderson

Thanks @AlenB, here's my finished code to draw a 20px width line and convert it into a solid object...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require ("physics")
local ui = require("ui")
 
physics.start()
physics.setDrawMode ( "hybrid" ) -- Uncomment in order to show in hybrid mode   
physics.setGravity( 0, 9.8 * 2)
 
physics.start()
 
local bats = {}
 
function main()
        display.setStatusBar( display.HiddenStatusBar )
        Runtime:addEventListener("touch", drawBat)
end
 
local function angleBetween ( srcObj, dstObj )
        local xDist = dstObj.x-srcObj.x ; local yDist = dstObj.y-srcObj.y
        local angleBetween = math.deg( math.atan( yDist/xDist ) )
        if (srcObj.x < dstObj.x) then
                angleBetween = angleBetween+90
        else
                angleBetween = angleBetween-90
        end
        return angleBetween
end
 
local function distanceBetween( point1, point2 )
        local xfactor = point2.x-point1.x ; local yfactor = point2.y-point1.y
        local distanceBetween = math.sqrt((xfactor*xfactor) + (yfactor*yfactor))
        return distanceBetween
end
 
local function createBatsBody( line, ending, type, props )
        local length = distanceBetween( line, ending )
        local angle = angleBetween( line, ending ) + 90
        
        local rect = display.newRect( 0, 0, length, line.width )
        rect.rotation = angle
        rect.x, rect.y = line.x + (ending.x-line.x)/2, line.y + (ending.y-line.y)/2
        
        props.shape = shape
        
        physics.addBody( rect, type, props )
        line:removeSelf()
        return rect
end
 
local delayRemoveLine = function( event )
        event:removeSelf()
end
 
function drawBat( event )
        if (event.phase == "began") then
                local bat = display.newLine( event.x, event.y, event.x, event.y )
                bat.width = 20
                table.insert( bats, 1, bat )
        elseif (event.phase == "moved") then
                local bat = display.newLine( bats[1].x, bats[1].y, event.x, event.y )
                bat.width = 20
                bats[1]:removeSelf()
                bats[1] = bat
        else
                if (#bats == 1) then
                        bats[1] = createBatsBody( bats[1], event, "static", {} )
                else
                        bats[1] = createBatsBody( bats[1], event, "dynamic", {} )
                end
                
                bats[1].timer = delayRemoveLine
                timer.performWithDelay( 5000, bats[1] )
        end
end
 
main()
views:2194 update:2011/10/4 17:12:07
corona forums © 2003-2011