Is is possible to set xGravity and yGravity independently?

Hello,

I am trying to have an object be controlled horizontally via the accelerometer, however I want to control the vertical motion via another method that doesn’t use the accelerometer.

My issue is that if I am using an event to move the object based on tilt, I have to keep feeding a value for the y portion as well when I use physics.setGravity(x, y)

Is there a way I can ONLY send the X value through that function, and ignore the y parameter?

thanks!

[import]uid: 41918 topic_id: 8679 reply_id: 308679[/import]

You need to pass both values to setGravity(), but can’t you just always pass 0 (zero) for the Y component?

Tim [import]uid: 8196 topic_id: 8679 reply_id: 31162[/import]

The problem in passing 0 is that whenever I tilt the device it sets the y component of gravity to something I don’t want. I want to control the y component with a button press, and completely independent of the tilt event; basically whenever the button is pressed, the y gravity is reversed, and the object begins to accelerate upwards.

I am pretty new with all of this, so chances are I am approaching this in a very odd way :wink:

[import]uid: 41918 topic_id: 8679 reply_id: 31163[/import]

Why not just store your value you wan they Y to be in a variable, and just pass that to setGravity? [import]uid: 34551 topic_id: 8679 reply_id: 31711[/import]

You don’t set the value to zero. Do something like the following:

[lua]physics = require(“physics”)

physics.start()
physics.setGravity(0,9.8)

gx,gy = physics.getGravity()

print("gx: “…gx…” gy: "…gy)

newYGravity = 4
– as you can see, i set the yGravity to 4, but retain what the xGravity was
physics.setGravity(gx, newYGravity)

gx,gy = physics.getGravity()

print("gx: “…gx…” gy: "…gy)[/lua]

When you run that, you will see this in your terminal (assuming you are utilizing the Simulator Terminal:

gx: 0 gy: 9.8000001907349 gx: 0 gy: 4 [import]uid: 34551 topic_id: 8679 reply_id: 31786[/import]

Thanks gamehkmail and Stevedbarlow for the replies.

stevedbarlow: That’s actually what I am trying right now, but it’s behaving sluggish and overall very awkward, my guess because I have two different listeners calling different methods to affect one of the directions independently of the othe, so they don’t seem to be playing nice.

I am gonna go a bit more the route that gamehkmail suggested. I am going to keep the ygravity component the way i have it and work my own movement logic for the horizontal movement, as for what I want to do I have a feeling gravity won’t be the right direction based on what I am seeing.

thanks again

[import]uid: 41918 topic_id: 8679 reply_id: 32025[/import]

I am not sure about gravity, but I know it’s not possible to set x or y component independently of a velocity. I mean, even if one tries to get the x,y velocity first and then put back either x or y unchanged and change the other, that “unchanged” component will be changed as well.

What I am doing now is to implement my own velocity and acceleration logic by capturing enterFrame event. With this, pause and resume can then be easily implemented as well. Hope it helps.

– Advanced UI + Graph for Corona® | Website | Forum (https) | See: [import]uid: 11385 topic_id: 8679 reply_id: 31772[/import]