Applying linear impulse toward a screen reference point

Hi everyone, hope you’re well.

Real world: You putt your golf ball towards the hole, it’s too fast and not dead-centre. The ball spins out of the hole, you’ve missed, doh!

I’ve been trying to simulate this in corona… First I thought of using a kind of magnetism simulation, which proved to be difficult as Corona hasn’t included BOX 2D’s radial gravity feature.

Particle candy has a nice attractor field simulation in it’s effects library, but only works on particles… I didn’t fancy having my main-sprite as a physics particle, so I’m left with one option that I can’t quite get my head around.

I intend on creating a sensor over the ‘hole’ that applies a linear impulse toward the center of this hole when the ball collides. This would work great for me as the impulse factor will have a stronger or weaker effect on the ball, based on it’s velocity…

I’m not sure if I’m on the right track with this I’m not even sure how to implement it, so if any of you can help or think of a better approach, I’d be truly grateful.

Mark [import]uid: 176868 topic_id: 35435 reply_id: 335435[/import]

Hi @markr1,
I tackled a somewhat similar scenario to this once. I put a sensor circle around the target, somewhat bigger than the visual size, and when the moving object collided with that, I snapped a stretchy touch joint between the object and the target’s center. Then, I started a listener to see how close the object’s center was to the target center, as the touch joint pulled it toward that point.

My goal was to have the moving object eventually “pull” to the center, but in your case, it seems like you’ll want to release the moving object (ball) if it’s going too fast, making it look like its rolling around the outside of the hole but then it escapes and rolls off somewhere else. I’m not 100% sure how it would work, but maybe you could detect that if the ball doesn’t reach the sufficient center in a very short amount of time (like 1/2 second), then you release the joint. Proper timing, and the amount of “pull” on the joint, will need to be tested thoroughly, but I think it’s achievable.

Might this solve the issue for you?
Brent [import]uid: 200026 topic_id: 35435 reply_id: 140780[/import]

Excellent, you understand my problem exactly… I did think about applying a pivot joint (maybe with easing to give that feeling of elasticity), but you’ve hit the nail on the head, releasing the object again is real pain in the ass.

I was hoping that I could simply apply some kind of inertial force to the ‘ball’ using a sensor (like yours, bigger than the visual necessity), but apply that force in the direction of the center of the hole… I’ve been scouring the net to see if I can apply linearImpulse (with fixed force) towards a specific X/Y coordinate on the screen (the middle of the hole), but I’m currently stuck. I know it’s not exactly in line with your solution, but I( would like to play with it anyway. Do you think you could give me a pointer on applying force to a body, towards a fixed location?

many thanks

[import]uid: 176868 topic_id: 35435 reply_id: 140782[/import]

OK, another idea I just had… when the ball enters the radial zone (slightly bigger than the hole), you start a Runtime listener or a quickly-repeating timer (50-100 milliseconds). As the listener/timer runs, you calculate the distance between the hole’s center and the ball’s center, and apply that inverse impulse to the ball’s center. This should behave as if an invisible force is “nudging” the ball toward the center for each iteration. Of course, you still want to check when the ball is sufficiently in the middle of the target so you can consider it a successful putt… and conversely, you want to shut all that process down and stop the listener/timer if the ball escapes outside the radial zone on the “ended” phase. [import]uid: 200026 topic_id: 35435 reply_id: 140785[/import]

I think you might want to take a look at the samples I’ve posted here: http://springboardpillow.blogspot.co.uk/2012/04/sample-code.html?m=1

The draggable fan demo will be of interest, but I think the best idea is the pivot joint. You can have an enter frame (or other, fast timer) to check the force being appled to the joint and remove it when it passes a threshold (high or low.)

I believe the game Blendamaze has the same effect that you’re looking for, so it would be great to here from them :slight_smile:

My draggable fan demo basically calculates the inverse distance from the centre of the hole and uses applyLinearForce to push the ball towards the centre. I’m just not sure that it would give the right aesthetic effect here. [import]uid: 8271 topic_id: 35435 reply_id: 140788[/import]

Thanks guys. The quality of this forum never fails to amaze me… One thing I’ve noticed, when using my ‘successful putt’ sensor (tiny radius in the centre of the hole((which I use to make the ball ‘skip’ or ‘sink’ depending on it’s velocity)), If I use getlinearVelocity, I get two vX/vY readings in the terminal, one showing velocity when sensor is entered and one showing velocity, when sensor is exited. This aligns with your suggestion of destroying a listener when phase == ended… Thanks, I hadn’t thought of that.

@Horace; Thanks. Blendmaze is great, I’ve been trying to figure out how they got the ball to draw a line, but that’s another thread entirely. I will have a good play with your sample code and if I get a result, will post on here.

Mark [import]uid: 176868 topic_id: 35435 reply_id: 140790[/import]

Hi @markr1,
I tackled a somewhat similar scenario to this once. I put a sensor circle around the target, somewhat bigger than the visual size, and when the moving object collided with that, I snapped a stretchy touch joint between the object and the target’s center. Then, I started a listener to see how close the object’s center was to the target center, as the touch joint pulled it toward that point.

My goal was to have the moving object eventually “pull” to the center, but in your case, it seems like you’ll want to release the moving object (ball) if it’s going too fast, making it look like its rolling around the outside of the hole but then it escapes and rolls off somewhere else. I’m not 100% sure how it would work, but maybe you could detect that if the ball doesn’t reach the sufficient center in a very short amount of time (like 1/2 second), then you release the joint. Proper timing, and the amount of “pull” on the joint, will need to be tested thoroughly, but I think it’s achievable.

Might this solve the issue for you?
Brent [import]uid: 200026 topic_id: 35435 reply_id: 140780[/import]

Excellent, you understand my problem exactly… I did think about applying a pivot joint (maybe with easing to give that feeling of elasticity), but you’ve hit the nail on the head, releasing the object again is real pain in the ass.

I was hoping that I could simply apply some kind of inertial force to the ‘ball’ using a sensor (like yours, bigger than the visual necessity), but apply that force in the direction of the center of the hole… I’ve been scouring the net to see if I can apply linearImpulse (with fixed force) towards a specific X/Y coordinate on the screen (the middle of the hole), but I’m currently stuck. I know it’s not exactly in line with your solution, but I( would like to play with it anyway. Do you think you could give me a pointer on applying force to a body, towards a fixed location?

many thanks

[import]uid: 176868 topic_id: 35435 reply_id: 140782[/import]

OK, another idea I just had… when the ball enters the radial zone (slightly bigger than the hole), you start a Runtime listener or a quickly-repeating timer (50-100 milliseconds). As the listener/timer runs, you calculate the distance between the hole’s center and the ball’s center, and apply that inverse impulse to the ball’s center. This should behave as if an invisible force is “nudging” the ball toward the center for each iteration. Of course, you still want to check when the ball is sufficiently in the middle of the target so you can consider it a successful putt… and conversely, you want to shut all that process down and stop the listener/timer if the ball escapes outside the radial zone on the “ended” phase. [import]uid: 200026 topic_id: 35435 reply_id: 140785[/import]

I think you might want to take a look at the samples I’ve posted here: http://springboardpillow.blogspot.co.uk/2012/04/sample-code.html?m=1

The draggable fan demo will be of interest, but I think the best idea is the pivot joint. You can have an enter frame (or other, fast timer) to check the force being appled to the joint and remove it when it passes a threshold (high or low.)

I believe the game Blendamaze has the same effect that you’re looking for, so it would be great to here from them :slight_smile:

My draggable fan demo basically calculates the inverse distance from the centre of the hole and uses applyLinearForce to push the ball towards the centre. I’m just not sure that it would give the right aesthetic effect here. [import]uid: 8271 topic_id: 35435 reply_id: 140788[/import]

Thanks guys. The quality of this forum never fails to amaze me… One thing I’ve noticed, when using my ‘successful putt’ sensor (tiny radius in the centre of the hole((which I use to make the ball ‘skip’ or ‘sink’ depending on it’s velocity)), If I use getlinearVelocity, I get two vX/vY readings in the terminal, one showing velocity when sensor is entered and one showing velocity, when sensor is exited. This aligns with your suggestion of destroying a listener when phase == ended… Thanks, I hadn’t thought of that.

@Horace; Thanks. Blendmaze is great, I’ve been trying to figure out how they got the ball to draw a line, but that’s another thread entirely. I will have a good play with your sample code and if I get a result, will post on here.

Mark [import]uid: 176868 topic_id: 35435 reply_id: 140790[/import]