Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to permanently set body's X position?
Started by jfpilch Feb 09 2019 10:33 AM

3 replies to this topic
physics

Best Answer davebollinger , 11 February 2019 - 07:10 AM

use a piston joint (axis 0,1; no motor)

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

jfpilch

[GLOBAL: userInfoPane.html]
jfpilch
  • Observer

  • 1 posts
  • Corona SDK

I am new to Corona, and struggling to figure out the following: I have a dynamic body that I would like to be able to move along the Y axis, but I would like to fix its X position, effectively preventing it from moving along the X axis.

 

Is there a smart way for me to achieve this? (I assumed resetting X frequently sounds like a terrible idea...)

Thanks!



[TOPIC: post.html]
#2

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 3,069 posts
  • Corona SDK

If you try to set the .x, .y or .rotation values of physics objects you will be fighting the physics engine. This is a bad idea because it will win. That will take the form of perhaps removing objects from the visible scene but without their variables disappearing. Any way it happens, it's bad.

 

The only physics object you can directly affect without definitely negatively affecting the objects are static objects, because the engine essentially doesn't care.

 

If you want to fix a dynamic object in place you have a number of options, but my preference is to create an offscreen, static sensor. Any dynamic object is then fixed to that static object with a weld joint. This works surprisingly well and has the unexpected benefit of making effects like two objects bouncing off each other more realistic. This is because you have two dynamic bodies rebounding from each other than one dynamic rebounding from a static object - which is the equivalent of hitting an immovable object (the bounce tends to be exaggerated.)

 

So, create an 'anchor':

local anchor = display.newGroup()
anchor.x, anchor.y = -500, -500
physics.newBody( anchor, "static", { radius=10, sensor=true } )

Then, if you have a platform you want to keep in place:

local platform = display.newImage("platform.png")
physics.addBody( platform, "dynamic" )

Weld the platform to the anchor:

local platformweld = physics.newJoint( "weld", anchor, platform, platform.x, platform.y )

If you want to move the platform, move the anchor. Of course, if you've welded other objects to the anchor they will move, too. So create the anchors you need.

 

Tip: If you're going to do that a lot you might want to put the anchor at the same location as the platform, or your "hybrid" mode will get real freaky.



[TOPIC: post.html]
#3

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,345 posts
  • Enterprise

  Best Answer

use a piston joint (axis 0,1; no motor)



[TOPIC: post.html]
#4

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 3,069 posts
  • Corona SDK

I've built some demos for you to take a look at:

 

https://github.com/HoraceBury/HelpDemos




[topic_controls]
[/topic_controls]