LibGDX Boid Seek Behaviour
Posted
by
childonline
on Game Development
See other posts from Game Development
or by childonline
Published on 2013-10-26T18:58:54Z
Indexed on
2013/10/26
22:17 UTC
Read the original article
Hit count: 536
I'm trying to make a swarm of boids which seek out the mouse position and move towards it, but I'm having a bit of a problem.
The boids just seem to want to go to upper-right corner of the game window. The mouse position seems influence the behavior a bit, but not enough to make the boid turn towards it.
I suspect there is a problem with the way LibGDX handles its coordinate system, but I'm not sure how to fix it
I've uploaded the eclipse project here!
Also here are the relevant bits of my code, in case you see something obviously wrong:
public Agent(){
_texture = GdxGame.TEX_AGENT;
TextureRegion region = new TextureRegion(_texture, 0, 0, 32, 32);
TextureRegion region2 = new TextureRegion(GdxGame.TEX_TARGET, 0, 0, 32, 32);
_sprite = new Sprite(region);
_sprite.setSize(.05f, .05f);
_sprite_target = new Sprite(region2);
_sprite_target.setSize(.1f, .1f);
_max_velocity = 0.05f;
_max_speed = 0.005f;
_velocity = new Vector2(0, 0);
_desired_velocity = new Vector2(0, 0);
_steering = new Vector2(0, 0);
_position = new Vector2(-_sprite.getWidth()/2, -_sprite.getHeight()/2);
_mass = 10f;
}
public void Update(float deltaTime){
_target = new Vector2(Gdx.input.getX(), Gdx.input.getY());
_desired_velocity = ((_target.sub(_position)).nor()).scl(_max_velocity,_max_velocity);
_steering = ((_desired_velocity.sub(_velocity)).limit(_max_speed)).div(_mass);
_velocity = (_velocity.add(_steering)).limit(_max_speed);
_position = _position.add(_velocity);
_sprite.setPosition(_position.x, _position.y);
_sprite_target.setPosition(Gdx.input.getX(), Gdx.input.getY());
}
I've used this tutorial here.
Thanks!
© Game Development or respective owner