Asteroids style wrapping with a dynamic character

Hi,

Just wondering if someone could point me in the right direction for implementing screen wrapping for a character in Corona. I could do this immediately, if I was controlling my own character movement, but since I am using the physics systems - I am sure I have to approach this properly.

I am currently using body:applyLinearImpulse for the vertical movement (i.e. one “pull” and it applies the impulse in the Y direction).

And I am also using body:applyForce to provide movement in the horizontal (x) plane - via the accelerometer. Everything works perfectly, but I now I need to implement screen wrapping.

I learnt last time that messing with physics while it’s in certain states is a no-no. Any ideas?

Cheers! [import]uid: 41286 topic_id: 17496 reply_id: 317496[/import]

Have you tried something like this: You can obviously change the numbers around if you want it to spawn on opposite side the sample below will keep your object on screen as if you had invisible walls. (good for tilt game since tilt vs “static” objects gives a lot of problems)
[lua]-- Keep Object on Screen
local function imageWrap (event)

if object.x < 20 then
object.x = 20
end

if object.x > 300 then
object.x = 300
end

if object.y < 0 then
object.y = 0
end

if object.y > 480 then
object.y = 480

end
end

Runtime:addEventListener(“enterFrame”, imageWrap) [import]uid: 30314 topic_id: 17496 reply_id: 66423[/import]

No, that would work - but as far as I can tell, because I am using a dynamic character, changing the characters positions directly would cause the physics to flip out (and it’s a valid point).

I presume I need to grab the current linear velocity and then move the object then reapply the forces, but I’m not quite sure how to do it. [import]uid: 41286 topic_id: 17496 reply_id: 66425[/import]

Have you tried it? I ran a sample with a dynamic character where the character is falling with gravity(0, 4.0) and when it reaches bottom of screen it appears back on top it worked fine. Not sure if same results if you using apply force or linear impulse if not I might have a work around i did in the past for one of my apps to simulate a [Warp] [import]uid: 30314 topic_id: 17496 reply_id: 66426[/import]

Yeah, no I tried it - it works okay (so far). I’m asking because i’ve seen a post that recommends you DO NOT teleport a dynamic object while it is under impulse / moving dynamically. So i’m after the Corona recommended alternative. Now - that poster (can’t find the post right now) may be incorrect - but it does jibe with my experience with physics simulations I’ve used in the past.

EDIT: Here is one of the lines from the docs themselves that suggests it may not be the appropriate way to do it (although, it doesn’t explicitly say you can’t).

Therefore, standard object read/write attributes like x, y and rotation should continue to work normally on a physical body, although if the bodyType is “dynamic”, the physics engine may “fight back” against your attempts to move the object manually, since the target object will be under the constant effect of gravity and other forces.

  • Please note - the character that I am warping, is “dynamic”.

EDIT x 2 - one line further down may be the answer…

The “DragPlatforms” sample code shows one method for changing body types to “kinematic” during dragging, which temporarily removes them from the influence of external forces. [import]uid: 41286 topic_id: 17496 reply_id: 66430[/import]