Android Open Source - Save-the-Planet Main Menu Item Ball






From Project

Back to project page Save-the-Planet.

License

The source code is released under:

Copyright (c) 2002 JSON.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software ...

If you think the Android project Save-the-Planet listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.karolmajta.stp.models;
//from  w ww . j a va2s  . c  o  m
import java.util.HashSet;

import com.karolmajta.procprox.Tap;
import com.karolmajta.stp.exception.NoDeferredException;
import com.karolmajta.stp.models.MainMenuItemBall.Deferred;

import android.util.Log;

public class MainMenuItemBall extends Tickable
  implements ICanCollide<MainMenuObstacleBall> {
  
  public abstract static class Deferred {
    public abstract void call();
  }
  
  private HashSet ignoredForCollision;
  
  // string label of this menu item
  private String label; 
  // coordinates of point where this ball is constrained
  private float x0; // pixels
  private float y0; // pixels
  // radius of the ball
  private float radius; // pixels
  // current coordinates of ball 
  private float currentX; // pixels
  private float currentY; // pixels
  // current velocities of ball
  private float currentVX;
  private float currentVY;
  // spring-dumping coefficients
  private float spring;
  private float damping;
  // mass
  private float mass;
  // deferred to launch when tapped
  private Deferred deferred;
  /**
   * 
   * @param label String label describing this menu item
   * @param x0 Constrain point (percentage of screen width)
   * @param y0 Constrain point (percentage of screen height)
   * @param radius Radius of ball (percentage of screen width)
   * @param currentX Current x coordinate of the center
   * @param currentY Current y coordinate of the center
   * @param currentVX Velocity x coordinate of the center
   * @param currentVY Velocity y coordinate of the center
   * @param spring Spring constant in the joint between ball and constraint
   * @param damping Damping factor between the joint and constraint
   * @param mass Mass of the ball
   */
  public MainMenuItemBall(
      String label,
      float x0,
      float y0,
      float radius,
      float currentX,
      float currentY,
      float currentVX,
      float currentVY,
      float spring,
      float damping,
      float mass
  ) {
    ignoredForCollision = new HashSet();
    
    this.label = label;
    this.x0 = x0;
    this.y0 = y0;
    this.radius = radius;
    this.currentX = currentX;
    this.currentY = currentY;
    this.currentVX = currentVX;
    this.currentVY = currentVY;
    this.spring = spring;
    this.damping = damping;
    this.mass = mass;
  }
  
  /**
   * Creates vanilla ball with empty string as label zero initial velocity
   * and current coordinates matching base coordinates.
   * 
   * @param x0
   * @param y0
   * @param radius
   * @param spring
   * @param damping
   * @param mass
   */
  public MainMenuItemBall(
      float x0,
      float y0,
      float radius,
      float spring,
      float damping,
      float mass
  ) {
    this("", x0, y0, radius, x0, y0, 0, 0, spring, damping, mass);
  }
  
  public String getLabel() {
    return label;
  }

  public void setLabel(String label) {
    this.label = label;
  }

  public float getX0() {
    return x0;
  }

  public void setX0(float x0) {
    this.x0 = x0;
  }

  public float getY0() {
    return y0;
  }

  public void setY0(float y0) {
    this.y0 = y0;
  }

  public float getRadius() {
    return radius;
  }

  public void setRadius(float radius) {
    this.radius = radius;
  }

  public float getCurrentX() {
    return currentX;
  }

  public void setCurrentX(float currentX) {
    this.currentX = currentX;
  }

  public float getCurrentY() {
    return currentY;
  }

  public void setCurrentY(float currentY) {
    this.currentY = currentY;
  }

  public float getCurrentVX() {
    return currentVX;
  }

  public void setCurrentVX(float currentVX) {
    this.currentVX = currentVX;
  }

  public float getCurrentVY() {
    return currentVY;
  }

  public void setCurrentVY(float currentVY) {
    this.currentVY = currentVY;
  }

  public float getSpring() {
    return spring;
  }

  public void setSpring(float spring) {
    this.spring = spring;
  }

  public float getDamping() {
    return damping;
  }

  public void setDamping(float damping) {
    this.damping = damping;
  }

  public float getMass() {
    return mass;
  }

  public void setMass(float mass) {
    this.mass = mass;
  }
  
  /**
   * 
   * @return Distance (difference between coordinates, so it can be less
   *     than zero!) between base x and current x.
   */
  public float getBaseDx() {
    return currentX-x0;
  }
  
  /**
   * 
   * @return Just like {@link MainMenuItemBall#getBaseDx()} but for the
   *     y axis.
   */
  public float getBaseDy() {
    return currentY-y0;
  }
  
  /**
   * 
   * @return Value of currentAX (acceleration x) that would be calculated
   *     in the next step of integration. 
   */
  public float getNextAX() {
    float dx = getBaseDx();
    float newAX = -(spring/mass)*dx -(damping/mass)*currentVX;
    return newAX;
  }
  
  /**
   *  
   * @return Value of currentAY (acceleration y) that would be calculated
   *     in the next step of integration
   */
  public float getNextAY() {
    float dy = getBaseDy();
    float newAY = -(spring/mass)*dy -(damping/mass)*currentVY;
    return newAY;
  }
  
  /**
   * 
   * @param dt
   * @return Value of currentVX (velocity x) that would be calculated in
   *     the next step of integration using time step dt.
   */
  public float getNextVX(long dt) {
    return currentVX+getNextAX()*dt;
  }
  
  /**
   * 
   * @param dt
   * @return Value of currentVY (velocity y) that would be calculated in
   *     the next step of integration using time step dt.
   */
  public float getNextVY(long dt) {
    return currentVY+getNextAY()*dt;
  }
  
  /**
   * 
   * @param dt
   * @return Value of currentX (coord x) that would be calculated in
   *     the next step of integration using time step dt.
   */
  public float getNextCurrentX(long dt) {
    float next = currentX+currentVX*dt;
    return next;
  }
  
  /**
   * 
   * @param dt
   * @return Value of currentY (coord y) that would be calculated in
   *     the next step of integration using time step dt.
   */
  public float getNextCurrentY(long dt) {
    float next = currentY+currentVY*dt;
    return next;
  }
  
  /**
   * 
   * @return Distance between center of ball and base point (constrains)
   */
  public float getBasePointDist() {
    float dxSquared = (x0-currentX)*(x0-currentX);
    float dySquared = (y0-currentY)*(y0-currentY);
    return (float)Math.sqrt(dxSquared+dySquared);
  }
  
  /**
   * Integrates new values of velocity and position and assigns them changing
   * state of the object
   * @param dt Integration step (milliseconds)
   */
  private void integrate(long dt) {
    float accX = getNextAX();
    float accY = getNextAY();
    float nextVX = currentVX+accX*dt;
    float nextVY = currentVY+accY*dt;
    float nextX = currentX+currentVX*dt;
    float nextY = currentY+currentVY*dt;
    
    currentVX = nextVX;
    currentVY = nextVY;
    currentX = nextX;
    currentY = nextY;
  }
  
  /**
   * Calls {@ling MainMenuItemBall#integrate(long)} with dt.
   */
  @Override
  public void onTick(long dt) {
    integrate(dt);
  }

  public void consumeTap(Tap t) throws NoDeferredException {
    float x = t.getStopX();
    float y = t.getStopY();
    float dist = (float)Math.sqrt((x-currentX)*(x-currentX)+(y-currentY)*(y-currentY));
    if(dist < radius){
      if(deferred != null){
        deferred.call();
      }else{
        throw new NoDeferredException();
      }
    }
  }
  
  public void provideDeferred(Deferred c){
    deferred = c;
  }

  @Override
  public void collide(MainMenuObstacleBall other) {
    float rx = currentX - other.getX();
    float ry = currentY - other.getY();
    float r = (float)Math.sqrt(rx*rx + ry*ry);
    float v1 = (float)Math.sqrt(Math.pow(other.getVX(), 2)+Math.pow(other.getVY(), 2));
    float v2 = (float)Math.sqrt(currentVX*currentVX+currentVY*currentVY);
    float dvx = (2/r)*rx*(other.getMass()/(mass+other.getMass()))*(v1+v2);
    float dvy = (2/r)*ry*(other.getMass()/(mass+other.getMass()))*(v1+v2);
    
    currentVX += dvx;
    currentVY += dvy;
  }

  @Override
  public boolean affectedBy(MainMenuObstacleBall other) {
    float x0 = currentX;
    float y0 = currentY;
    float r0 = radius;
    
    float x1 = other.getX();
    float y1 = other.getY();
    float r1 = other.getRadius();
    
    boolean result;
    float dist =(float)Math.sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1));
    if(dist < r0 + r1){
      result = true;
    }else{
      result = false;
    }
    
    if(result){
      if(ignoredForCollision.contains(other)){
        return false;
      }else{
        ignoredForCollision.add(other);
        return true;
      }
    }else{
      ignoredForCollision.remove(other);
      return false;
    }
  }
}




Java Source Code List

com.karolmajta.procprox.DragDetector.java
com.karolmajta.procprox.Drag.java
com.karolmajta.procprox.FontManager.java
com.karolmajta.procprox.IEventFilter.java
com.karolmajta.procprox.TapDetector.java
com.karolmajta.procprox.Tap.java
com.karolmajta.procprox.excepiton.FontNotCreatedException.java
com.karolmajta.stp.LoadingScreenActivity.java
com.karolmajta.stp.MainMenuActivity.java
com.karolmajta.stp.exception.NoDeferredException.java
com.karolmajta.stp.exception.NoTasksInProgressQueueException.java
com.karolmajta.stp.exception.STPException.java
com.karolmajta.stp.exception.UnboundViewException.java
com.karolmajta.stp.models.ICanCollide.java
com.karolmajta.stp.models.IPConstants.java
com.karolmajta.stp.models.IProgress.java
com.karolmajta.stp.models.ITask.java
com.karolmajta.stp.models.ITickable.java
com.karolmajta.stp.models.MainMenuItemBall.java
com.karolmajta.stp.models.MainMenuObstacleBall.java
com.karolmajta.stp.models.ObstacleManager.java
com.karolmajta.stp.models.SyncProgress.java
com.karolmajta.stp.models.SyncTask.java
com.karolmajta.stp.models.Tickable.java
com.karolmajta.stp.models.Viewport.java
com.karolmajta.stp.views.FancyTextView.java
com.karolmajta.stp.views.IDrawable.java
com.karolmajta.stp.views.MainMenuItemBallDebugView.java
com.karolmajta.stp.views.MainMenuItemBallView.java
com.karolmajta.stp.views.MainMenuObstacleBallDebugView.java
com.karolmajta.stp.views.MainMenuObstacleBallView.java
com.karolmajta.stp.views.ObstacleManagerView.java
com.karolmajta.stp.views.ProgressDebugView.java
com.karolmajta.stp.views.ProgressView.java
com.karolmajta.stp.views.View.java