Create Fixed position Object (like stone)

I have a very simple question!

i have a stone and a ball.when i move ball with touch and collide with stone, stone should not move and it should be fixed in its place but ball should not go into stone! they should act like collision of stone and ball in real situation

i tested static bodytype for stone and dynamic for ball but ball goes into the stone.
i also used high friction and density for stone but no luck!

Thanks

Static stone and dynamic ball is right, so the way you set up your bodies has an issue.

Did you enable hybrid debug drawing to see what is happening?

https://docs.coronalabs.com/api/library/physics/setDrawMode.html

Friction, density, etc have nothing to do with penetration.

Is your ball moving very fast?  If so, set the isBullet attribute on the ball after adding the body:

https://docs.coronalabs.com/api/type/Body/isBullet.html

Hybrid is enable.they go into eachother!

Hi @arash45654,

We will probably need to see your code. Are you applying a “scale” to any of these objects?

I’m not seeing any issues with physics.

https://www.youtube.com/watch?v=u3XUfcRjazk&feature=youtu.be

Try this example:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2017/06/stoneBall.zip

local physics = require "physics" physics.start() ---physics.setDrawMode( "hybrid" ) local stone = display.newImageRect( "images/kenney\_stone.png", 100, 100 ) stone.x = 400 stone.y = 800 physics.addBody( stone, "static" ) local ball = display.newImageRect( "images/kenney1.png", 100, 100 ) ball.x = 400 ball.y = 600 physics.addBody( ball, "dynamic", { radius = 50, bounce = 0.9 } )  

i tested above sample,it does not have any problem,the problem happens when i use transition.to() to move ball when gravity=0

here is the screenshot:

https://drive.google.com/file/d/0BznIqOoMie9pckVHM004b2xmYTQ/view?usp=sharing

here is the code:

local physics = require "physics" physics.start() physics.setGravity( 0, 0 ) physics.setDrawMode( "hybrid" ) local img1=display.newImage( "sun.png" ) img1.x=display.contentCenterX img1.y=display.contentCenterY img1.width=50 img1.height=50 img1:scale( 2, 2 ) local img2=display.newImage( "moon.png" ) img2.x=display.contentCenterX-20 img2.y=0--display.contentCenterY img2.width=50 img2.height=50 img2:scale( 2, 2 ) physics.addBody(img1,"static") physics.addBody(img2,"dynamic") img2.isFixedRotation=true local function go( event ) -- body if(event.phase=="began")then -- img2.x=event.x -- img2.y=event.y transition.to( img2, {time=10000,y=display.contentHeight } ) end end img2:addEventListener( "touch", go )

You can’t use transition with physics like that.

You are effectively manually moving the object and the physics engine (Box2D) has no idea that the body has moved.

You need to use velocities and forces to move physics bodies.

At the very least you need to disable sleeping, but even then transitions will try to overlap objects and mess things up.

Again, the two features transition.* and physics.* are ignorant of each other.

Also, I just looked at your screenshot and the bodies are square and too small.

The size issue is a side-effect of scaling + newImage() + physics bodies.  You’d be better off doing this:

( MAY CONTAIN TYPOS )

local physics = require "physics" physics.start() physics.setGravity( 0, 0 ) physics.setDrawMode( "hybrid" ) local img1=display.newImageRect( "sun.png", 100, 100 ) img1.x = display.contentCenterX img1.y = display.contentCenterY physics.addBody(img1,"static", { radius = 50 } ) local img2=display.newImageRect( "moon.png", 100, 100 ) img2.x = display.contentCenterX - 20 img2.y = 0 physics.addBody(img2,"dynamic", { radius = 50 } ) img2.isFixedRotation = true img2.touch = function( self, event ) if( event.phase == "began" ) then local dy = display.contentHeight - self.y self:setLinearVelocity( 0, dy/10 ) self:removeEventListener("touch") end end img2:addEventListener( "touch" )

is there any function to setTarget position with physics?
I just saw joint touch that has setTarget function ,but i can not use it in my situation.

No, there is not.

Hi @arash45654,

You can move physical objects using transitions, but you need to be careful with how you handle it, otherwise you’ll encounter complications of the two “systems” not know what the other one is doing (which is sort of what @roaminggamer is eluding to).

See here for some details on this:

https://docs.coronalabs.com/guide/physics/limitations/index.html#transitions

Brent

There are a couple of things going on here.

First and foremost, when you scale an object, physics doesn’t scale with it. In this case, your display objects are twice the size of the physics body. Secondly you are letting the bodies default to a square.  You probably should consider setting the bodies to be a circle at which point you can specify the radius to be the size you really want it to be.  You’re setting the width/height to 50 then doubling the size to 100. Why not just set it to 100 to begin with? Regardless you can do:

physics.addBody( img1, { radius=50 } )

to make it a 100px wide circle. Then you are using a non-physics way of moving the bodies. transition.to doesn’t know about physics, so it just moves the objects. If you have a collision listener setup, it should generate a collision, but you’re not allowing physics to control the body movement so they will overlap.

Rob

Static stone and dynamic ball is right, so the way you set up your bodies has an issue.

Did you enable hybrid debug drawing to see what is happening?

https://docs.coronalabs.com/api/library/physics/setDrawMode.html

Friction, density, etc have nothing to do with penetration.

Is your ball moving very fast?  If so, set the isBullet attribute on the ball after adding the body:

https://docs.coronalabs.com/api/type/Body/isBullet.html

Hybrid is enable.they go into eachother!

Hi @arash45654,

We will probably need to see your code. Are you applying a “scale” to any of these objects?

I’m not seeing any issues with physics.

https://www.youtube.com/watch?v=u3XUfcRjazk&feature=youtu.be

Try this example:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2017/06/stoneBall.zip

local physics = require "physics" physics.start() ---physics.setDrawMode( "hybrid" ) local stone = display.newImageRect( "images/kenney\_stone.png", 100, 100 ) stone.x = 400 stone.y = 800 physics.addBody( stone, "static" ) local ball = display.newImageRect( "images/kenney1.png", 100, 100 ) ball.x = 400 ball.y = 600 physics.addBody( ball, "dynamic", { radius = 50, bounce = 0.9 } )  

i tested above sample,it does not have any problem,the problem happens when i use transition.to() to move ball when gravity=0

here is the screenshot:

https://drive.google.com/file/d/0BznIqOoMie9pckVHM004b2xmYTQ/view?usp=sharing

here is the code:

local physics = require "physics" physics.start() physics.setGravity( 0, 0 ) physics.setDrawMode( "hybrid" ) local img1=display.newImage( "sun.png" ) img1.x=display.contentCenterX img1.y=display.contentCenterY img1.width=50 img1.height=50 img1:scale( 2, 2 ) local img2=display.newImage( "moon.png" ) img2.x=display.contentCenterX-20 img2.y=0--display.contentCenterY img2.width=50 img2.height=50 img2:scale( 2, 2 ) physics.addBody(img1,"static") physics.addBody(img2,"dynamic") img2.isFixedRotation=true local function go( event ) -- body if(event.phase=="began")then -- img2.x=event.x -- img2.y=event.y transition.to( img2, {time=10000,y=display.contentHeight } ) end end img2:addEventListener( "touch", go )

You can’t use transition with physics like that.

You are effectively manually moving the object and the physics engine (Box2D) has no idea that the body has moved.

You need to use velocities and forces to move physics bodies.

At the very least you need to disable sleeping, but even then transitions will try to overlap objects and mess things up.

Again, the two features transition.* and physics.* are ignorant of each other.