Help with Falling and Collision

Can someone help me create a part of code where a certain object falls down to the center of the phone screen from random areas? Think of Radial Gravity. I tried that but it seems to not work out. I need it so From random parts of the screen they fall and once they hit the center(onCollision) it goes to the next scene… I kind of get the “go to next scene” part, but just the objects falling towards the center with the on collision thing…

Please don’t just post links… That’s what I don’t get and need help from an actual person.

Hi @Spid3rtech,

Before we can help you, we need more detail. Do you want the objects to actually be “pulled in” toward the center, like the center is a “magnet” pulling them toward it?

You mention that they “fall from random points”. Does that mean they fall under normal downward gravity, but then if they hit a region near the center of the screen, the scene changes?

Brent

Well, ok. Sorry about that. But this is what I mean. I wanted it so for example, an “Arrow”, yes, getting pulled toward the center of the screen. By spawning at random points I mean that they spawn outside of the screen, in all kinds of directions. So not that they fall only from the top, but from the sides too, and bottom, and not ONLY from those points, just randomly, like the top right, bottom, bottom left, etc. So yeah, think of earth, and a meteor that is coming towards it, falling to the surface, but now think of “Arrows” but that fall randomly around “Earth” to the center. Keep in mind that, that is an example. So when they touch the center object on the screen, it will lead them to the new scene. So Boom, an “Arrow” hit the center, the entire screen clears and goes to the next scene, where the same exact thing happens, but the center object changed.

Sorry for the long post. :confused:

OK, that clarifies it more. Does this “pull toward center” concept need to be the object moving directly in a straight line toward the center? Or would they behave more like satellites which should be pulled toward the center but not necessarily in a direct line?

Yeah, direct line. Also, If the (Example again…)“Tip” of the “Arrow” “Falls” forward toward the ground.(So much quotations xD) I can’t seem to figure that out too… 

How about the rate of “falling”? Is it consistent, or do the arrows need to speed up as if under the force of radial gravity?

Well I want them to fall at a certain speed, but maybe in the next scene they get a little faster, and the rate of spawning is not to much as well.

OK, but consistent speed or gravitational acceleration? If a meteor falls toward earth, it accelerates as it gets nearer because of gravity. If you want that simulation, then the approach is completely different than if you don’t want that behavior.

Brent

Ah, that’s what you were asking. I am thinking of consistent speed. So it falls at a certain speed the whole time, right? Then that’s what I need.

OK, got it. So in that case – and assuming there is no other gravity in the world – then I suggest you use simple transitions from the point the arrows start to the center of the screen. Then use a sensor object (probably a circle) that detect collisions. Is that about what you need?

Yup, that’s basically it. To me it seemed pretty easy. But it’s tougher than I thought it was. I have already tried inputting Radial Gravity and all, but something is wrong with my code, so I just decided to restart. That’s when I posted this.

By saying my code didn’t work, I meant that Radial Gravity worked, but when the object spawn I got an error. But since I have restarted I can’t show you it…

Well, the other error is unrelated it seems. Transitions will solve your main scenario, but you’ll need to use various math formulas to make the speed consistent, since arrows may start further away from the center and the time from point A to point B will never be the same. Also, you’ll need to find the math formula to make the arrows point toward the center… that’s very basic trigonometry and there should be a thousand examples online or several in the Corona forums.

Brent

Yeah I will figure that out. But for now, do you think you can help me get started on the main objective? How they fall towards the center object. With the collision details as well… If you could/have time?

Could anyone help me get started with that?

Here is some basic code that will solve the main problem, it has a few problems like spawning objects inside the screen area and the speed not being consistent but will get you started

local halfHeight = display.contentHeight / 2 local halfWidth = display.contentWidth / 2 local transitionSpeed = 2500 local createdObjects = {} local x = 1 local centerObject = display.newCircle( halfWidth, halfHeight, 25 ) centerObject:setFillColor( 0, 1, 0 ) local function change() print("\<- Change to new scene now -\>") end local function createObject() createdObjects.x = display.newCircle( math.random( -300, display.contentWidth + 300), math.random( -300, display.contentHeight + 300), 25 ) createdObjects.x:setFillColor( 1, 0, 0 ) transition.to( createdObjects.x, { time = transitionSpeed, x = halfWidth, y = halfHeight, onComplete = change }) x = x+1 end timer.performWithDelay( 1000, createObject, 0 )

Thanks! But actually that is kind of different than what I had in mind. I was thinking of something pretty different :stuck_out_tongue:

Hi @Spid3rtech,

The community is here to help, and so are myself and Rob, but generally you won’t get somebody to help you write a specific aspect of your game for you. Everything you describe – and why I helped you describe it in much more detail – is easily accomplished with basic transitions from point A to B, and collisions are easily handled if you follow the guide examples (or a sample project) which shows collisions in action.

The guide on creating physics bodies and detecting collisions are here:

https://docs.coronalabs.com/guide/physics/physicsBodies/index.html

https://docs.coronalabs.com/guide/physics/collisionDetection/index.html

If you prefer to work with actual code instead of reading a guide, we have a sample here:

https://github.com/coronalabs/samples-coronasdk/tree/master/Physics/CollisionDetection

Aside from that, learning these things earlier than later will help you in the long run. Even if somebody writes this code for you, it’ll be best if you learn how to write it yourself. Then, for the next scenario in your game, you’ll have better knowledge of how to approach it.

Take care,

Brent

Thanks for the reply Brent. What if I found the code I erased? If I posted it here or sent it to you by private message, would you be able to help me out? Because I have already read guides. The thing is, it helps me more when someone helps me. Like for instance, when QuizMaster was still active, he helped me code a game, and I learned SO much because of how he helped me. So I think it’s better for me to work side by side with someone. Even if you don’t have time(QuizMaster did not have a lot of time), it still helpes me to work with someone.

Hi @Spid3rtech,

What I would suggest is that you implement one part at a time, and get that fully working, before you move to the next part. So, try to write some code which makes an object “spawn” somewhere outside of the center of the screen, then make it transition to the center using “transition.to()”. That should be possible with just 2 lines of code.

Brent

Wait really?

Ok, I am just putting the code here JUST in case if you want to check it out.

But can you give me an example on that if you don’t mind? To make them move towards the center with transition. I have never learnt about that yet.

local composer = require( "composer" ) local scene = composer.newScene() local widget = require("widget") local physics = require("physics") physics.start() physics.setDrawMode("normal") system.setIdleTimer( false ) function scene:create( event )&nbsp; local BG = display.newImage ("BG.png")&nbsp; local Gem = display.newImageRect ("Gem.png", 50, 50) Gem.x = display.contentCenterX Gem.y = display.contentCenterY local Spear = display.newImageRect ("Spear.png", 15, 50) Spear.x = 100 Spear.y = display.contentCenterY&nbsp; end&nbsp; -- "scene:show()"&nbsp; function scene:show( event )&nbsp; &nbsp; &nbsp; local sceneGroup = self.view&nbsp; &nbsp; &nbsp; local group = display.newGroup() &nbsp; &nbsp; local phase = event.phase&nbsp; &nbsp; &nbsp; if ( phase == "will" ) then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Called when the scene is still off screen (but is about to come on screen).&nbsp; &nbsp; &nbsp; elseif ( phase == "did" ) then&nbsp; -----------------------&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; local Spears = {}&nbsp; &nbsp; &nbsp; local SpearsTimer&nbsp; &nbsp; &nbsp; local SpearsCounter = 1 &nbsp; &nbsp; local delayTimer &nbsp; &nbsp; local removeListeners end end local field = display.newCircle( 0, 0, 0 ) ; field.alpha = 0.3 field.name = "field" field.x = display.contentCenterX ; field.y = display.contentCenterY physics.addBody( field, "static", { isSensor=true, radius=320 } ) &nbsp; &nbsp; local spawnSpears = function()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local Fall = math.random(display.contentWidth \* -0.2, display.contentWidth \* 1.2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Spears[SpearsCounter] = display.newImageRect( "Spear.png", 15, 50 )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Spears[SpearsCounter].x = Fall&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Spears[SpearsCounter].y = -200 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; physics.addBody( Spears[SpearsCounter], "dynamic", {bounce = 0} )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --Spears[SpearsCounter].collision = onCollision&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --Spears[SpearsCounter]:addEventListener( "collision", Spears[SpearsCounter] )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transition.to( Spears[SpearsCounter], { rotation = Spears[SpearsCounter].rotation+720, time=15000, onComplete=spinImage } )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Spears[SpearsCounter].gravityScale = 0.5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sceneGroup:insert(Spears[SpearsCounter]) &nbsp; &nbsp; &nbsp; &nbsp; group:insert(Spears[SpearsCounter]) &nbsp; &nbsp; &nbsp; &nbsp; SpearsCounter = SpearsCounter + 1 &nbsp; &nbsp; end&nbsp; &nbsp; &nbsp;SpearsTimer = timer.performWithDelay( 5000, spawnSpears, -1 ) -- this is a time and 1000 is 1 second and 1000 miliseconds.. it will spawn one circle every one second.. you can change this at your own liking.&nbsp; -- "scene:hide()"&nbsp; function scene:hide( event )&nbsp; &nbsp; &nbsp; local sceneGroup = self.view&nbsp; &nbsp; &nbsp; local phase = event.phase&nbsp; &nbsp; &nbsp; if ( phase == "will" ) then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- composer.removeScene("Game1")&nbsp; &nbsp; &nbsp; elseif ( phase == "did" ) then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --composer.removeScene("Game1")&nbsp; &nbsp; &nbsp; end&nbsp; end&nbsp; -- "scene:destroy()"&nbsp; function scene:destroy( event )&nbsp; &nbsp; &nbsp; local sceneGroup = self.view&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; composer.removeScene("Game") end&nbsp; -------------------------------------------------------------------&nbsp; -- -------------------------------------------------------------------------------&nbsp; -- Listener setup&nbsp; scene:addEventListener( "create", scene )&nbsp; scene:addEventListener( "show", scene )&nbsp; scene:addEventListener( "hide", scene )&nbsp; scene:addEventListener( "destroy", scene )&nbsp; -- -------------------------------------------------------------------------------&nbsp; return scene