Math crash

I have a section of code in Golem Particle that is supposed to make a random x and y within a radius, and it works. It’s used to set the beginning particle’s x and y. I am trying to add a new vent style where you can specify the ending point too, though. So I thought, “If you can make a random x and y within a radius to appear within, why can’t you create a random x and y within a radius to transition.to?” So I made the exact same function with an endRadius instead of just a radius. Here comes my problem. Every time I use the function, the corona simulator crashes and I get the spinning beach ball. The appear within radius function works fine. Just the transition radius doesn’t. Here’s my code for the random position:

[lua]local function newCircleEmit()
local function burst()

if (random) then
r1, r2, g1, g2, b1, b2, a1, a2=math.random(255), math.random(255), math.random(255), math.random(255), math.random(255), math.random(255), math.random(255), math.random(255)
end

local color=newFillColor({r1, r2}, {g1, g2}, {b1, b2}, {a1, a2})
local r=color[1]
local g=color[2]
local b=color[3]
local a=color[4]

local X1 = math.random(-startRadius, startRadius)
local Y1 = math.random(-startRadius, startRadius)

local X2 = math.random(-endRadius, endRadius)
local Y2 = math.random(-endRadius, endRadius)

while math.sqrt( (X1 - ventPoint.x)*(X1 - ventPoint.x) + (Y1 - ventPoint.y)*(Y1 - ventPoint.y)) > startRadius do
X1 = math.random(-startRadius, startRadius)
Y1 = math.random(-startRadius, startRadius)
end
while math.sqrt( (X2 - endPoint.x)*(X2 - endPoint.x) + (Y2 - endPoint.y)*(Y2 - endPoint.y)) > endRadius do
X2 = math.random(-endRadius, endRadius)
Y2 = math.random(-endRadius, endRadius)
end

local particleRadius=math.random(pRadius1, pRadius2)

particle=display.newCircle(0, 0, particleRadius)
vent:insert(particle)
particle:setFillColor(r, g, b, a)
particle.x, particle.y=X1, Y1

local function killParticle(self)
self:removeSelf()
self=nil
end
transition.to(particle, {alpha=endAlpha, time=life, onComplete=killParticle})

transition.to(particle, {y=Y2, x=X2, time=particleSpeed})
end
timer.performWithDelay(0, burst, numParticles)
end[/lua]

What I do earlier is make an endPoint for the radius to be drawn around (you can specify the size) and set it’s x and y to the ending x and y you specify. If you need any more code just let me know. Any help would be appreciated very much.

binc [import]uid: 147322 topic_id: 28585 reply_id: 328585[/import]

It works with anything other than the endPoint’s x and y. You can make any value you want, and it works perfectly. But the instant you do endPoint x and y, it crashes. This is really odd.

binc [import]uid: 147322 topic_id: 28585 reply_id: 115558[/import]

You have two transitions applied to same particle, one of them nils the particle at the end, if particle speed is greater than life, you have a problem. Why have two transitions?

Apart from that - use repeat…until instead of while loops, and instead of sqrt compare to the square of radius - pre-calced. Just some optimizations :slight_smile: [import]uid: 160496 topic_id: 28585 reply_id: 117246[/import]

mike470: Thanks so much for responding! I’ve been working on it and I cancel the transitions in the killParticle function, and that’s so that if the particle speed is less than life it cancels, but if the particle speed is greater than life, you get a transition to the endpoint and then it’s still and fades out. No problem there. But could you give me an idea of the code I might use with your ideas?6 [import]uid: 147322 topic_id: 28585 reply_id: 117269[/import]

You do this:

local X1 = math.random(-startRadius, startRadius)  
local Y1 = math.random(-startRadius, startRadius)  
  
while math.sqrt( (X1 - ventPoint.x)\*(X1 - ventPoint.x) + (Y1 - ventPoint.y)\*(Y1 - ventPoint.y)) \> startRadius do  
 X1 = math.random(-startRadius, startRadius)  
 Y1 = math.random(-startRadius, startRadius)  
end  

A more efficient and elegant way:

local X1 local Y1 local rad2=startRadius\*startRadius repeat X1 = math.random(-startRadius, startRadius) Y1 = math.random(-startRadius, startRadius) until (X1 - ventPoint.x)\*(X1 - ventPoint.x) + (Y1 - ventPoint.y)\*(Y1 - ventPoint.y) \<= rad2 [import]uid: 160496 topic_id: 28585 reply_id: 117326[/import]

Um, I’m getting the beach ball with that too.
For some background information, I’m currently building a ‘mortar’ function, where you can specify the launching stuff, and then the explosion. Then it launches and it works fine until the explosion. Then the simulator stops responding and it just sits there and whirs. Do you need some code? [import]uid: 147322 topic_id: 28585 reply_id: 117589[/import]

Ah of course it is an endless loop. The code should be


Until X1*X1+Y1*Y1<=rad2

Can’t uncapitalize that U in Until on my iPad but you know what I mean. [import]uid: 160496 topic_id: 28585 reply_id: 117611[/import]

Great, great, great! Thanks mike470! Resolved. [import]uid: 147322 topic_id: 28585 reply_id: 117734[/import]