Help!! linearImpulse and applyForce

Ok… I am not a stupid person, but I am having a difficult time trying to understand the math and logic behind these two functions (may with just my app)

Here is the scenario… (please see pic) I have a large circle with 3 objects spaced out equally spaced around. The large circle can move around the screen. After a perform with delay – I want to call a ball to be fired/move from Obj1, Obj2 or Obj3 to the x0, y0 of the screen no matter where Obj1 is located.
With using ApplyForce or LinearImpulse – I have no idea how to find the collation between Obj1 (2 or 3) to x0,y0…
Yes I can get Obj1.x and Obj1.y and I know that I want to shoot thru x0, y0 – but how does this adapt to the arguments required in ApplyForce or LinearImpulse?
PLEASE HELP!!!
Thanks!
[import]uid: 53789 topic_id: 20820 reply_id: 320820[/import]

Thanks in advance!
[import]uid: 53789 topic_id: 20820 reply_id: 81930[/import]

I read that a few times but am having trouble taking it in - basically you want to shoot an object from one of the three circles (obj1, 2 and 3) to the center of the large circle, yes?

If so what is the large circle called? It’s x and y at any given time would be the center. [import]uid: 52491 topic_id: 20820 reply_id: 82150[/import]

Hi Peach – sorry for the confusion…

To answer the first question: Yes, I want to shoot an object (bullet) from one of the three circles (obj1, 2 OR 3) - randomly picked – but not just to the center BUT keep going.

Shooting to the center could be simulated by using a simply transition from the Obj1.x, Obj1.y to x=0, y=0 – which I have done, but I want the object (bullet) to pass thru x0,y0 and keep going in that direction.

2nd question: Let’s just call the large circle “Base” to keep it simple. The base’s (large circle) center is always at x0,y0
The reason I didn’t give x and y on the three circles (obj1,2 or 3) was because they are not a fixed location.

[import]uid: 53789 topic_id: 20820 reply_id: 82227[/import]

Hey again,

You’d likely look at something like this;

[lua]local base = display.newCircle( 160, 240, 120 )
base.alpha = 0.2

ball1 = display.newCircle( 230, 150, 20 )

function test ()
if ball1.x > base.x and ball1.y < base.y then
transition.to(ball1, {x=ball1.x-base.x*2, y=ball1.y+base.y*2})
elseif ball1.x > base.x and ball1.y > base.y then
transition.to(ball1, {x=ball1.x-base.x*2, y=ball1.y-base.y*2})
elseif ball1.x < base.x and ball1.y < base.y then
transition.to(ball1, {x=ball1.x+base.x*2, y=ball1.y+base.y*2})
elseif ball1.x < base.x and ball1.y > base.y then
transition.to(ball1, {x=ball1.x+base.x*2, y=ball1.y-base.y*2})
end
end
test()[/lua]

That’s off the top of my head and there would be more efficient ways of handling it perhaps but it should work fine I think - test it and let me know :slight_smile:

Peach [import]uid: 52491 topic_id: 20820 reply_id: 82414[/import]

Thanks Peach - I will look at this tonight - but is there a way that you know how to do this with and impulse or apply force? [import]uid: 53789 topic_id: 20820 reply_id: 82522[/import]

You would be able to do it but the math is escaping me at the second. You’d need to work out the balls position compared to the center of the base and do some calcs from there. [import]uid: 52491 topic_id: 20820 reply_id: 82645[/import]

Hey
I think I understand what you are trying to do… so maybe this would do the trick:

… use the localToContent() function to get the screen coordinates of the Base relative to screen 0,0… something like:

 baseX, baseY = base:localToContent(0,0) 

… then get the objects coordinates relative to the Bases

 objX, objY = obj:localToContent(baseX,baseY) 

… now you have an x and y value for your forceX and forceY…

… apply them to the bullet!

Would this do the trick?

Richie [import]uid: 12583 topic_id: 20820 reply_id: 82671[/import]

… AN AFTER THOUGHT!

… you may have to do some basic stuff to make sure the bullet fires toward the Base center and not away from the obj in the opposite direction.

ie.
Base at 160,240
Obj at 200, 200

when the objX and objY is calculated… you will need to make them negative forcex and forcey…

This should be easy enough by checking obj X and Y coord > bases X and Y coords

Hope this helps [import]uid: 12583 topic_id: 20820 reply_id: 82672[/import]