applyLinearImpulse() not pushing in a straight line toward velocity

Hi, 

  I have a game I am working on that “Sucks” things into the center of the screen.  I am doing this by using setLinearVelocity().  I have this working as intended.  Once this is done, I want to give the things varying speeds.  To do this, I was trying to use density and applyLinearImpulse().  According to the API, “If the target point is the body’s center of mass, it will tend to push the body in a straight line; if the target is offset from the body’s center of mass, the body will spin about its center of mass”. So, I chose the object.x and object.y but it doesn’t seem to be working as I had inspected.  Some of them continue to the center as hoped, but many of them are slightly off or completely off when using the applyLinearImpulse().  

Thoughts?

local function createPixel() local randPixel = math.random ( 5 ) --used to decide which size/color/speed local randColor = math.random ( 5 ) local newPixel local pixelW local pixelH if ( randPixel == 1) then newPixel = display.newImageRect( mainGroup, "imgs/bluePixel.png", screenW/17.5, screenW/17.5 ) physics.addBody( newPixel, "dynamic", { density=.1 } ) newPixel.isSensor = true elseif (randPixel == 2 ) then newPixel = display.newImageRect( mainGroup, "imgs/redPixel.png", screenW/17.5, screenW/17.5 ) physics.addBody( newPixel, "dynamic", { density=.3 } ) newPixel.isSensor = true elseif (randPixel == 3 ) then newPixel = display.newImageRect( mainGroup, "imgs/greenPixel.png", screenW/17.5, screenW/17.5 ) physics.addBody( newPixel, "dynamic", { density=.7 } ) newPixel.isSensor = true elseif (randPixel == 4 ) then newPixel = display.newImageRect( mainGroup, "imgs/purplePixel.png", screenW/17.5, screenW/17.5 ) physics.addBody( newPixel, "dynamic", { density=1 } ) newPixel.isSensor = true elseif (randPixel == 5 ) then newPixel = display.newImageRect( mainGroup, "imgs/yellowPixel.png", screenW/17.5, screenW/17.5 ) physics.addBody( newPixel, "dynamic", { density=200 } ) newPixel.isSensor = true end table.insert( pixelsTable, newPixel ) newPixel.myName = "pixel" newPixel.isBullet = true local whereFrom = math.random( 4 ) local toX local toY --Set direction to the center of the screen (WORKING) if ( whereFrom == 1 ) then -- From the left newPixel.x = -60 newPixel.y = math.random( screenH ) toX = centerX-newPixel.x toY = centerY-newPixel.y newPixel:setLinearVelocity( toX, toY ) elseif ( whereFrom == 2 ) then -- From the top newPixel.x = math.random( screenW ) newPixel.y = -60 toX = centerX-newPixel.x toY = centerY-newPixel.y newPixel:setLinearVelocity( toX, toY ) elseif ( whereFrom == 3 ) then -- From the right newPixel.x = display.contentWidth + 60 newPixel.y = math.random( screenH ) toX = centerX-newPixel.x toY = centerY-newPixel.y newPixel:setLinearVelocity( toX, toY ) elseif ( whereFrom == 4 ) then -- From the bottom newPixel.x = math.random (screenW) newPixel.y = screenH + 60 toX = centerX-newPixel.x toY = centerY-newPixel.y newPixel:setLinearVelocity( toX, toY ) end --Speed it up (not working, changes direction from center) newPixel:applyLinearImpulse(1, 1, newPixel, newPixel.y) -- speeds them up based on density end
newPixel:applyLinearImpulse(1, 1, newPixel, newPixel.y) 

look closely… you have a syntax error… what about the x position?

needs “.x”:

newPixel:applyLinearImpulse(1, 1, newPixel.x, newPixel.y)

also seems suspicious that you’re giving them all a uniform 1,1 impulse towards lower right instead of toward center.  granted that the magnitude of its effect will vary based on mass (density) but the direction seems “wrong” based on the rest of your thread.

[quote=“roaminggamer,post:2,topic:345956”]

newPixel:applyLinearImpulse(1, 1, newPixel, newPixel.y) 

look closely… you have a syntax error… what about the x position? [/quote]
My bad I erased it all while trying other things and made this error. It was correct when tested (still not working as hoped)

[quote=“davebollinger,post:3,topic:345956”]

needs “.x”:

newPixel:applyLinearImpulse(1, 1, newPixel.x, newPixel.y)

also seems suspicious that you’re giving them all a uniform 1,1 impulse towards lower right instead of toward center. granted that the magnitude of its effect will vary based on mass (density) but the direction seems “wrong” based on the rest of your thread. [/quote]
What do u mean? I thought the 1,1 was the intensity and the x, y was the direction (or if center presses in straight line).

** UPDATED: Fixed math errors below**

This: 

newPixel:applyLinearImpulse(1, 1, newPixel.x, newPixel.y)

is telling the engine to:
apply an instantaneous force of magnitude 1.4142… in the direction <1,1> at the center of object newPixel

Another Example

local mag = math.sqrt(2)/2 newPixel:applyLinearImpulse(mag, -mag, newPixel.x, newPixel.y)

apply an instantaneous force of magnitude 1 in the direction <1,-1> at the center of object newPixel

Yet Another Example

local mag = math.sqrt(2)/2 \* 10 \* newPixel.mass newPixel:applyLinearImpulse(mag, mag, newPixel.x, newPixel.y)

apply an instantaneous force of magnitude (10 x object’s mass) in the direction <1,1> at the center of object newPixel

If you want to push the object in the direction of screen center, you’d have to do vector math.

With SSK2 math2d library that math would be:

local magnitude = 10 -- Whatever you want local vec = ssk.math2d.diff( newPixel.x, newPixel.y, centerX, centerY, true) -- Equivalent to: -- local vec = ssk.math2d.sub( centerX, centerY, newPixel.x, newPixel.y, true) vec = ssk.math2d.normalize(vec) vec = ssk.math2d.scale( vec, magnitude \* newPixel.mass ) newPixel:applyLinearImpulse( vec.x, vec.y, newPixel.x, newPixel.y

[quote=“roaminggamer,post:5,topic:345956”]

** UPDATED: Fixed math errors below**

This:

newPixel:applyLinearImpulse(1, 1, newPixel.x, newPixel.y)

is telling the engine to:
[font=‘courier new’]apply an instantaneous force of magnitude [/font]1.4142…[font=‘courier new’] in the direction <1,1> at the center of object newPixel[/font]

And this:

local mag = math.sqrt(2)/2 newPixel:applyLinearImpulse(mag, -mag, newPixel.x, newPixel.y)

is telling the engine to:
[font=‘courier new’]apply an instantaneous force of magnitude 1 in the direction <1,-1> at the center of object newPixel[/font]
And one more example…

And this:

local mag = math.sqrt(2)/2 \* 10 \* newPixel.mass newPixel:applyLinearImpulse(mag, mag, newPixel.x, newPixel.y)

is telling the engine to:
[font=‘courier new’]apply an instantaneous force of magnitude 10x object mass in the direction <1,1> at the center of object newPixel[/font] [/quote]

OK. Thanks I guess I was miss understanding the force side of the function . I’ll give vectors a try when I get home.

As always your help is much appreciated!

This worked.  Thanks!

newPixel:applyLinearImpulse(1, 1, newPixel, newPixel.y) 

look closely… you have a syntax error… what about the x position?

needs “.x”:

newPixel:applyLinearImpulse(1, 1, newPixel.x, newPixel.y)

also seems suspicious that you’re giving them all a uniform 1,1 impulse towards lower right instead of toward center.  granted that the magnitude of its effect will vary based on mass (density) but the direction seems “wrong” based on the rest of your thread.

[quote=“roaminggamer,post:9,topic:345956”]

newPixel:applyLinearImpulse(1, 1, newPixel, newPixel.y) 

look closely… you have a syntax error… what about the x position? [/quote]
My bad I erased it all while trying other things and made this error. It was correct when tested (still not working as hoped)

[quote=“davebollinger,post:10,topic:345956”]

needs “.x”:

newPixel:applyLinearImpulse(1, 1, newPixel.x, newPixel.y)

also seems suspicious that you’re giving them all a uniform 1,1 impulse towards lower right instead of toward center. granted that the magnitude of its effect will vary based on mass (density) but the direction seems “wrong” based on the rest of your thread. [/quote]
What do u mean? I thought the 1,1 was the intensity and the x, y was the direction (or if center presses in straight line).

** UPDATED: Fixed math errors below**

This: 

newPixel:applyLinearImpulse(1, 1, newPixel.x, newPixel.y)

is telling the engine to:
apply an instantaneous force of magnitude 1.4142… in the direction <1,1> at the center of object newPixel

Another Example

local mag = math.sqrt(2)/2 newPixel:applyLinearImpulse(mag, -mag, newPixel.x, newPixel.y)

apply an instantaneous force of magnitude 1 in the direction <1,-1> at the center of object newPixel

Yet Another Example

local mag = math.sqrt(2)/2 \* 10 \* newPixel.mass newPixel:applyLinearImpulse(mag, mag, newPixel.x, newPixel.y)

apply an instantaneous force of magnitude (10 x object’s mass) in the direction <1,1> at the center of object newPixel

If you want to push the object in the direction of screen center, you’d have to do vector math.

With SSK2 math2d library that math would be:

local magnitude = 10 -- Whatever you want local vec = ssk.math2d.diff( newPixel.x, newPixel.y, centerX, centerY, true) -- Equivalent to: -- local vec = ssk.math2d.sub( centerX, centerY, newPixel.x, newPixel.y, true) vec = ssk.math2d.normalize(vec) vec = ssk.math2d.scale( vec, magnitude \* newPixel.mass ) newPixel:applyLinearImpulse( vec.x, vec.y, newPixel.x, newPixel.y

[quote=“roaminggamer,post:12,topic:345956”]

** UPDATED: Fixed math errors below**

This:

newPixel:applyLinearImpulse(1, 1, newPixel.x, newPixel.y)

is telling the engine to:
[font=‘courier new’]apply an instantaneous force of magnitude [/font]1.4142…[font=‘courier new’] in the direction <1,1> at the center of object newPixel[/font]

And this:

local mag = math.sqrt(2)/2 newPixel:applyLinearImpulse(mag, -mag, newPixel.x, newPixel.y)

is telling the engine to:
[font=‘courier new’]apply an instantaneous force of magnitude 1 in the direction <1,-1> at the center of object newPixel[/font]
And one more example…

And this:

local mag = math.sqrt(2)/2 \* 10 \* newPixel.mass newPixel:applyLinearImpulse(mag, mag, newPixel.x, newPixel.y)

is telling the engine to:
[font=‘courier new’]apply an instantaneous force of magnitude 10x object mass in the direction <1,1> at the center of object newPixel[/font] [/quote]

OK. Thanks I guess I was miss understanding the force side of the function . I’ll give vectors a try when I get home.

As always your help is much appreciated!

This worked.  Thanks!