Hi guys, I write here because right now I lack the knowledge for executing a good idea that uses box2d (sorry, don't want to reveal it right now) and I need some help moving forward. It has been months since I haven't touched the code, maybe it is due for attempting to create at first a relatively ambitious project without having too much experience. I'm still a newbie and seeing all that it's missing for completing the project makes me doubt if I could ever finish it.
Anyways, I managed to code the prototype, which I think it was the biggest challenge, and it is playable. Now I'm stuck in trying to create a box2d object that moves both in random and specific patterns, and that can target and follow another moving object. Sadly, love.physics isn't very suited to newbies, and I can't find box2d tutorials that I can apply easily to my love.physics problems. The code right now is too messy to be shown (it hurts to look it lol), and I want to be able to do just that simple thing, that could be considered as artificial intelligence movement in box2d, does anybody knows how?
AI in love.physics help
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: AI in love.physics help
If you want a function to move a body to a point, here's an example. This one is a simple version that just applies force and damping. It should be possible to make a better one that stops right at the endpoint rather than approaching it asymptotically, like this one does, but that would be quite more complicated. I wrote a bit about that (in Spanish) here: http://orden-y-concierto.blogspot.hu/20 ... iales.html
You'll have to adjust the dampFactor to your forceFactor if you want critical damping (critical damping is where the object approaches the destination the fastest possible without overshooting). [Edit: the program calculates it for you now]. The idea is that you tell it to move to a certain point (how you decide the points to go to is up to you). The point to go to is defined in targetX, targetY.
Yeah, the movement isn't great, but at least I hope it helps a bit. Other than that, maybe you can use [wiki]Body:setLinearVelocity[/wiki] to make them go at constant velocities from one point to another.
Code: Select all
local forceFactor = 20 -- Force multiplier
local world = love.physics.newWorld(0, 0) -- assumed no gravity
local thingy = {
x = 50;
y = 50;
targetX = 50;
targetY = 50;
}
thingy.body = love.physics.newBody(world, thingy.x, thingy.y, "dynamic")
thingy.shape = love.physics.newCircleShape(20)
thingy.fixture = love.physics.newFixture(thingy.body, thingy.shape)
local dampFactor = 2*math.sqrt(forceFactor*thingy.body:getMass())
local function updateThingy()
-- Apply a force in the direction of the target proportional to the distance,
-- with drag.
thingy.x, thingy.y = thingy.body:getPosition()
local vx, vy = thingy.body:getLinearVelocity()
local Fx = (thingy.targetX - thingy.x) * forceFactor - vx * dampFactor
local Fy = (thingy.targetY - thingy.y) * forceFactor - vy * dampFactor
thingy.body:applyForce(Fx, Fy)
end
function love.update(dt)
if love.mouse.isDown(1) then
thingy.targetX, thingy.targetY = love.mouse.getPosition()
end
updateThingy()
world:update(dt)
end
function love.draw()
local x, y = thingy.body:getPosition()
love.graphics.circle("fill", x, y, thingy.shape:getRadius())
end
function love.keypressed(k)
if k == "escape" then love.event.quit() end
end
Re: AI in love.physics help
Not exactly what you're looking for, but maybe it'll help... I pulled it from something else I was working on and trimmed it down for you: (not using love.physics)
I'm sure you could figure out how to make it work with box2d, I'm just being lazy right now.
Code: Select all
if obj.isTracking then -- "tracking" coords... if you change target x/y it will follow
obj.x = obj.x - (obj.x - target.x) * dt * obj.speed
obj.y = obj.y - (obj.y - target.y) * dt * obj.speed
-- Remove this for continuous tracking
if math.abs(obj.x - target.x) < .3 and math.abs(obj.y - target.y) < .3 then
obj.x, obj.y = target.x, target.y
end
end
- scissors61
- Citizen
- Posts: 76
- Joined: Fri Jan 08, 2016 10:16 am
Re: AI in love.physics help
Thanks a lot, pgimeno. This is what I was trying to achieve.
I couldn't understand it, but with what you did here I guess you solved the problem you wrote about in the post.It should be possible to make a better one that stops right at the endpoint rather than approaching it asymptotically, like this one does, but that would be quite more complicated. I wrote a bit about that (in Spanish) here: http://orden-y-concierto.blogspot.hu/20 ... iales.html
Yes, that's what I'm going to do. In the end, implementing your code with some random parameters of "linearVelocity" will look like a fish tank with fishes that eat baits, the goal I had in mind.Yeah, the movement isn't great, but at least I hope it helps a bit. Other than that, maybe you can use Body:setLinearVelocity to make them go at constant velocities from one point to another.
Thanks Beelz, I'll try to understand it to see if I can implement it in areas with no physics, thought first of doing it without physics but couldn't.I'm sure you could figure out how to make it work with box2d, I'm just being lazy right now.
Re: AI in love.physics help
Glad it helped!scissors61 wrote:Thanks a lot, pgimeno. This is what I was trying to achieve.
Unfortunately, no. The code posted above takes infinite time to reach the destination; the blog post is about reaching the destination in minimum time when the acceleration is limited.scissors61 wrote:I couldn't understand it, but with what you did here I guess you solved the problem you wrote about in the post.
Who is online
Users browsing this forum: No registered users and 90 guests