Is there a way to do collision detection without the overhead of the whole physics engine? Also, is there a way to do pixel-perfect collision detection?
Label: corona android iphone ipad building error inmobi problem display screen View image tableView failed object physics touch button game lua config event listener |
Collision detection without physics
Sure why not- you know the coords of the objects so you an just compare coords, then if they overlap and they are objects that should collide with each other, call some event handler. Of course, you have to implement gravity, friction, etc all by yourself.
I dont know about "pixel-perfect" but you can definitely pass a description of a shape (by its points) into the definition of a physical body.
@Hookflash: There is no pixel perfect collision detection in Corona. Collision detection without the the physic engine? Sure, via your LUA code.
Simple techniques, check if the rectangles (bounding spaces) of your object intersect, in Objective-C you have
1 | CGRectIntersect (rect1, rect2) |
One additional note. If your objects are in different groups, the x/y values will be relative to the group and not the global screen coordinates. You can use object.stageBounds to find the x/y value in the global space.
-Tom
Hey not sure if anyone is still looking at this thread but I too am trying to get some simple collision detection working without using the physics engine.
I have objects moving down the screen and have a movable object towards the bottom of the screen you move around with the accelerometer. All I want to do is detect when the objects moving down the screen intersect with the object you are moving with the accelerometer. I tried using the code above but I guess I am confused on where to implement it, I am new to Corona and Lua (only been using it for about a week).
Here is the code I have so far:
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 | -- Get rid of the status bar display.setStatusBar( display.HiddenStatusBar ) -- So the screen stays active system.setIdleTimer( false ) movable_object = display.newRect( 200, 0, 50, 50 ) movable_object:setFillColor( 255, 255, 255 ) movable_object.x = 240 movable_object.y = 210 local function movebox() xcord=math.random(80, 375) square = display.newRect( 200, 0, 50, 50 ) square:setFillColor( 0x31, 0x5a, 0x18 ) square.x = xcord square.y = -25 local w,h = display.contentWidth, display.contentHeight local transitiontime=3800 local delaytime=math.random(300, 3000) transition.to( square, { time=transitiontime, transition=easing.linear, delay=delaytime, y=(h+50), onComplete=movebox } ) end xcord=math.random(80, 375) local square = display.newRect( 200, 0, 50, 50 ) square:setFillColor( 0x31, 0x5a, 0x18 ) square.x = xcord square.y = -25 local w,h = display.contentWidth, display.contentHeight local transitiontime=3800 local delaytime=math.random(300, 3000) transition.to( square, { time=transitiontime, transition=easing.linear, delay=delaytime, y=(h+50), onStart=movebox } ) function onTilt( event ) --moves object movable_object.x = (movable_object.x - event.yGravity * 40) --so object doesn't go off screen if (movable_object.x < 25) then movable_object.x = 25 end if (movable_object.x > 455) then movable_object.x = 455 end local sqrt = math.sqrt local dx = movable_object.x - square.x; local dy = movable_object.y - square.y; local distance = sqrt(dx*dx + dy*dy); if distance < 32 then transition.cancel(square) end end system.setAccelerometerInterval( 65 ) Runtime:addEventListener( "accelerometer", onTilt ) |
Hi Clark,
I have not run your code, but from the top of my head.
1. Have a flag that states that your square has collided and hence must stop moving.
You can calling the MoveBox() method and when the transition ends, you are calling it again and it keeps repeating.
Now in the accelerometer method, set a flag, eg.
hasCollided = true
in the moveBox(), have the first line
if hasCollided then return end
and before the function definition of MoveBox, place a line
local hasCollided = false
cheers,
Jayant C Varma
Thanks Jayant for your quick reply, I will try to implement your suggestions, still trying to figure everything out with Corona.
Just to clarify I am calling the movebox() method over and over to continually create squares at random x coordinates and move them down the screen. For the point of this demonstration the purpose of this game would be to move your object (the white square) to intersect with the green squares that are falling down to gain points and each time you do that the transition ends (and maybe that square disappears?) but the movebox() method continues to create the moving of the squares down the screen so you can intersect with more squares to gain more points, does that make sense at all?
Thanks,
-Clark
Ah, OK!!
Sometimes it is not very clear on what someone is trying to do.
so, if I get that right
1. You have squares that keep falling off the top (let's say) of the screen to the bottom, these are what you refer to as the Green Squares
2. You have a White Square that is controlled by the accelerometer that should intersect, or almost let's say collect the green squares.
3. When the Green and the White square collide, you want to increase the score and hide the green square or take it off the screen.
In my opinion, here's how I would approach it,
1. Have a onEnterFrame event (the heartbeat of the game)
which will determine if the game is running or over
and will keep spawning the green boxes at a random rate, these will have a transitionTo function that will move them slightly at random speed, and onComplete will run the function CheckForCollision
2. the Accelerometer handling routine (you already have that)
will move the white square
3. The CheckForCollision Routine will check that the green square and the white square have collided or not, the movement specified has to be such that the squares should not have passe each other significantly before the onComplete fires. This will if the squares collide, removeSelf and increase the score, otherwise it will move itself again for a small distance at the end of which it will spawn CheckForCollision onComplete.
Does that help/make sense?
You could also have the checkForCollision in the whiteBoxMove, but this in my opinion is more CPU intensive as you will check for every square at every movement, where as the way suggested above, the square that completes the movement is checked against the white square only.
cheers,
Jayant C Varma
That makes perfect sense, I didn't even think of doing it that way but it makes perfect sense, thanks for your help!
you are welcome
?:)
cheers,
Jayant C Varma
Hello,
following the thread about collision without physics... Is there any way to have collision detection with a polyline object? If yes, is there anywhere I could check a sample? I'm new in Corona and have no idea what LUA is.
Thank you
Don't know anything about polylines, sorry. For rectangular shapes I wrote this function to do simple collision detection without physics:
http://developer.anscamobile.com/code/flashs-hittestobject-emulated-using-contentbounds
For circular shapes, get the distance between them as jayantv described.
I was looking for something similar like a month ago, i was looking for a collision event without bouncing or without any physics behaviour by default, after searching a bit, i found the atribute "Sensor" in the physics engine.
A sensor is an object listening for a collision event with other sensor/physicsbody, the object can launch collision events, but wont act as a body, so it wont bounce with other objects, the objects collisioning a sensor won't bounce either, that way i could made some "coins" in the map, so my main character can pick them =), is a good way to make coins, items, special zones in the map and others.
Finally i ended using some atributes of the physics engine to make my game more realistic, and for all the other objects used sensors.
Hope this helps someone.
http://developer.anscamobile.com/content/game-edition-physics-bodies#Sensors
Greetings
Joe
When checking for distances with Pythagoras's theorem, don't bother to take the square root. It's a waste of processor. Instead, compare to the squared distance.
For instance, if your circles are 10 pixels in radius, then check for a squared distance of 100 or less.
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed
Android Market Application Error
Corona + pubnub = sample poker game code?
tower defense game help
improve speed from sqlite !!
Translations - audio book of poetry (Italian language) for iOS
Tap Party 2 - Free multiplayer game
Splatz! and Splatz! Light available.
Changing particle emitter position while using SetEmitterTarget()
A note about system requirements