# Java Code Examples for com.badlogic.gdx.math.Vector2.scl()

The following are Jave code examples for showing how to use scl() of the class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
 Project: cocos2d-java   File: CollideAlgorithms.java   Source Code and License Vote up 6 votes
```/**测试在Axis轴上的A、B是否分离  并 将轴用于MTD测试
* @param Axis
* @param A
* @param B
* @param positionA
* @param positionB
* @return <code>true</code> 分离
* <code>false</code> 未分离 */
public static final boolean axisSeparatePolygonsMTD(final Vector2 Axis, final Polygon A, final Polygon B,
final Vector2 positionA, final Vector2 positionB) {
pair=CalculateInterval(Axis, A, positionA);
final float mina=pair.min;
final float maxa=pair.max;

pair=CalculateInterval(Axis, B, positionB);
final float minb=pair.min;
final float maxb=pair.max;

if (mina > maxb || minb > maxa)
return true;

// find the interval overlap
final float d0 = maxa - minb;
final float d1 = maxb - mina;
final float depth = (d0 < d1)? d0 : d1;

// convert the separation axis into a push vector (re-normalise
// the axis and multiply by interval overlap)
float axisLengthSquared = V2.dot(Axis, Axis);
Axis.scl(depth / axisLengthSquared);
return false;
}
```
Example 2
 Project: cocos2d-java   File: CollideAlgorithms.java   Source Code and License Vote up 6 votes
```/**测试X轴向上的两个AABB是否分离
* <br>true为分离  axis为x轴的单位向量 必须传入(1,0)<br>
* <b>使用两个pool</b>
* @param Axis
* @param A
* @param B
* @param positionA
* @param positionB
* @return*/
public static final boolean axisSeparateAABBMTDX(final Vector2 axis,
final AABBShape A, final AABBShape B,
final Vector2 positionA, final Vector2 positionB) {

final float mina=positionA.x+A.getPoints()[0].x;
final float maxa=positionA.x+A.getPoints()[2].x;
final float minb=positionB.x+B.getPoints()[0].x;
final float maxb=positionB.x+B.getPoints()[2].x;

if (mina > maxb || minb > maxa)
return true;

// find the interval overlap
final float d0 = maxa - minb;
final float d1 = maxb - mina;
final float depth = (d0 < d1)? d0 : d1;
// convert the separation axis into a push vector (re-normalise
// the axis and multiply by interval overlap)
axis.scl(depth);
return false;
}
```
Example 3
 Project: cocos2d-java   File: CollideAlgorithms.java   Source Code and License Vote up 6 votes
```/**测试在Axis轴上的A、B是否分离
* 用于MTD测试的方法
* <br>true为分离
* @param Axis
* @param A
* @param B
* @return */
public static final boolean axisSeparatePolygonsMTD(final Vector2 Axis, final Polygon A, final AABBShape B,
final Vector2 positionA, final Vector2 positionB ) {
pair=CalculateInterval(Axis, A, positionA);
final float mina=pair.min;
final float maxa=pair.max;

pair=CalculateInterval(Axis, B, positionB);
final float minb=pair.min;
final float maxb=pair.max;

if (mina > maxb || minb > maxa)
return true;

// find the interval overlap
final float d0 = maxa - minb;
final float d1 = maxb - mina;
final float depth = (d0 < d1)? d0 : d1;

// convert the separation axis into a push vector (re-normalise
// the axis and multiply by interval overlap)
float axisLengthSquared = V2.dot(Axis, Axis);
Axis.scl(depth / axisLengthSquared);
return false;
}
```
Example 4
 Project: cocos2d-java   File: CollideAlgorithms.java   Source Code and License Vote up 6 votes
```/**测试在X轴上的A、B是否分离
* 用于MTD测试的方法
* Axis必须为(1,0)
* @param Axis
* @param A
* @param B
* @return <code>true</code> A与B分离
* <code>false</code> A与B重合 */
public static final boolean axisSeparatePolygonsMTDX(final Vector2 Axis, final Polygon A, final AABBShape B,
final Vector2 positionA, final Vector2 positionB ) {
pair=CalculateInterval(Axis, A, positionA);
final float mina=pair.min;
final float maxa=pair.max;

final float minb=B.getPoints()[0].x+positionB.x;
final float maxb=B.getPoints()[2].x+positionB.x;

if (mina > maxb || minb > maxa)
return true;

// find the interval overlap
final float d0 = maxa - minb;
final float d1 = maxb - mina;
final float depth = (d0 < d1)? d0 : d1;

//直接求出即可
Axis.scl(depth);
return false;
}
```
Example 5
 Project: cocos2d-java   File: CollideAlgorithms.java   Source Code and License Vote up 6 votes
```/**测试在Y轴上的A、B是否分离
* 用于MTD测试的方法
* Axis必须为(0,1)
* <br>true为分离
* @param Axis
* @param A
* @param B
* @return <code>true</code> A与B分离
* <code>false</code> A与B重合*/
public static final boolean axisSeparatePolygonsMTDY(final Vector2 Axis, final Polygon A, final AABBShape B,
final Vector2 positionA, final Vector2 positionB) {
pair=CalculateInterval(Axis, A, positionA);
final float mina=pair.min;
final float maxa=pair.max;

final float minb=B.getPoints()[0].y+positionB.y;
final float maxb=B.getPoints()[2].y+positionB.y;

if (mina > maxb || minb > maxa)
return true;

// find the interval overlap
final float d0 = maxa - minb;
final float d1 = maxb - mina;
final float depth = (d0 < d1)? d0 : d1;

Axis.scl(depth);
return false;
}
```
Example 6
 Project: school-game   File: BaseEntity.java   Source Code and License Vote up 6 votes
```/**
* Erlaubt es den Unterklassen möglichst einfach einen beliebigen Box2D Körper zu erstellen.
*
* @param position die Startposition des Body
* @param shape die Form, die für dne Body verwendet werden soll
* @param type der Typ des Körpers
* @return ein Box2D Körper
*/
protected Body createEntityBody(Vector2 position, Shape shape, BodyDef.BodyType type)
{
position.scl(Physics.MPP);

BodyDef bodyDef = new BodyDef();
bodyDef.type = type;
bodyDef.position.set(position);
bodyDef.fixedRotation = true;

Body body = worldObjectManager.getPhysicalWorld().createBody(bodyDef);
body.setUserData(this);

FixtureDef fixture = new FixtureDef();
fixture.shape = shape;
fixture.filter.categoryBits = Physics.CATEGORY_ENTITIES;

body.createFixture(fixture).setUserData(this);

shape.dispose();

return body;
}
```
Example 7
 Project: cocos2d-java   File: CollideAlgorithms.java   Source Code and License Vote up 5 votes
```/**圆与圆的MTD相交测试
* @param A
* @param B
* @param positionA
* @param positionB
* @param MTD
* @return <code>true</code> 形状相交
* <code>false</code> 形状不相交 */
public static final boolean Intersect(final Circle A,final Circle B,
final Vector2 positionA,final Vector2 positionB,final Vector2 MTD){

//pool1为MTD的方向 pool2保存pool1的向量
pool1.set(positionA.x+A.circleCenter.x-positionB.x-B.circleCenter.x,
positionA.y+A.circleCenter.y-positionB.y-B.circleCenter.y);
pool2.set(pool1);

//求出向量的模并单位化向量
final float length = V2.normalize(pool1);

//计算两圆的刺入深度

if(depth >= 0){
MTD.set(pool1);
MTD.scl(depth);
// MTD方向应该是由positionB指向positionA
if (V2.dot(pool1, MTD)<0) {
V2.negate(MTD);
}
return true;
}

return false;
}
```
Example 8
 Project: miniventure   File: Player.java   Source Code and License Vote up 5 votes
```public void checkInput(float delta, @NotNull Vector2 mouseInput) {
// checks for keyboard input to move the player.
// getDeltaTime() returns the time passed between the last and the current frame in seconds.
int speed = Tile.SIZE * 5; // this is technically in units/second.
Vector2 movement = new Vector2();
if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) movement.x--;
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) movement.x++;
if(Gdx.input.isKeyPressed(Input.Keys.UP)) movement.y++;
if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) movement.y--;

movement.nor();

movement.nor();

movement.scl(speed * delta);

move(movement.x, movement.y);

if(pressingKey(Input.Keys.Q)) cycleHeldItem(false);
if(pressingKey(Input.Keys.E)) cycleHeldItem(true);

if(pressingKey(Input.Keys.C))
attack();
else if(pressingKey(Input.Keys.V))
interact();

heldItem = heldItem == null ? null : heldItem.consume();
}
```
Example 9
 Project: Klooni1010   File: Piece.java   Source Code and License Vote up 5 votes
```Vector2 calculateGravityCenter() {
int filledCount = 0;
Vector2 result = new Vector2();
for (int i = 0; i < cellRows; ++i) {
for (int j = 0; j < cellCols; ++j) {
if (shape[i][j]) {
filledCount++;
pos.x + j * cellSize - cellSize * 0.5f,
pos.y + i * cellSize - cellSize * 0.5f);
}
}
}
return result.scl(1f / filledCount);
}
```
Example 10
 Project: KyperBox   File: GameLayer.java   Source Code and License Vote up 5 votes
```public Vector2 project(Vector2 coords) {
Viewport view = layer.getState().getGame().getView();
view.project(coords);
coords.scl(getZoom());
coords.set(coords.x + position.x, coords.y + position.y);
return coords;
}
```
Example 11
 Project: school-game   File: TutorialDummy.java   Source Code and License Vote up 5 votes
```@Override
public void render(Batch batch, float deltaTime)
{
if (body == null) return;

Color oldColor = batch.getColor();

batch.setColor(1f, (health + 5)/10f, (health + 5)/10f, 1f);

Vector2 pos = body.getPosition();
pos.scl(Physics.PPM);

TextureRegion region = MathUtils.isZero(hitTimer) ? dummy : dummy_hit;

batch.draw(region,                                    // TextureRegion (front, back, side)
pos.x - dummy.getRegionWidth() / 2,           // Offset to the X position (character center)
pos.y,                                        // Y position is at the foots
dummy.getRegionWidth() / 2,                   // Origin X (important for flipping)
dummy.getRegionHeight(),                      // Origin Y
dummy.getRegionWidth(),                       // Width
dummy.getRegionHeight(),                      // Height
1f,                                           // Scale X (-1 to flip)
1f,                                           // Scale Y
0f);                                          // Rotation

batch.setColor(oldColor);
}
```
Example 12
 Project: school-game   File: Crab.java   Source Code and License Vote up 5 votes
```@Override
public void render(Batch batch, float deltaTime)
{
if (body == null) return;

animationTime += deltaTime;

Color oldColor = batch.getColor();

batch.setColor(1f, (health + 4)/8f, (health + 4)/8f, 1f);

Vector2 pos = body.getPosition();
pos.scl(Physics.PPM);

TextureRegion region = crabAnimation.getKeyFrame(animationTime);

batch.draw(region,                                    // TextureRegion (front, back, side)
pos.x - region.getRegionWidth() / 2,           // Offset to the X position (character center)
pos.y,                                        // Y position is at the foots
region.getRegionWidth() / 2,                   // Origin X (important for flipping)
region.getRegionHeight(),                      // Origin Y
region.getRegionWidth(),                       // Width
region.getRegionHeight(),                      // Height
1f,                                           // Scale X (-1 to flip)
1f,                                           // Scale Y
0f);                                          // Rotation

batch.setColor(oldColor);
}
```
Example 13
 Project: school-game   File: Rabbit.java   Source Code and License Vote up 5 votes
```@Override
public void render(Batch batch, float deltaTime)
{
if (body == null) return;

Color oldColor = batch.getColor();

batch.setColor(1f, (health + 5)/10f, (health + 5)/10f, 1f);

Vector2 pos = body.getPosition();
pos.scl(Physics.PPM);

float scaleX = 1f;

animationTime += deltaTime;

batch.draw(rabbit,                                      // TextureRegion (front, back, side)
pos.x - rabbit.getRegionWidth() / 2,           // Offset to the X position (character center)
pos.y,                                          // Y position is at the foots
rabbit.getRegionWidth() / 2,                   // Origin X (important for flipping)
rabbit.getRegionHeight(),                      // Origin Y
rabbit.getRegionWidth(),                       // Width
rabbit.getRegionHeight(),                      // Height
scaleX,                                         // Scale X (-1 to flip)
1f,                                             // Scale Y
0f);                                            // Rotation

batch.setColor(oldColor);
}
```
Example 14
 Project: school-game   File: Wolf.java   Source Code and License Vote up 4 votes
```@Override
public void render(Batch batch, float deltaTime)
{
if (body == null) return;

Color oldColor = batch.getColor();

batch.setColor(1f, (health + 5)/10f, (health + 5)/10f, 1f);

Vector2 pos = body.getPosition();
pos.scl(Physics.PPM);

TextureRegion region = null;
float scaleX = 1f;

animationTime += deltaTime;

switch (orientation)
{
case LOOK_FORWARD:
region = npcFrontWalk.getKeyFrame(animationTime);
break;
case LOOK_LEFT:
region = npcSideWalk.getKeyFrame(animationTime);
break;
case LOOK_BACKWARD:
region = npcBackWalk.getKeyFrame(animationTime);
scaleX = -1f;
break;
case LOOK_RIGHT:
region = npcSideWalk.getKeyFrame(animationTime);
scaleX = -1f;
break;
}

batch.draw(region,                                      // TextureRegion (front, back, side)
pos.x - region.getRegionWidth() / 2,           // Offset to the X position (character center)
pos.y,                                          // Y position is at the foots
region.getRegionWidth() / 2,                   // Origin X (important for flipping)
region.getRegionHeight(),                      // Origin Y
region.getRegionWidth(),                       // Width
region.getRegionHeight(),                      // Height
scaleX,                                         // Scale X (-1 to flip)
1f,                                             // Scale Y
0f);                                            // Rotation

batch.setColor(oldColor);
}
```
Example 15
```/**
* Computes the Polygon Properties of a given Polygon.
*
* @param polygon The polygon to be analyzed.
* @return The Polygon Properties computed.
*/
public static PolygonProperties computePolygonProperties(Vector2[] polygon) {
PolygonProperties polygonProperties = null;

int count = polygon.length;

if (count >= 3) {
Vector2 centroid = new Vector2(0, 0);
float area = 0;

Vector2 refPoint = new Vector2(0, 0);
float threeInverse = 1 / 3f;

for (int i = 0; i < count; i++) {
/*
* Create a new vector to represent the reference point for
* forming triangles. Then use refPoint, polygonVertex and
* thirdTriangleVertex as vertices of a triangle.
*/
refPoint.set(0, 0);
Vector2 polygonVertex = polygon[i];
Vector2 thirdTriangleVertex = i + 1 < count ? polygon[i + 1]
: polygon[0];

Vector2 firstDirectionVector = polygonVertex.sub(refPoint);
Vector2 secondDirectionVector = thirdTriangleVertex
.sub(refPoint);

float triangleArea = firstDirectionVector
.crs(secondDirectionVector) / 2;
area += triangleArea;

/* Area weighted centroid */
.scl(triangleArea * threeInverse));
}

if (area > EPSILON) {
centroid.scl(1 / area);
} else {
area = 0;
}

polygonProperties = new PolygonProperties(centroid, area);
}

return polygonProperties;
}
```
Example 16
 Project: projecttd   File: MoveToSystem.java   Source Code and License Vote up 4 votes
```@Override
public void processEntity(Entity entity, float deltaTime) {
TransformComponent transform = pm.get(entity);
VelocityComponent velocity = vm.get(entity);
PathComponent path = pathM.get(entity);
MinionComponent minionComponent = minionM.get(entity);

if (path.nextPoint == null) {
velocity.linear.setZero();
velocity.angular = 0;
return;
}

// calc dir and len
Vector2 toTarget = temp.set(path.nextPoint).sub(transform.position);
float distance = toTarget.len();
// don't go too far!
if (distance <= 0.05) {
velocity.linear.setZero();
velocity.angular = 0;
path.nextPoint = null;
return;
}

float maxSpeed = minionComponent.speed;
System.out.println("speed " + maxSpeed);
// Target velocity combines speed and direction
Vector2 targetVelocity = toTarget.scl(maxSpeed / distance); // Optimized code for:
// toTarget.nor().scl(maxSpeed)

// Acceleration tries to get to the nextPoint velocity without exceeding max acceleration
targetVelocity.sub(velocity.linear).scl(1f / linearAccelerationTime).limit(maxLinearAcceleration);

// set it
velocity.linear.set(toTarget);

// angular

// Check for a zero direction, and set to 0 is so
if (velocity.linear.isZero(zeroThreshold)) {
velocity.angular = 0;
return;
}

// Calculate the orientation based on the velocity of the owner
float targetOrientation = VectorUtil.vectorToAngle(velocity.linear);

// Get the rotation direction to the nextPoint wrapped to the range [-PI, PI]
float rotation = ArithmeticUtils.wrapAngleAroundZero(targetOrientation - (transform.rotation - 90) * MathUtils.degreesToRadians);

// Absolute rotation
float rotationSize = rotation < 0f ? -rotation : rotation;

// Check if we are there, set velocity to 0 and return if so
if (rotationSize <= 0.1) {
velocity.angular = 0;
return;
}

// Use maximum rotation
float targetRotation = maxAngularSpeed;

// The final nextPoint rotation combines
// speed (already in the variable) and direction
targetRotation *= rotation / rotationSize;

// Acceleration tries to get to the nextPoint rotation
velocity.angular = (targetRotation - velocity.angular) / angularAccelerationTime;

// Check if the absolute acceleration is too great
float angularAcceleration = velocity.angular < 0f ? -velocity.angular : velocity.angular;
if (angularAcceleration > maxAngularAcceleration) {
velocity.angular *= maxAngularAcceleration / angularAcceleration;
}
}
```
Example 17
 Project: school-game   File: BaseNPC.java   Source Code and License Vote up 4 votes
```/**
* Rendert das Objekt in die Map.
*
* @param batch der Batch der Map
* @param deltaTime die Zeit, die seit dem letzten Frame vergangen ist
*/
@Override
public void render(Batch batch, float deltaTime)
{
if (body == null) return;

Vector2 pos = body.getPosition();
pos.scl(Physics.PPM);

TextureRegion region = null;
float scaleX = 1f;

boolean notMoving = MathUtils.isZero(body.getLinearVelocity().x, 0.5f) && MathUtils.isZero(body.getLinearVelocity().y, 0.5f);

if (notMoving)
{
animationTime = 0f;
} else {
animationTime += deltaTime;
}

switch (orientation)
{
case LOOK_FORWARD:
region = notMoving ? npcFront : npcFrontWalk.getKeyFrame(animationTime);
break;
case LOOK_LEFT:
region = notMoving ? npcSide : npcSideWalk.getKeyFrame(animationTime);
scaleX = -1f;
break;
case LOOK_BACKWARD:
region = notMoving ? npcBack : npcBackWalk.getKeyFrame(animationTime);
scaleX = -1f;
break;
case LOOK_RIGHT:
region = notMoving ? npcSide : npcSideWalk.getKeyFrame(animationTime);
break;
}

batch.draw(region,                                      // TextureRegion (front, back, side)
pos.x - npcSide.getRegionWidth() / 2,           // Offset to the X position (character center)
pos.y,                                          // Y position is at the foots
npcSide.getRegionWidth() / 2,                   // Origin X (important for flipping)
npcSide.getRegionHeight(),                      // Origin Y
npcSide.getRegionWidth(),                       // Width
npcSide.getRegionHeight(),                      // Height
scaleX,                                         // Scale X (-1 to flip)
1f,                                             // Scale Y
0f);                                            // Rotation
}
```
Example 18
 Project: school-game   File: Dragon.java   Source Code and License Vote up 4 votes
```@Override
public void render(Batch batch, float deltaTime)
{
if (body == null) return;

Color oldColor = batch.getColor();

batch.setColor(1f, (health + 30)/60f, (health + 30)/60f, 1f);

Vector2 pos = body.getPosition();
pos.scl(Physics.PPM);

TextureRegion region = null;
float scaleX = 1f;

animationTime += deltaTime;

switch (orientation)
{
case LOOK_FORWARD:
region = npcFrontWalk.getKeyFrame(animationTime);
break;
case LOOK_LEFT:
region = npcSideWalk.getKeyFrame(animationTime);
break;
case LOOK_BACKWARD:
region = npcBackWalk.getKeyFrame(animationTime);
scaleX = -1f;
break;
case LOOK_RIGHT:
region = npcSideWalk.getKeyFrame(animationTime);
scaleX = -1f;
break;
}

batch.draw(region,                                      // TextureRegion (front, back, side)
pos.x - region.getRegionWidth() / 2,           // Offset to the X position (character center)
pos.y,                                          // Y position is at the foots
region.getRegionWidth() / 2,                   // Origin X (important for flipping)
region.getRegionHeight(),                      // Origin Y
region.getRegionWidth(),                       // Width
region.getRegionHeight(),                      // Height
scaleX,                                         // Scale X (-1 to flip)
1f,                                             // Scale Y
0f);                                            // Rotation

batch.setColor(oldColor);
}
```
Example 19
 Project: school-game   File: Warrior.java   Source Code and License Vote up 4 votes
```@Override
public void render(Batch batch, float deltaTime)
{
if (body == null) return;

Color oldColor = batch.getColor();

batch.setColor(1f, (health + 30)/60f, (health + 30)/60f, 1f);

Vector2 pos = body.getPosition();
pos.scl(Physics.PPM);

TextureRegion region = null;
float scaleX = 1f;

animationTime += deltaTime;

switch (orientation)
{
case LOOK_FORWARD:
region = npcFrontWalk.getKeyFrame(animationTime);
break;
case LOOK_LEFT:
region = npcSideWalk.getKeyFrame(animationTime);
scaleX = -1f;
break;
case LOOK_BACKWARD:
region = npcBackWalk.getKeyFrame(animationTime);
scaleX = -1f;
break;
case LOOK_RIGHT:
region = npcSideWalk.getKeyFrame(animationTime);
break;
}

batch.draw(region,                                      // TextureRegion (front, back, side)
pos.x - region.getRegionWidth() / 2,           // Offset to the X position (character center)
pos.y,                                          // Y position is at the foots
region.getRegionWidth() / 2,                   // Origin X (important for flipping)
region.getRegionHeight(),                      // Origin Y
region.getRegionWidth(),                       // Width
region.getRegionHeight(),                      // Height
scaleX,                                         // Scale X (-1 to flip)
1f,                                             // Scale Y
0f);                                            // Rotation

batch.setColor(oldColor);
}
```
Example 20
 Project: school-game   File: Skeleton.java   Source Code and License Vote up 4 votes
```@Override
public void render(Batch batch, float deltaTime)
{
if (body == null) return;

Color oldColor = batch.getColor();

batch.setColor(1f, (health + 2)/4f, (health + 2)/4f, 1f);

Vector2 pos = body.getPosition();
pos.scl(Physics.PPM);

TextureRegion region = null;
float scaleX = 1f;

animationTime += deltaTime;

switch (orientation)
{
case LOOK_FORWARD:
region = npcFrontWalk.getKeyFrame(animationTime);
break;
case LOOK_LEFT:
region = npcSideWalk.getKeyFrame(animationTime);
break;
case LOOK_BACKWARD:
region = npcBackWalk.getKeyFrame(animationTime);
scaleX = -1f;
break;
case LOOK_RIGHT:
region = npcSideWalk.getKeyFrame(animationTime);
scaleX = -1f;
break;
}

batch.draw(region,                                      // TextureRegion (front, back, side)
pos.x - region.getRegionWidth() / 2,           // Offset to the X position (character center)
pos.y,                                          // Y position is at the foots
region.getRegionWidth() / 2,                   // Origin X (important for flipping)
region.getRegionHeight(),                      // Origin Y
region.getRegionWidth(),                       // Width
region.getRegionHeight(),                      // Height
scaleX,                                         // Scale X (-1 to flip)
1f,                                             // Scale Y
0f);                                            // Rotation

batch.setColor(oldColor);
}
```