How do i get all the numbers?

I’m assuming you know about greater-than, less-than, and their -or-equal-to counterparts?

local function checkTheDoohickey(event) if box.x \>= -10 and box.x \<= 10 then print("Ya-hori-hei!") end end Runtime:addEventListener("enterFrame", checkTheDoohickey)
  • Caleb

** Update Misunderstood your question.  I thought you wanted to ensure you randomly got all values in a range.  Will leave this answer anyways **

Use a ‘shuffle bag’

http://gamedevelopment.tutsplus.com/tutorials/shuffle-bags-making-random-feel-more-random–gamedev-1249

Back to your original question.  Sounds like you want to randomly select a range and check for a match against that range.

-- I don't know the constraints for your range so I'm making my own up. -- local low = math.random( -10 , -1) local high = math.random( 0, 10 ) if( box.x \>= low and box.y \<= high ) then print("BUBBA") end

Wouldn’t this be a use for queryRegion?  https://docs.coronalabs.com/api/library/physics/queryRegion.html

Rob

Hi. You expressed some interest in C++ a few weeks back. Given that, all kinds of great intersection code can be found at David Eberly’s site. He also has an excellent book along the same lines.

Basically, in the case of a box, you’re trying to find the best intersection among four line segments. You can do this by interpreting the box as moving toward the line, or vice versa. The latter is simply a matter of negating the velocity vector.

If the box is axis-aligned and can’t rotate, and any line is either horizontal or vertical, this is quite easy, since you can check one component at a time. Going right, for instance, you can do something like the following:

-- We want to find the time, t, when x, moving at speed vx, equals wall\_x -- This is just x + vx \* t = wall\_x... -- Or vx \* t = (wall\_x - x)... -- ...or t = (wall\_x - x) / vx -- Our equation breaks down when vx is almost / exactly zero, i.e. we're barely / not-at-all moving horizontally if math.abs(velocity.x) \> 1e-3 then -- any speed? local t = (wall.x - object.x) / velocity.x if t \>= 0 -- Intersection in direction of motion and t \<= velocity.x \* dt then -- Will hit in the current frame? DoSomething(t) end else -- Not moving, but already touching? end

The other three directions will be similar. If you want to check the intersection for the side of the box, rather than its center, use x + width / 2 instead of just x (or  x - width / 2  when going left, and so on).

Moving at angles, you could actually incur a couple intersections at once. If this matters, you might put any valid intersections into an array first, sort them, then use the first one.

The above sort of logic will handle intersections with infinitely long, axis-aligned lines. For something simple and one-time like going out-of-bounds, this might suffice. If these are short segments, you’ll need to bounds-check the intersections to make sure they are both on one of your box’s sides as well as within the segment itself. If the lines can be angled as well, some additional concepts need to be brought in, but I’ll omit those unless you need them.

Thanks @Caleb P! Your answer was the closest to what i wanted. Although i tried that method when my problem first started but i never got to actually experimenting with it. Again thanks!

–SonicX278

Here’s something related with code translated from this answer on StackOverflow.

local function getSide(x1, y1, x2, y2, pointX, pointY) if x1 == x2 and y1 ~= y2 then if pointX \< x2 then return y2 \> y1 and 1 or -1 elseif pointX \> x2 then return y2 \> y1 and -1 or 1 end elseif y1 == y2 and x1 ~= x2 then if pointY \< y2 then return x2 \> x1 and -1 or 1 elseif pointY \> y2 then return x2 \> x1 and 1 or -1 end end local slope = (y2 - y1) / (x2 - x1) local yIntercept = y1 - x1 \* slope local solution = (slope \* pointX) + yIntercept if pointY \> solution then return x2 \> x1 and 1 or -1 elseif pointY \< solution then return x2 \> x1 and -1 or 1 end return 1 end local l1, l2 = display.newCircle(math.random(0, display.contentWidth), math.random(0, display.contentHeight), 10), display.newCircle(math.random(0, display.contentWidth), math.random(0, display.contentHeight), 10) local p = display.newCircle(display.contentCenterX, display.contentCenterY, 20) display.getCurrentStage():setFocus(p) local previousSide = getSide(l1.x, l1.y, l2.x, l2.y, p.x, p.y) function p:touch(event) p.x, p.y = event.x, event.y local side = getSide(l1.x, l1.y, l2.x, l2.y, p.x, p.y) if side ~= previousSide then print("Passed!") p.xScale, p.yScale = 1.5, 1.5 if p.transition then transition.cancel(p.transition) end p.transition = transition.to(p, { xScale = 1, yScale = 1, transition = easing.outElastic, time = 500 }) end previousSide = side if side == 1 then p:setFillColor(0.8, 0.8, 1) else p:setFillColor(1, 1, 0) end end p:addEventListener("touch") if previousSide == 1 then p:setFillColor(0.8, 0.8, 1) else p:setFillColor(1, 1, 0) end

To recap the code, basically all you need to see if you’ve passed a line is a function to tell you which side of the line you’re on. Then, store the previous side and check for if the current side is different from the previous side. When it changes, you’ll know you passed it.

This which-side-of-line can also be expanded to solve your original problem in a more robust way (probably a little slow, though): Calculate the perpendiculars to your two lines and see if you’re on the [x] side of one line and the [y] side of the other.

Something to note is that this passing code doesn’t tell you whether you’re between the two points - all it does is tell when you’ve passed a line from P1 to P2. This isn’t the same as a line segment : using this approach, when you pass outside the points you’ll still trigger the pass routine.

  • Caleb