Limiting the object location

Hi. I have made a program that sets a linear impulese on the circle shape’s X Axis when the user tilt the phone using the accelerometer event. To prevent the circle from going very far from the screen’s content area, I put one rectangle object on the left and one rectangle object on the right, both are beyond the content width of the stage and has a static physical body.

But, I feel quite uncomfortable for the program and made me think that it is not the proper way to do it. Is there any other way to limit the object location that it is much proper than what I just did?

Hi.
I’m not sure to have understood

but display.newContainer can define a clipped area !

Hi. I made a circle in the center of the stage and add it to the physics whith a “dynamic” body. To prevent this in falling so deep, I have made a rectangle ground on the bottom and also add it to physics but whith a “static” body.

I want to prevent the circle from falling so deep even without a rectangle ground, like limiting the circle’s location. etc.

As this is the same topic, but slightly rephrased, please keep them together.

If you want to restrict the movement of your dynamic physics bodies, you need to do so using other physics objects. Alternatively, you could have an enterFrame listener to check for object(s) location(s) and then do something for them, but this isn’t likely a good approach.

If you just want to create boundaries, then you can create static physics bodies, but just make them invisible.

Can you limit the location programmatically using xy-coordinates?

Did you mean object:localToContent( x, y )? Well, I’ve tried but it did nothing to stop the physical object from falling so deep.

Coordinates + thrust/momentum or what ever is there… I am sure you can play with those parameters and get what you want in a beautiful technical manner.

Pardon me. I dont think that I quite understand the coordinates + thrust/momentum. But can you show me an example how exactly to do the whole thing you were trying to point out? Thank you very much.

May be share part of your project where you play with that rolling object, we will try to help.

Sorry for what took me a while.

Here is the piece of code that I would like to improve:

local physics = require(“physics”)
physics.start();
physics.setGravity(0,0);
physics.setDrawMode(“debug”);

local barierL

local barierR

local leaner

local leanHere = display.newGroup()

local function Move ( event )
leaner:setLinearVelocity(event.xRaw * 100000, 0)
end

barierL = display.newRect(leanHere, display.screenOriginX-75, display.contentHeight - 150, 40, 1000)
barierL.alpha = 0.0
physics.addBody(barierL, “static”, {bounce = 0, filter = { categoryBits = 2, maskBits = 4 }})
barierL.myName = “barierL”

barierR = display.newRect(leanHere, display.contentWidth-(display.screenOriginX)+75, display.contentHeight - 150, 40, 1000)
barierR.alpha = 0.0
physics.addBody(barierR, “static”, {bounce = 0, filter = { categoryBits = 2, maskBits = 4 }})
barierR.myName = “barierR”

leaner = display.newRect(leanHere, 384, 874, 200, 340)
leaner.alpha = 0.0
physics.addBody(leaner, {density = 0.5, bounce = 0, filter = {categoryBits = 4, maskBits = 2}})
leaner.isSleepingAllowed = false
leaner.myName = “leaner”

Runtime:addEventListener(“accelerometer”, Move)

The barierL and the barierR are stopping the leaner from going beyond the stage as it goes through the accelerometer function. My question through this entire discussion is how can I Limit the leaner’s location without the barierL and barierR? Thank you very much.

The most “natural” way to limit movement of a dynamic body is to use other bodies like you’re already doing. The only other approach really would be to have an enterFrame listener function which gets called every frame and there you check the x,y of the body and add conditions to prevent from exceeding certain bounds

If you want it reset to a location if it exceeds bounds, you can simply do that or if you want it to act like there’s an invisible barrier, you could consider maintaining a table where you store its previous x,y coordinates and keep forcing it to stay at those coordinates every time it exceeds the bounds that you want to impose

If you don’t trust the physics to keep it within bounds, use an enterFrame listener (or just a repeating timer.performWithDelay) that calls a function that checks the objects currrent x/y, and if it’s outside a range then reset it.

Just make sure you remove the enterFrame listener or repeating timer when you’re done.

Thanks to all of you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.