Power limitation on pool example

is there any way to implement a power limitation using the code from the pool game example? I have written a function to find the distance of the touch from the cue ball, but I can’t think of a way to use it:

local function getDistance(sx,sy,tx,ty) local xDiff = math.sqrt((sx-tx)^2) local yDiff = math.sqrt((sy-ty)^2) return math.sqrt((xDiff^2)+(yDiff^2)) end [import]uid: 116264 topic_id: 30421 reply_id: 330421[/import]

OK. First, head over to The Lab and grab the lib_vector.lua library.

http://www.nightmareswithin.com/thelab/gamesdivision/vector-library

Next, drop in the top of main.lua:

local lib\_vector = require("lib\_vector")  

Then replace:

t:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )  

with:

local maxpower = 150  
local dx = (t.x - event.x)  
local dy = (t.y - event.y)  
local vec = Vector.new(dx,dy)  
local dist = vec:len()  
  
if(dist \> maxpower)then  
 vec:Normalize()  
 vec = vec \* maxpower  
end  
t:applyForce( vec.x, vec.y, t.x, t.y )   
--t:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )  

Seems to be spot on. :slight_smile: [import]uid: 21331 topic_id: 30421 reply_id: 121966[/import]

Thanks that looks good but the link on the website for the vector library doesnt work? [import]uid: 116264 topic_id: 30421 reply_id: 122147[/import]

Make sure you click on the “Download File” icon. :slight_smile: [import]uid: 21331 topic_id: 30421 reply_id: 122153[/import]

OK. First, head over to The Lab and grab the lib_vector.lua library.

http://www.nightmareswithin.com/thelab/gamesdivision/vector-library

Next, drop in the top of main.lua:

local lib\_vector = require("lib\_vector")  

Then replace:

t:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )  

with:

local maxpower = 150  
local dx = (t.x - event.x)  
local dy = (t.y - event.y)  
local vec = Vector.new(dx,dy)  
local dist = vec:len()  
  
if(dist \> maxpower)then  
 vec:Normalize()  
 vec = vec \* maxpower  
end  
t:applyForce( vec.x, vec.y, t.x, t.y )   
--t:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )  

Seems to be spot on. :slight_smile: [import]uid: 21331 topic_id: 30421 reply_id: 121966[/import]

Thanks that looks good but the link on the website for the vector library doesnt work? [import]uid: 116264 topic_id: 30421 reply_id: 122147[/import]

Make sure you click on the “Download File” icon. :slight_smile: [import]uid: 21331 topic_id: 30421 reply_id: 122153[/import]