How to cap speed of a moving object

Started by
3 comments, last by Aardvajk 10 years ago

//package bouncingBoxes;

import java.awt.Point;
import java.awt.Rectangle;

public class MyBox 
{
	private Rectangle box;
	private int dx;
	private int dy;
	private int speed = 1;
	
	
	
	
	private static final int BOX_WIDTH = 20;
	private static final int BOX_HEIGHT = 30;
	// constructor
	public MyBox(Rectangle box, int dx, int dy)
	{
		this.box = box;
		this.dx = dx;
		this.dy = dy;
	}
	// sets the vector
	public void setVector(int dx, int dy)
	{
		this.dx = dx;
		this.dy = dy;
	}
	// move the box
	public void move()
	{
		box.translate(dx, dy);
	}
	// moves the box just by the x coordinate
	public void moveX(int x)
	{
		dx = x;
		box.translate(dx, dy);
	}
	
	public void moveY(int y)
	{
		dy = y;
		box.translate(dx, dy);
	}
	
	public Rectangle getBox()
	{
		return box;
	}
	
	// gets the Y coordinate
	public int getDY()
	{
		return dy;
	}
	
	// gets the X coordinate
	public int getDX()
	{
		return dx;
	}

	public Point getNextLocation() 
	{
		//next x position
		int x = (int)box.getX() + dx;
		int y = (int)box.getY() + dy;

		//return the next location
	
		return  new Point(x,y);
	}

	public void bounceY() 
	{
		//swap direction in the y direction
		
		
		dy *= -speed;
		speed++;
		
	
	}

	public void bounceX() 
	{
		// swap direction in the x direction
		dx *= -speed;
		speed++;
	
	}
	

//package bouncingBoxes;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.JComponent;

/**
   This component displays a rectangle that can be moved. 
 */
public class RectangleComponent extends JComponent
{  
	private static final int BOX_WIDTH = 20;
	private static final int BOX_HEIGHT = 30;


	private ArrayList<MyBox> boxes;


	public RectangleComponent()
	{  
		boxes = new ArrayList<MyBox>();
	}

	public void paintComponent(Graphics g)
	{  
		Graphics2D g2 = (Graphics2D) g;

		for(MyBox box: boxes)
		{
			g2.draw( box.getBox() );
		}
	}

	public void addRectangle(int x, int y, int dx, int dy)
	{
		Rectangle rect = new Rectangle(x, y, BOX_WIDTH, BOX_HEIGHT); 

		MyBox box = new MyBox(rect, dx, dy);
		boxes.add(box);

		repaint();
	}


	public void addRectangle(int x, int y)
	{
		Rectangle rect = new Rectangle(x, y, BOX_WIDTH, BOX_HEIGHT); 

		MyBox box = new MyBox(rect, 4, 4);
		boxes.add(box);

		repaint();
	}

	public void updateLocations()
	{
		for(MyBox box: boxes)
		{
			//get bottom after future move
			Point p = box.getNextLocation();
			Rectangle r = box.getBox();


			//if bottom of box will hit bottom of screen ...
			//    or hits off of top
			if( (p.getY() + r.height > this.getHeight() )  ||
					(p.getY() <=0) )
			{
				//switch direction
				box.bounceY();
				
			}
			if( (p.getX() + r.width > this.getWidth())   ||
					(p.getX() <=0) )
			{
				//switch direction
				box.bounceX();
			}

			box.move();
		}

		repaint();
	}

	
	/*
	 * Did not do what I had hoped
	 * keep it here in event that I need it later
	 * public void outOfBounds()
	{
		for(MyBox box: boxes)
		{

			if(box.getDX() > this.getWidth())
			{
				int x = box.getDX() - this.getWidth();
				box.moveX(x);
			}
			
			if(box.getDX() < 0)
			{
				int x = box.getDX();
				box.moveX(-x);
			}
			
			if(box.getDY() > this.getHeight())
			{
				int y = box.getDY() - this.getHeight();
				box.moveY(y);
			}
			
			if(box.getDY() < 0)
			{
				int y = box.getDY();
				box.moveY(-y);
			}
		}
		
	}
	*/
}



I'm trying to get this to work so that the rectangle will bounce off the walls, increasing speed as it bounces, however it will obviously increase so quickly that it will bounce out of the screen eventually. I want to find out when it goes out of bounds, then find the speed it was at when it went out of bounds, and then remove THAT speed by 1, without incrementing speed after it goes out of bounds, allowing it to stay fast, without causing the out of bounds speed again. I'm kinda burned out from work and classes, and can't think. Anything anyone can do to help, even a bump in the right direction while I continue tot google search would be wonderful. Thank you.

Advertisement

Perhaps a simple

if(speed > limit)

speed = limit;

?

Caveat: I have not read your code.

But isn't the direct translation of your problem to pseudo code:

if(IsOutOfScreen(Box))

{

var maxSpeed = Box.currentSpeed;

maxSpeed--;

print("The Box should not go faster than ", maxSpeed);

}

And then you hardcode this constant in your code. Am I missing something?

Anyway, maxSpeed-- might not be the correct way to do this. Also note that your speed in not a physical speed, but is coupled to the update rate.

I might just be implementing this in the wrong areas then, because I've tried the solution from Vortez, and something similar to theslimdes method as well, and neither worked. So maybe it's just where I tried to get them to work.

A general solution is to convert your proposed movement to a vector and check its length. If it is greater than the max speed, normalise it then multiply it by the max speed. This is your new movement vector.

This keeps the speed consistent regardless of the direction, horizontal, diagonal etc. Better to use floats to represent position and velocity and convert to int when you draw.

This topic is closed to new replies.

Advertisement