Java Code Examples for com.badlogic.gdx.math.MathUtils#sin()

The following examples show how to use com.badlogic.gdx.math.MathUtils#sin() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Enemy.java    From ud406 with MIT License 6 votes vote down vote up
public void update(float delta) {
    switch (direction) {
        case LEFT:
            position.x -= Constants.ENEMY_MOVEMENT_SPEED * delta;
            break;
        case RIGHT:
            position.x += Constants.ENEMY_MOVEMENT_SPEED * delta;
    }

    if (position.x < platform.left) {
        position.x = platform.left;
        direction = Direction.RIGHT;
    } else if (position.x > platform.right) {
        position.x = platform.right;
        direction = Direction.LEFT;
    }

    final float elapsedTime = Utils.secondsSince(startTime);
    final float bobMultiplier = 1 + MathUtils.sin(MathUtils.PI2 * elapsedTime / Constants.ENEMY_BOB_PERIOD);
    position.y = platform.top + Constants.ENEMY_CENTER.y + Constants.ENEMY_BOB_AMPLITUDE * bobMultiplier;
}
 
Example 2
Source File: Enemy.java    From ud406 with MIT License 6 votes vote down vote up
public void update(float delta) {
    switch (direction) {
        case LEFT:
            position.x -= Constants.ENEMY_MOVEMENT_SPEED * delta;
            break;
        case RIGHT:
            position.x += Constants.ENEMY_MOVEMENT_SPEED * delta;
    }

    if (position.x < platform.left) {
        position.x = platform.left;
        direction = Direction.RIGHT;
    } else if (position.x > platform.right) {
        position.x = platform.right;
        direction = Direction.LEFT;
    }

    final float elapsedTime = Utils.secondsSince(startTime);
    final float bobMultiplier = 1 + MathUtils.sin(MathUtils.PI2 * elapsedTime / Constants.ENEMY_BOB_PERIOD);
    position.y = platform.top + Constants.ENEMY_CENTER.y + Constants.ENEMY_BOB_AMPLITUDE * bobMultiplier;
}
 
Example 3
Source File: Enemy.java    From ud406 with MIT License 6 votes vote down vote up
public void update(float delta) {
    switch (direction) {
        case LEFT:
            position.x -= Constants.ENEMY_MOVEMENT_SPEED * delta;
            break;
        case RIGHT:
            position.x += Constants.ENEMY_MOVEMENT_SPEED * delta;
    }

    if (position.x < platform.left) {
        position.x = platform.left;
        direction = Direction.RIGHT;
    } else if (position.x > platform.right) {
        position.x = platform.right;
        direction = Direction.LEFT;
    }

    final float elapsedTime = Utils.secondsSince(startTime);
    final float bobMultiplier = 1 + MathUtils.sin(MathUtils.PI2 * ((elapsedTime / Constants.ENEMY_BOB_PERIOD) + randomPhase));
    position.y = platform.top + Constants.ENEMY_CENTER.y + Constants.ENEMY_BOB_AMPLITUDE * bobMultiplier;
}
 
Example 4
Source File: Enemy.java    From ud406 with MIT License 6 votes vote down vote up
public void update(float delta) {
    switch (direction) {
        case LEFT:
            position.x -= Constants.ENEMY_MOVEMENT_SPEED * delta;
            break;
        case RIGHT:
            position.x += Constants.ENEMY_MOVEMENT_SPEED * delta;
    }

    if (position.x < platform.left) {
        position.x = platform.left;
        direction = Direction.RIGHT;
    } else if (position.x > platform.right) {
        position.x = platform.right;
        direction = Direction.LEFT;
    }

    final float elapsedTime = Utils.secondsSince(startTime);
    final float bobMultiplier = 1 + MathUtils.sin(MathUtils.PI2 * (bobOffset + elapsedTime / Constants.ENEMY_BOB_PERIOD));
    position.y = platform.top + Constants.ENEMY_CENTER.y + Constants.ENEMY_BOB_AMPLITUDE * bobMultiplier;
}
 
Example 5
Source File: Enemy.java    From ud406 with MIT License 6 votes vote down vote up
public void update(float delta) {
    switch (direction) {
        case LEFT:
            position.x -= Constants.ENEMY_MOVEMENT_SPEED * delta;
            break;
        case RIGHT:
            position.x += Constants.ENEMY_MOVEMENT_SPEED * delta;
    }

    if (position.x < platform.left) {
        position.x = platform.left;
        direction = Direction.RIGHT;
    } else if (position.x > platform.right) {
        position.x = platform.right;
        direction = Direction.LEFT;
    }

    final float elapsedTime = Utils.secondsSince(startTime);
    final float bobMultiplier = 1 + MathUtils.sin(MathUtils.PI2 * elapsedTime / Constants.ENEMY_BOB_PERIOD);
    position.y = platform.top + Constants.ENEMY_CENTER.y + Constants.ENEMY_BOB_AMPLITUDE * bobMultiplier;
}
 
Example 6
Source File: Enemy.java    From ud406 with MIT License 6 votes vote down vote up
public void update(float delta) {
    switch (direction) {
        case LEFT:
            position.x -= Constants.ENEMY_MOVEMENT_SPEED * delta;
            break;
        case RIGHT:
            position.x += Constants.ENEMY_MOVEMENT_SPEED * delta;
    }

    if (position.x < platform.left) {
        position.x = platform.left;
        direction = Direction.RIGHT;
    } else if (position.x > platform.right) {
        position.x = platform.right;
        direction = Direction.LEFT;
    }

    final float elapsedTime = Utils.secondsSince(startTime);
    final float bobMultiplier = 1 + MathUtils.sin(MathUtils.PI2 * elapsedTime / Constants.ENEMY_BOB_PERIOD);
    position.y = platform.top + Constants.ENEMY_CENTER.y + Constants.ENEMY_BOB_AMPLITUDE * bobMultiplier;
}
 
Example 7
Source File: Enemy.java    From ud406 with MIT License 6 votes vote down vote up
public void update(float delta) {
    switch (direction) {
        case LEFT:
            position.x -= Constants.ENEMY_MOVEMENT_SPEED * delta;
            break;
        case RIGHT:
            position.x += Constants.ENEMY_MOVEMENT_SPEED * delta;
    }

    if (position.x < platform.left) {
        position.x = platform.left;
        direction = Direction.RIGHT;
    } else if (position.x > platform.right) {
        position.x = platform.right;
        direction = Direction.LEFT;
    }

    final float elapsedTime = Utils.secondsSince(startTime);
    final float bobMultiplier = 1 + MathUtils.sin(MathUtils.PI2 * elapsedTime / Constants.ENEMY_BOB_PERIOD);
    position.y = platform.top + Constants.ENEMY_CENTER.y + Constants.ENEMY_BOB_AMPLITUDE * bobMultiplier;
}
 
Example 8
Source File: OrthographicCameraExercise.java    From ud405 with MIT License 6 votes vote down vote up
@Override
public void render() {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    // TODO: Call update() on the camera


    // TODO: Set the SceneRenderer's projection matrix equal to the camera's combined matrix


    renderer.begin(ShapeType.Filled);
    float interval = TimeUtils.timeSinceMillis(timeCreated);
    float x = X_CENTER + X_AMPLITUDE * MathUtils.sin(MathUtils.PI2 * interval /PERIOD);
    float y = Y_CENTER + Y_AMPLITUDE * MathUtils.sin(2* MathUtils.PI2 * interval / PERIOD);
    renderer.circle(x, y, BALL_RADIUS);
    renderer.end();
}
 
Example 9
Source File: Enemy.java    From ud406 with MIT License 6 votes vote down vote up
public void update(float delta) {
    switch (direction) {
        case LEFT:
            position.x -= Constants.ENEMY_MOVEMENT_SPEED * delta;
            break;
        case RIGHT:
            position.x += Constants.ENEMY_MOVEMENT_SPEED * delta;
    }

    if (position.x < platform.left) {
        position.x = platform.left;
        direction = Direction.RIGHT;
    } else if (position.x > platform.right) {
        position.x = platform.right;
        direction = Direction.LEFT;
    }

    final float elapsedTime = Utils.secondsSince(startTime);

    // TODO: And used it to make sure that the enemies don't all bob up and down at once
    final float bobMultiplier = 1 + MathUtils.sin(MathUtils.PI2 * ((elapsedTime / Constants.ENEMY_BOB_PERIOD) + randomPhase));

    position.y = platform.top + Constants.ENEMY_CENTER.y + Constants.ENEMY_BOB_AMPLITUDE * bobMultiplier;
}
 
Example 10
Source File: Enemy.java    From ud406 with MIT License 6 votes vote down vote up
public void update(float delta) {
    switch (direction) {
        case LEFT:
            position.x -= Constants.ENEMY_MOVEMENT_SPEED * delta;
            break;
        case RIGHT:
            position.x += Constants.ENEMY_MOVEMENT_SPEED * delta;
    }

    if (position.x < platform.left) {
        position.x = platform.left;
        direction = Direction.RIGHT;
    } else if (position.x > platform.right) {
        position.x = platform.right;
        direction = Direction.LEFT;
    }

    final float elapsedTime = Utils.secondsSince(startTime);
    final float bobMultiplier = 1 + MathUtils.sin(MathUtils.PI2 * (bobOffset + elapsedTime / Constants.ENEMY_BOB_PERIOD));
    position.y = platform.top + Constants.ENEMY_CENTER.y + Constants.ENEMY_BOB_AMPLITUDE * bobMultiplier;
}
 
Example 11
Source File: Enemy.java    From ud406 with MIT License 6 votes vote down vote up
public void update(float delta) {
    switch (direction) {
        case LEFT:
            position.x -= Constants.ENEMY_MOVEMENT_SPEED * delta;
            break;
        case RIGHT:
            position.x += Constants.ENEMY_MOVEMENT_SPEED * delta;
    }

    if (position.x < platform.left) {
        position.x = platform.left;
        direction = Direction.RIGHT;
    } else if (position.x > platform.right) {
        position.x = platform.right;
        direction = Direction.LEFT;
    }

    final float elapsedTime = Utils.secondsSince(startTime);
    final float bobMultiplier = 1 + MathUtils.sin(MathUtils.PI2 * (bobOffset + elapsedTime / Constants.ENEMY_BOB_PERIOD));
    position.y = platform.top + Constants.ENEMY_CENTER.y + Constants.ENEMY_BOB_AMPLITUDE * bobMultiplier;
}
 
Example 12
Source File: Enemy.java    From ud406 with MIT License 6 votes vote down vote up
public void update(float delta) {
    switch (direction) {
        case LEFT:
            position.x -= Constants.ENEMY_MOVEMENT_SPEED * delta;
            break;
        case RIGHT:
            position.x += Constants.ENEMY_MOVEMENT_SPEED * delta;
    }

    if (position.x < platform.left) {
        position.x = platform.left;
        direction = Direction.RIGHT;
    } else if (position.x > platform.right) {
        position.x = platform.right;
        direction = Direction.LEFT;
    }

    final float elapsedTime = Utils.secondsSince(startTime);

    // TODO: And used it to make sure that the enemies don't all bob up and down at once
    final float bobMultiplier = 1 + MathUtils.sin(MathUtils.PI2 * ((elapsedTime / Constants.ENEMY_BOB_PERIOD) + randomPhase));

    position.y = platform.top + Constants.ENEMY_CENTER.y + Constants.ENEMY_BOB_AMPLITUDE * bobMultiplier;
}
 
Example 13
Source File: Enemy.java    From ud406 with MIT License 6 votes vote down vote up
public void update(float delta) {
    switch (direction) {
        case LEFT:
            position.x -= Constants.ENEMY_MOVEMENT_SPEED * delta;
            break;
        case RIGHT:
            position.x += Constants.ENEMY_MOVEMENT_SPEED * delta;
    }

    if (position.x < platform.left) {
        position.x = platform.left;
        direction = Direction.RIGHT;
    } else if (position.x > platform.right) {
        position.x = platform.right;
        direction = Direction.LEFT;
    }

    final float elapsedTime = Utils.secondsSince(startTime);
    final float bobMultiplier = 1 + MathUtils.sin(MathUtils.PI2 * elapsedTime / Constants.ENEMY_BOB_PERIOD);
    position.y = platform.top + Constants.ENEMY_CENTER.y + Constants.ENEMY_BOB_AMPLITUDE * bobMultiplier;
}
 
Example 14
Source File: Enemy.java    From ud406 with MIT License 6 votes vote down vote up
public void update(float delta) {
    switch (direction) {
        case LEFT:
            position.x -= Constants.ENEMY_MOVEMENT_SPEED * delta;
            break;
        case RIGHT:
            position.x += Constants.ENEMY_MOVEMENT_SPEED * delta;
    }

    if (position.x < platform.left) {
        position.x = platform.left;
        direction = Direction.RIGHT;
    } else if (position.x > platform.right) {
        position.x = platform.right;
        direction = Direction.LEFT;
    }

    final float elapsedTime = Utils.secondsSince(startTime);
    final float bobMultiplier = 1 + MathUtils.sin(MathUtils.PI2 * (bobOffset + elapsedTime / Constants.ENEMY_BOB_PERIOD));
    position.y = platform.top + Constants.ENEMY_CENTER.y + Constants.ENEMY_BOB_AMPLITUDE * bobMultiplier;
}
 
Example 15
Source File: DirectionActor.java    From riiablo with Apache License 2.0 5 votes vote down vote up
@Override
public void drawDebug(ShapeRenderer shapes) {
  //super.drawDebug(shapes);
  if (!isVisible()) return;

  float cx = getX() + r;
  float cy = getY() + r;

  shapes.setColor(Color.WHITE);
  shapes.set(ShapeRenderer.ShapeType.Filled);
  shapes.circle(cx, cy, r, 64);
  shapes.set(ShapeRenderer.ShapeType.Line);

  float snap = Direction.snapToDirection(angle, dirs);
  float dx = r * MathUtils.cos(snap);
  float dy = r * MathUtils.sin(snap);

  shapes.setColor(Color.BLUE);
  shapes.line(cx, cy, cx + dx, cy + dy);

  if (DEBUG_ANGLE) {
    dx = r * MathUtils.cos(angle);
    dy = r * MathUtils.sin(angle);

    shapes.setColor(Color.BLACK);
    shapes.line(cx, cy, cx + dx, cy + dy);
  }
}
 
Example 16
Source File: CCNode.java    From cocos2d-java with MIT License 4 votes vote down vote up
private CCAffineTransform nodeToParentTransform(){
		if (m_bTransformDirty){
//			CCPoint zero = CCPoint.getZero();
//			m_sTransform.setToIdentity();
//			
//			
//			if (m_fRotation != 0)
//				m_sTransform.rotate(-m_fRotation * MathUtils.degreesToRadians);
//			
//			m_bTransformDirty = false;
			
			// Translate values
	        float x = m_obPosition.x;
	        float y = m_obPosition.y;

	        if (m_bIgnoreAnchorPointForPosition) 
	        {
	            x += m_obAnchorPointInPoints.x;
	            y += m_obAnchorPointInPoints.y;
	        }
	     // Rotation values
			// Change rotation code to handle X and Y
			// If we skew with the exact same value for both x and y then we're simply just rotating
	        float cx = 1, sx = 0, cy = 1, sy = 0;
	        if ( (m_fRotationX != 0) || (m_fRotationY != 0) )
	        {
	            float radiansX = -MathUtil.CC_DEGREES_TO_RADIANS(m_fRotationX);
	            float radiansY = -MathUtil.CC_DEGREES_TO_RADIANS(m_fRotationY);
	            cx = MathUtils.cos(radiansX);
	            sx = MathUtils.sin(radiansX);
	            cy = MathUtils.cos(radiansY);
	            sy = MathUtils.sin(radiansY);
	        }

	        boolean needsSkewMatrix = ( (m_fSkewX != 0) || (m_fSkewY != 0) );


	        // optimization:
	        // inline anchor point calculation if skew is not needed
	        // Adjusted transform calculation for rotational skew
	        if (! needsSkewMatrix && !m_obAnchorPointInPoints.equalToPoint(CCPoint.Zero))
	        {
	            x += cy * -m_obAnchorPointInPoints.x * m_fScaleX + -sx * -m_obAnchorPointInPoints.y * m_fScaleY;
	            y += sy * -m_obAnchorPointInPoints.x * m_fScaleX +  cx * -m_obAnchorPointInPoints.y * m_fScaleY;
	        }


	        // Build Transform Matrix
	        // Adjusted transform calculation for rotational skew
	        
//	        m_sTransform = new CCAffineTransform( cy * m_fScaleX,  sy * m_fScaleX,
//	            -sx * m_fScaleY, cx * m_fScaleY,
//	            x, y );
	        m_sTransform.set(cy * m_fScaleX, sy * m_fScaleX, -sx * m_fScaleY, cx * m_fScaleY, x, y);
	        
	        

	        // XXX: Try to inline skew
	        // If skew is needed, apply skew and then anchor point
	        if (needsSkewMatrix) 
	        {
	            CCAffineTransform skewMatrix = new CCAffineTransform(1.0f, MathUtil.tanf(MathUtil.CC_DEGREES_TO_RADIANS(m_fSkewY)),
	            		MathUtil.tanf(MathUtil.CC_DEGREES_TO_RADIANS(m_fSkewX)), 1.0f,
	                0.0f, 0.0f );
	            m_sTransform = CCAffineTransform.multiply(skewMatrix, m_sTransform);

	            // adjust anchor point
	            if (!m_obAnchorPointInPoints.equalToPoint(CCPoint.Zero))
	            {
	                m_sTransform = CCAffineTransform.CCAffineTransformTranslate(m_sTransform, -m_obAnchorPointInPoints.x, -m_obAnchorPointInPoints.y);
	            }
	        }
	        
	        if (m_bAdditionalTransformDirty)
	        {
	            //m_sTransform = CCAffineTransform.multiply(m_sTransform, m_sAdditionalTransform);
	        	m_sTransform.multiply(m_sAdditionalTransform);
	            m_bAdditionalTransformDirty = false;
	        }

	        m_bTransformDirty = false;
			
			
		}
		
		return m_sTransform;
	}
 
Example 17
Source File: BouncingBall.java    From ud405 with MIT License 4 votes vote down vote up
private void randomKick() {
    Random random = new Random();
    float angle = MathUtils.PI2 * random.nextFloat();
    velocity.x += KICK_VELOCITY * MathUtils.cos(angle);
    velocity.y += KICK_VELOCITY * MathUtils.sin(angle);
}
 
Example 18
Source File: BouncingBall.java    From ud405 with MIT License 4 votes vote down vote up
private void randomKick() {
    Random random = new Random();
    float angle = MathUtils.PI2 * random.nextFloat();
    velocity.x += KICK_VELOCITY * MathUtils.cos(angle);
    velocity.y += KICK_VELOCITY * MathUtils.sin(angle);
}
 
Example 19
Source File: OscillatingCircle.java    From ud406 with MIT License 4 votes vote down vote up
public Circle getCurrentCircle(float elapsedTime) {
    float x = originX + magnitude * MathUtils.cos(angle) * MathUtils.sin(MathUtils.PI2 * elapsedTime / period);
    float y = originY + magnitude * MathUtils.sin(angle) * MathUtils.sin(MathUtils.PI2 * elapsedTime / period);
    return new Circle(x, y, radius);
}
 
Example 20
Source File: PointF.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 4 votes vote down vote up
public PointF polar( float a, float l ) {
	this.x = l * MathUtils.cos(a);
	this.y = l * MathUtils.sin( a );
	return this;
}