Distance Joint .... collision event

Hi
Can I call distance Joint on collision event. ? Is a different technique to accomplish this or is impossible in general.
I get this error :

ERROR: physics.newJoint() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event

[import]uid: 13156 topic_id: 16852 reply_id: 316852[/import]

ok I solved this…
Corona documentation …

"Important Restriction: Collision handling

Currently, the Box2D physics engine will reliably crash during a collision if Corona code attempts to modify objects still involved in the collision (since Box2D is still working out iterated mathematics on them).

The standard workaround is to “wait a bit”:

Do not modify / create / destroy physics objects during a collision, on penalty of crashing.
If you need to modify / create / destroy an object as a result of a collision, your collision handler should set a flag or add a time delay so the change can occur later, e.g. with timer.performWithDelay()."

I used timer.performWithDelay and its ok.
[import]uid: 13156 topic_id: 16852 reply_id: 63110[/import]

Yes, I remember this well. I was getting crashing nonstop, then just delayed it and it was fine.
Box2d is cool, but has little odd things going on with it :slight_smile:

ng [import]uid: 61600 topic_id: 16852 reply_id: 63121[/import]

Hi nicholas
do you know how to draw a line acting like a physics distance joint ?
This line should by redrawing all the time ? Any trace ? [import]uid: 13156 topic_id: 16852 reply_id: 63346[/import]

Well, this code allows you to touch the screen and draw a line. It’s a physics body so if you draw the line, any other physics body will react with it.

This line of code, I found if you increase it (default is 8) physics interactions are a lot more accurate (ie, prevent things from going through walls). Just mentioning this, save you some sanity later on.

physics.setPositionIterations(32)
Here is the sample code:

Do what you please, most of it is from someone else and then I integrated it. This is before my integration, and the code is completely changed, but this is what I started with.

-Ng

[code]
–// Code modified from an example from a dude named “gander”
–// I can’t remember where I got it. I modified it to my own use in my game coming out in Dec 2011.
–// I’m throwing credit where credit is due, just don’t remember hehe :slight_smile:

local physics = require(“physics”)
physics.start()

physics.setPositionIterations(32)
–“hybrid” “debug” “normal”
physics.setDrawMode(“hybrid”)

local _W = display.contentWidth
local _H = display.contentHeight

local linesConstruct = {}
local lineAmount = 1
local line_width = 10
local B4_x, B4_y, ball

local function TouchDraw(event)

if event.phase == “began” then
B4_x = event.x
B4_y = event.y

elseif event.phase == “moved” then
linesConstruct[lineAmount] = display.newLine(B4_x, B4_y, event.x, event.y)
linesConstruct[lineAmount]:setColor(math.random(255), math.random(255), math.random(255))
linesConstruct[lineAmount].width = line_width
dist_x = event.x - B4_x
dist_y = event.y - B4_y

–Polygon line that’s a physics body
physics.addBody(linesConstruct[lineAmount], “static”, { density = 1, friction = 0.1, bounce = 0, shape = {0, 0, dist_x, dist_y, 0, 0} } )
B4_x = event.x
B4_y = event.y
lineAmount = lineAmount + 1
elseif event.phase == “ended” then
end
end

Runtime:addEventListener(“touch”, TouchDraw)

[/code] [import]uid: 61600 topic_id: 16852 reply_id: 63371[/import]

Hi thanks nicholas … but I need a line exactly like my distance joint.

For example when the object is close enough to another object it’s creates physics distance Joint and I want to see this Joint this line which connect two objects, like you can see in debug mode.

Anyway thank you , I will take a look on yours linesConstruct. [import]uid: 13156 topic_id: 16852 reply_id: 63373[/import]

Ahh,
Yea I Know EXACTLY what you want.
I’ve been struggling with that piece, I had a game idea based on touch joints utilizing distance joints and I could not figure it out.

I’ve seen it done, I just don’t know how to do it :expressionless:

If I do I’ll post, I love helping even though I am a beginner with coding (started June 2011, from nothing at all no coding EVER!).

:slight_smile:

[import]uid: 61600 topic_id: 16852 reply_id: 63375[/import]

he he cool thanks :slight_smile:
I’am trying to resolve this too…
Any one can help us ? :slight_smile: [import]uid: 13156 topic_id: 16852 reply_id: 63378[/import]

nicholasclayg , PiotrT, did you ever figure this out? I’m just wondering, if not then I am going to take a crack at it.

Let me know.

Larry [import]uid: 11860 topic_id: 16852 reply_id: 67045[/import]

see if this helps, basically a circle / ballon hovering over a platform connected by a joint.

The line that connects the two is redrawn based on the xpos of the circle, in the enterframe event.

[code]
display.setStatusBar( display.HiddenStatusBar )
_W = display.contentWidth
_H = display.contentHeight

local W = _W
local H = _H

_G.physics = require( “physics” )
physics.start()

physics.setGravity( 0, -9.8)
–physics.setDrawMode( “hybrid” )

local myCircle = display.newCircle( 100, 100, 30 )
local myRectangle = display.newRect(50,display.contentHeight-50, 150, 50)

myCircle:setFillColor(255,33,225)

myRectangle.strokeWidth = 3
myRectangle:setFillColor(255, 0, 0)
myRectangle:setStrokeColor(255, 0, 0)

physics.addBody( myCircle, { density=0.9, friction=0.3, bounce=0.3} )
physics.addBody( myRectangle, “static”, { friction=0.5, bounce=0.3 } )

myCircle.x = myRectangle.x; myCircle.y = myRectangle.y-150;
myCircle.x = myRectangle.x
myCircle:setReferencePoint(display.BottomCenterReferencePoint);
local myline = display.newLine(myCircle.x,myCircle.y, myRectangle.x,myRectangle.y )
myline:setColor( 255, 255, 0, 255 )
myline.width = 3

–Wheel Joint
local myJoint = physics.newJoint(“wheel”, myCircle, myRectangle, myCircle.x, myCircle.y, 1, 1)

myJoint.isMotorEnabled = false;
myJoint.motorSpeed = 0;

–Limit the distance that myCircle will travel from jointed object (currentXpos, 100px up)
myJoint.isLimitEnabled = true
myJoint:setLimits(0, 100);

local myListener = function( event )

–remove and redraw line while the Circle moves
if myline ~= nil then
myline:removeSelf()
myline = display.newLine(myCircle.x,myCircle.y, myRectangle.x,myRectangle.y )
myline:setColor( 255, 255, 0, 255 )
myline.width = 3
myCircle:toFront()
end
end
Runtime:addEventListener( “enterFrame”, myListener )
[/code] [import]uid: 11860 topic_id: 16852 reply_id: 67066[/import]