List of usage examples for com.badlogic.gdx.math Vector2 nor
@Override
public Vector2 nor()
From source file:io.piotrjastrzebski.sfg.game.objects.Pickup.java
License:Open Source License
public void updateAngle() { final Vector2 v = body.getLinearVelocity(); float angle = v.nor().angle() - 90; pickupSkeleton.getRootBone().setRotation(angle); }
From source file:jordanlw.gdxGame.Game.java
License:Open Source License
private void handleInput(Vector2 clickRelativePlayer, Vector2 mousePressedPosition, Vector2 distanceToMouse, Vector2 bulletVector) {/*from ww w .j ava2 s . c om*/ Player player = getLocalPlayer(); Integer movementSpeed = 250; Vector2 deltaPosition = new Vector2(0, 0); if (Gdx.input.isKeyPressed(Input.Keys.W)) { deltaPosition.y += movementSpeed; movementThisFrame = true; } if (Gdx.input.isKeyPressed(Input.Keys.S)) { deltaPosition.y -= movementSpeed; movementThisFrame = true; } if (Gdx.input.isKeyPressed(Input.Keys.A)) { deltaPosition.x -= movementSpeed; movementThisFrame = true; } if (Gdx.input.isKeyPressed(Input.Keys.D)) { deltaPosition.x += movementSpeed; movementThisFrame = true; } deltaPosition = deltaPosition.nor().scl(movementSpeed * Gdx.graphics.getDeltaTime()); player.position.setPosition(player.position.getPosition(new Vector2()).add(deltaPosition)); if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)) mousePressedPosition.set(Gdx.input.getX(), camera.viewportHeight - Gdx.input.getY()); clickRelativePlayer.set(mousePressedPosition.x - player.position.x, -(mousePressedPosition.y - player.position.y)); distanceToMouse.x = (float) Math.sqrt( clickRelativePlayer.x * clickRelativePlayer.x + clickRelativePlayer.y * clickRelativePlayer.y); bulletVector.x = ((windowSize.x + windowSize.y) * clickRelativePlayer.x) + player.position.x; bulletVector.y = ((windowSize.x + windowSize.y) * -clickRelativePlayer.y) + player.position.y; }
From source file:MeshBoneUtil.CreatureManager.java
License:Open Source License
public String ProcessContactBone(Vector2 pt_in, float radius, MeshBone bone_in) { String ret_name = ""; Vector3 diff_vec = bone_in.getWorldEndPt().cpy().sub(bone_in.getWorldStartPt()); Vector2 cur_vec = new Vector2(diff_vec.x, diff_vec.y); float cur_length = (float) cur_vec.len(); Vector2 unit_vec = cur_vec.cpy(); unit_vec.nor(); Vector2 norm_vec = new Vector2(unit_vec.y, unit_vec.x); Vector2 src_pt = new Vector2(bone_in.getWorldStartPt().x, bone_in.getWorldStartPt().y); Vector2 rel_vec = pt_in.cpy().sub(src_pt); float proj = (float) rel_vec.dot(unit_vec); if ((proj >= 0) && (proj <= cur_length)) { float norm_proj = (float) rel_vec.dot(norm_vec); if (norm_proj <= radius) { return bone_in.getKey(); }//from w w w. j a v a 2s.c o m } Vector<MeshBone> cur_children = bone_in.getChildren(); for (MeshBone cur_child : cur_children) { ret_name = ProcessContactBone(pt_in, radius, cur_child); if (!(ret_name.equals(""))) { break; } } return ret_name; }
From source file:org.destinationsol.game.planet.PlanetManager.java
License:Apache License
private void applyGrav(SolGame game, SolSystem nearestSys) { float npGh = myNearestPlanet.getGroundHeight(); float npFh = myNearestPlanet.getFullHeight(); float npMinH = myNearestPlanet.getMinGroundHeight(); Vector2 npPos = myNearestPlanet.getPos(); Vector2 sysPos = nearestSys.getPos(); float npGravConst = myNearestPlanet.getGravConst(); List<SolObject> objs = game.getObjMan().getObjs(); for (int i = 0, objsSize = objs.size(); i < objsSize; i++) { SolObject obj = objs.get(i);// w ww . j a v a 2 s . c om if (!obj.receivesGravity()) continue; Vector2 objPos = obj.getPosition(); float minDist; Vector2 srcPos; float gravConst; boolean onPlanet; float toNp = npPos.dst(objPos); float toSys = sysPos.dst(objPos); if (toNp < npFh) { if (recoverObj(obj, toNp, npMinH)) continue; minDist = npGh; srcPos = npPos; gravConst = npGravConst; onPlanet = true; } else if (toSys < Const.SUN_RADIUS) { minDist = SunSingleton.SUN_HOT_RAD; srcPos = sysPos; gravConst = SunSingleton.GRAV_CONST; onPlanet = false; } else { continue; } Vector2 grav = SolMath.getVec(srcPos); grav.sub(objPos); float len = grav.len(); grav.nor(); if (len < minDist) { len = minDist; } float g = gravConst / len / len; grav.scl(g); obj.receiveForce(grav, game, true); SolMath.free(grav); if (!onPlanet) { mySunSingleton.doDmg(game, obj, toSys); } } }
From source file:releasethekraken.entity.pirate.EntityGunTower.java
public void attack() { float newtonForce = 150F; //The amount of force applied to the projectile if (target != null) { Vector2 difference = this.target.getPos().cpy().sub(this.getPos()); //Get the difference vector difference.nor().scl(newtonForce); //Normalize it to a unit vector, and scale it new EntityBullet(this.world, this.getPos().x, this.getPos().y, difference.x, difference.y, this, 2); } // target// w w w.j av a2 s .c o m }
From source file:releasethekraken.entity.pirate.EntityPirateCannon.java
public void attack() { float newtonForce = 1000F; //The amount of force applied to the projectile if (target != null) { Vector2 difference = this.target.getPos().cpy().sub(this.getPos()); //Get the difference vector difference.nor().scl(newtonForce); //Normalize it to a unit vector, and scale it new EntityCannonBomb(this.world, this.getPos().x, this.getPos().y, difference.x, difference.y, this, 25);/*from w w w.ja v a 2s . c om*/ } // target }
From source file:releasethekraken.entity.seacreature.EntityFish.java
@Override public void attack(int damage) { EntityPirate target = this.world.getClosestTarget(this, EntityPirate.class, 25, true); //System.out.println(target.toString()); float newtonForce = 100F; //The amount of force applied to the projectile if (target != null) { Vector2 difference = target.getPos().cpy().sub(this.getPos()); //Get the difference vector difference.nor().scl(newtonForce); //Normalize it to a unit vector, and scale it new EntityWaterSquirt(this.world, this.getPos().x, this.getPos().y, difference.x, difference.y, this, damage);//from www.j av a 2 s. co m } // target }
From source file:releasethekraken.entity.seacreature.EntityOrca.java
/** * Builds and adds a new projectile to the world *//*from w w w.ja v a 2 s. co m*/ @Override public void attack(int damage) { EntityPirate target = this.world.getClosestTarget(this, EntityPirate.class, 50, true); //System.out.println(target.toString()); float newtonForce = 1200F; //The amount of force applied to the projectile if (target != null) { Vector2 difference = target.getPos().cpy().sub(this.getPos()); //Get the difference vector difference.nor().scl(newtonForce); //Normalize it to a unit vector, and scale it new EntityWaterBomb(this.world, this.getPos().x, this.getPos().y, difference.x, difference.y, this, damage); } // target }
From source file:releasethekraken.entity.seacreature.EntityShark.java
@Override public void update() { super.update(); //Move towards enemies if (this.world.getWorldTime() % ReleaseTheKraken.TICK_RATE == 0) //Acquire a target every second this.target = this.world.getClosestTarget(this, EntityPirate.class, 20, true); Vector2 targetPos;/*from www.j a v a 2 s . c o m*/ if (this.target != null) targetPos = this.target.getPos(); //Move towards the target else targetPos = this.world.getPathTargetPos(this); //Follow the path //Move Vector2 difference = targetPos.sub(this.getPos()); difference.nor().scl(this.moveForce); this.physBody.applyForce(difference, this.getPos(), true); float angleToTarget = targetPos.sub(this.getPos()).angle(); //Convert to -180 to 180 while (angleToTarget > 180) angleToTarget -= 360; while (angleToTarget < -180) angleToTarget += 360; //Get the angle difference float angle = angleToTarget - (this.physBody.getAngle() * MathUtils.radiansToDegrees); //Convert to -180 to 180 while (angle > 180) angle -= 360; while (angle < -180) angle += 360; //Gdx.app.log(this.getClass().getSimpleName(), String.format("target: %20.20s, targetPos: %20.20s, angleToTarget: %10.10s, relativeAngle: %10.10s", this.target, targetPos, angleToTarget, angle)); //Calculate torque to apply float torque = (angle > 0 ? 2000F : -2000F); //Rotate towards velocity this.physBody.applyTorque(torque, true); //The range at which the attack animation starts playing float attackRange = 6; //Change animation to attack animation if the distance is smaller than attackRange if (this.target != null && this.physBody.getPosition().sub(this.target.getPos()).len() < attackRange) this.currentAnimation = GameAssets.entitySharkAttackMoveAnimation; else this.currentAnimation = GameAssets.entitySharkMoveAnimation; }
From source file:releasethekraken.entity.seacreature.EntityTurtle.java
@Override public void attack(int damage) { EntityPirate target = this.world.getClosestTarget(this, EntityPirate.class, 35, true); //System.out.println(target.toString()); float newtonForce = 100F; //The amount of force applied to the projectile if (target != null) { Vector2 difference = target.getPos().cpy().sub(this.getPos()); //Get the difference vector difference.nor().scl(newtonForce); //Normalize it to a unit vector, and scale it new EntityWaterSquirt(this.world, this.getPos().x, this.getPos().y, difference.x, difference.y, this, damage);/*w ww .j ava 2s . c o m*/ } // target }