Collision detection, rectangle intersects at corner - java

I'm trying to learn how collision detection works in android and I am doing so using Android rectangles.
I need to be able to detect a collision of the second rectangles top, left and bottom walls but the issue is that the first rectangle is able to go straight in to the second rectangle through the top left corner.
Below is a snippet of the code that I am using:
Snippet from GameView2 class:
private Shape s1 = new Shape(this, Color.BLACK, 0, 100, 50, 50);
private Shape s2 = new Shape(this, Color.BLUE, 200, 100, 50, 50);
#Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
this.drawBackground(canvas);
s1.onDraw(canvas);
s2.onDraw(canvas);
s1.x+=s1.vx;
s1.y+=s1.vy;
//left wall collision
if(s1.x+s1.width==s2.x && s1.y+s1.height > s2.y && s1.y < s2.y + s2.height){
s1.vx = 0;
}
//top wall
else if(s1.y+s1.height==s2.y && s1.x+s1.width > s2.x && s1.x < s2.x+s2.width){
s1.vy = 0;
}
else{
s1.vx = 2;
}
}
Shape class:
public class Shape{
protected GameView2 game_view;
protected int x;
protected int y;
protected int vx;
protected int vy;
protected int width;
protected int height;
protected int color;
public Shape(GameView2 game_view, int color, int x, int y, int width, int height) {
this.game_view = game_view;
this.x = x;
this.y = y;
this.vx = 2;
this.vy = 0;
this.width = width;
this.height = height;
this.color = color;
}
public Rect getRect(){
return new Rect(x, y, x + width, y + height);
}
public void onDraw(Canvas canvas){
Paint paint = new Paint();
Rect rec = new Rect(x, y, x + width, y + height);
paint.setColor(color);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(rec, paint);
}
}

Here is a really simple algorithm for collision between two rectangles.
x1+width1 < x2 || x2+width2 < x1 || y1+height1 < y2 || y2+height2
If the statement above is true, no collision occurs. Else, the two rectangles are colliding.

Related

Is there a way to make a constructor that draws a rectangle

My partner and I are trying to remake Tetris for our final project of the year in my Computer Science class we currently have a for loop that draws individual rectangles in an overwritten paint method.
private final int spacer = 30;
public int getSpacer()
{
return spacer;
}
public void paint(Graphics g) {
setBackground(Color.GRAY);
for(int i = getHeight()/2 - (spacer * 10); i < getHeight()/2 + (spacer * 10); i += spacer) {
for(int x = getWidth()/2 - (spacer * 5); x < getWidth()/2 + (spacer * 5); x += (spacer)) {
g.drawRect(x, i, (spacer), (spacer));
}
}
setForeground(Color.black);
}
The method basically takes the width and height of the window and makes a 10 x 20 grid of boxes that are 30 units, pixels I think, wide.
We'd like to make a Grid.java class that takes in color, the spacer int, and an x and y int. The constructor for Grid.java should draw the exact same thing as the code above using the for loop, but when we tried it gave us a white screen that would not resize with the window.
private final int spacer = 30;
private static Grid[][] arr = new Grid[10][20];
public int getSpacer()
{
return spacer;
}
public void paint(Graphics g) {
setBackground(Color.GRAY);
int countY = 0;
int countX = 0;
for(int y = getHeight()/2 - (spacer * 10); y < getHeight()/2 + (spacer * 10); y += spacer) {
for(int x = getWidth()/2 - (spacer * 5); x < getWidth()/2 + (spacer * 5); x += spacer) {
arr[countX][countY] = new Grid(x, y, spacer, g);
countX++;
}
countY++;
}
setForeground(Color.black);
}
*Grid.java Class*
package Tetris_Shapes;
import javax.swing.*;
import java.awt.*;
public class Grid {
private int x;
private int y;
private int side;
private Graphics g;
public Grid(int x, int y, int side, Graphics g) {
// g.drawRect(x, y, spacer, spacer);
this.x = x;
this.y = y;
this.side = side;
this.g = g;
paint(this.g);
}
private void paint(Graphics g) {
g.drawRect(x, y, side, side);
}
}
When we try and run this we get the white box that doesn't resize. My question is does anyone know of a way to get a constructor to draw shapes. Thank you in advance, this is pretty niche so I'm also going to apologize in advance.

Libgdx - Parallax background set horizontally, can't implement scrolling up and down

I found a tutorial online to create a horizontally scrolling parallax background with this custom class:
public class ParallaxBackground extends Actor {
private float scroll;
private Array<Texture> layers;
private final int LAYER_SPEED_DIFFERENCE = 2;
float x, y, width, height, scaleX, scaleY;
float originX, originY, rotation;
float srcX, srcY;
boolean flipX, flipY;
private float speed;
public ParallaxBackground(Array<Texture> textures, HeroKnight player) {
layers = textures;
for(int i = 0; i <textures.size;i++){
layers.get(i).setWrap(Texture.TextureWrap.MirroredRepeat, Texture.TextureWrap.MirroredRepeat);
}
scroll = 0;
speed = 0;
x = y = originX = originY = rotation = 0;
srcY = 0;
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
scaleX = scaleY = 1;
flipX = flipY = false;
}
public void setSpeed(float newSpeed){
this.speed = newSpeed;
}
public void update(float deltaTime) {
}
#Override
public void draw(Batch batch, float parentAlpha) {
batch.setColor(getColor().r, getColor().g, getColor().b, getColor().a * parentAlpha);
scroll+=speed;
srcX = scroll;
batch.draw(layers.get(0), x, y, originX, originY, width, height, scaleX, scaleY, rotation, (int)srcX, (int) srcY, layers.get(0).getWidth(), layers.get(0).getHeight(), flipX, flipY);
for(int i = 1;i<layers.size;i++) {
srcX = scroll + i*this.LAYER_SPEED_DIFFERENCE *scroll;
batch.draw(layers.get(i), x, y, originX, originY, width, height,scaleX,scaleY,rotation,(int)srcX,(int)srcY,layers.get(i).getWidth(),layers.get(i).getHeight(),flipX,flipY);
}
}
.
backgroundViewport = new ScreenViewport();
stage = new Stage(backgroundViewport);
Array<Texture> textures = new Array<Texture>();
for(int i = 1; i <= 5;i++){
textures.add(new Texture(Gdx.files.internal("images/"+ i+".png")));
textures.get(textures.size-1).setWrap(Texture.TextureWrap.MirroredRepeat, Texture.TextureWrap.MirroredRepeat);
}
parallaxBackground = new ParallaxBackground(textures, player);
parallaxBackground.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
parallaxBackground.setSpeed(0);
stage.addActor(parallaxBackground);
In the render method:
stage.act();
stage.draw();
Eveything works well when you are moving left to right. However, on my background image there is water down, so logically I want the screen to show it when the player is on the lower platforms, and make it scroll higher so that the water disappears when you go up.
So the question is: how do I make the blue part visible when the player is lower, and scroll it to the red part when you go higher?

Shapes drawn at obscure positions

this is a really weird bug and I can't seem to figure it out. To simplify this, I have three classes: A Canvas class (which draws the object with Graphics2D.draw()) then lots ob furniture classes, which can be viewed as one because all they do is return different shapes. Last I have a CustomShape class, which lets me create a new Shape based on my existing other shapes. But the shapes are being drawn at weird places. The x any y coordinates do not match where the shape is being drawn.
Closet.java:
public class Closet {
double x, y, width, height, rotation;
Color color;
Closet() {
this.x = X;
this.y = Y;
this.width = 40;
this.height = 40;
this.rotation = 0;
this.color = Color.blue;
}
public Shape getShape() {
GeneralPath closetShape = new GeneralPath();
closetShape.append(new Rectangle2D.Double(0, 0, width, height), false);
closetShape.moveTo(0 , 0);
closetShape.lineTo(width, height);
closetShape.moveTo(0, height);
closetShape.lineTo(width, 0);
// transform:
AffineTransform t = new AffineTransform();
t.translate(x, y);
Rectangle2D umriss = closetShape.getBounds2D();
t.rotate(Math.toRadians(rotation),umriss.getX()+umriss.getWidth()/2,umriss.getY()+umriss.getHeight()/2);
return t.createTransformedShape(closetShape);
}
}
CustomShape.java
public class CustomShape {
double x, y, width, height, rotation;
Color color;
private Closet[] c;
CustomShape(Closet... elements) {
this.m = elements;
GeneralPath path = new GeneralPath();
Closet[] newC = elements.clone();
Arrays.stream(newC).forEach(e -> path.append(e.getShape(), false));
this.x = path.getBounds2D().getX();
this.y = path.getBounds2D().getY();
this.width = path.getBounds2D().getWidth();
this.height = path.getBounds2D().getHeight();
this.rotation = 0;
this.color = Color.blue;
}
public Shape getShape() {
GeneralPath path = new GeneralPath();
Arrays.stream(c).forEach(e -> path.append(e.getShape(), false));
AffineTransform t = new AffineTransform();
t.translate(x, y);
Rectangle2D umriss = path.getBounds2D();
t.rotate(Math.toRadians(rotation),umriss.getX()+umriss.getWidth()/2,umriss.getY()+umriss.getHeight()/2);
return t.createTransformedShape(path);
}
}
I found the solution: I had transformed these Shapes before adding them to the CustomShape. That is why the x and y coordinates were wrong.

Java - How to move a shape

So far I am just making a new Shape to "move" it. But is there a better way to change the position of a Shape in Java?
// Collision Shape
protected RectangularShape col;
public GameObject() {
col = new Ellipse2D.Double(x, y, ObjectSpriteManager.SIZE, ObjectSpriteManager.SIZE);
}
public void tick() {
x += velX;
y += velY;
col = new Ellipse2D.Double(x, y, ObjectSpriteManager.SIZE, ObjectSpriteManager.SIZE);
}
See the setFrame() methods of RectangulerShape. For example:
col.setFrame(x, y, width, height);

Having trouble displaying balls in the right position using Java and vector objects

I'm having issues with the balls that I display in my JFrame window. Here's the main idea of my application:
Point object (with coordinates x and y) --> Vector object (a class I wrote that has the x and y components of a vector, as well as a few methods... gets its location and head/tail points from the Point object) --> Ball object (the position, velocity, and acceleration vectors are collected in an ArrayList, also has radius and color attributes) --> ContainerBox object (contains the balls, defines the min/max x and y for collision detection purposes).
I'm working toward having one ball centered in the window and the other orbiting the first, but right now I'm just trying to get my objects to play nicely. I'm very new to OOP principles and this is the first time I've written a program using classes in this way.
Everything works perfectly, the JFrame comes up and displays the balls... the problem is that the balls won't show up in the right places. No matter what I put in for the x and y coordinates of the balls (either explicitly or using the objects), they always show up in the upper left-hand corner of the screen. Here's a pic of what I get: Picture Here
I don't know whether it's the vectors or the collision detection or what... any ideas? Thanks a lot for reading and replying!
EDIT: Here's some of the code that I'm using (sorry to put so much, I have no idea where the problem is):
EDIT AGAIN: Added the ball class that I forgot.
package chaneyBouncingBall;
import java.util.*;
public class Point
{
float x;
float y;
public Point(float x, float y)
{
this.x = x;
this.y = y;
}
}
package chaneyBouncingBall;
import java.util.ArrayList;
public class Chaney2DVector
{
float x;
float y;
Point fromLocation;
public Chaney2DVector(float x,
float y)
{
this.x = x;
this.y = y;
}
public Chaney2DVector(Point point1,
Point point2)
{
fromLocation = new Point(point1.x, point1.y);
this.x = point2.x - point1.x;
this.y = point2.y - point1.y;
}
}
package chaneyBouncingBall;
import java.awt.*;
import java.util.*;
public class Ball
{
float x, y;
float velX, velY;
float accelX, accelY;
float radius;
private Color color;
public Ball(float x, float y, float velX,
float velY, float accelX,
float accelY, float radius,
Color color)
{
Chaney2DVector position = new Chaney2DVector(x, y);
Chaney2DVector velocity = new Chaney2DVector(velX, velY);
Chaney2DVector acceleration = new Chaney2DVector(accelX, accelY);
ArrayList posVelAcc = new ArrayList();
posVelAcc.add(position);
posVelAcc.add(velocity);
posVelAcc.add(acceleration);
this.radius = radius;
this.color = color;
}
public void draw(Graphics g)
{
g.setColor(color);
g.fillOval((int)(x - radius), (int)(y - radius),
(int)(2 * radius), (int)(2 * radius));
}
public void moveOneStepWithCollisionDetection( ContainerBox box)
{
float ballMinX = box.minX + radius;
float ballMinY = box.minY + radius;
float ballMaxX = box.maxX - radius;
float ballMaxY = box.maxY - radius;
x = x + velX;
y = y + velY;
if (x < ballMinX)
{
velX = -velX;
x = ballMinX;
}
else if (x > ballMaxX)
{
velX = -velX;
x = ballMaxX;
}
if (y < ballMinY)
{
velY = -velY;
y = ballMinY;
}
else if (y > ballMaxY)
{
velY = -velY;
y = ballMaxY;
}
}
}
package chaneyBouncingBall;
import java.awt.*;
/**
* A rectangular container box, containing the bouncing ball.
*/
public class ContainerBox {
int minX, maxX, minY, maxY; // Box's bounds (package access)
private Color colorFilled; // Box's filled color (background)
private Color colorBorder; // Box's border color
private static final Color DEFAULT_COLOR_FILLED = Color.BLACK;
private static final Color DEFAULT_COLOR_BORDER = Color.YELLOW;
/** Constructors */
public ContainerBox(int x, int y, int width, int height, Color colorFilled, Color colorBorder) {
minX = x;
minY = y;
maxX = x + width - 1;
maxY = y + height - 1;
this.colorFilled = colorFilled;
this.colorBorder = colorBorder;
}
/** Constructor with the default color */
public ContainerBox(int x, int y, int width, int height) {
this(x, y, width, height, DEFAULT_COLOR_FILLED, DEFAULT_COLOR_BORDER);
}
/** Set or reset the boundaries of the box. */
public void set(int x, int y, int width, int height) {
minX = x;
minY = y;
maxX = x + width - 1;
maxY = y + height - 1;
}
/** Draw itself using the given graphic context. */
public void draw(Graphics g) {
g.setColor(colorFilled);
g.fillRect(minX, minY, maxX - minX - 1, maxY - minY - 1);
g.setColor(colorBorder);
g.drawRect(minX, minY, maxX - minX - 1, maxY - minY - 1);
}
}
package chaneyBouncingBall;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
/**
* The control logic and main display panel for game.
*/
public class BallWorld extends JPanel {
private static final int UPDATE_RATE = 50; // Frames per second (fps)
private Ball ball; // A single bouncing Ball's instance
private Ball ball2;
private ContainerBox box; // The container rectangular box
private DrawCanvas canvas; // Custom canvas for drawing the box/ball
private int canvasWidth;
private int canvasHeight;
/**
* Constructor to create the UI components and init the game objects.
* Set the drawing canvas to fill the screen (given its width and height).
*
* #param width : screen width
* #param height : screen height
*/
public BallWorld(int width, int height) {
canvasWidth = width;
canvasHeight = height;
boolean stationary = true;
Random rand = new Random();
int angleInDegree = rand.nextInt(360);
int radius = 50;
int radius2 = 25;
// double accelAngle;
float x1 = rand.nextInt(canvasWidth - radius * 2 - 20) + radius + 10;
float y1 = rand.nextInt(canvasHeight - radius * 2 - 20) + radius + 10;
float x2 = rand.nextInt(canvasWidth - radius * 2 - 20) + radius + 10;
float y2 = rand.nextInt(canvasHeight - radius * 2 - 20) + radius + 10;
// float x = 100;
// float y = 100;
float velX1 = 0;
float velY1 = 0;
float accelX1 = 0;
float accelY1 = 0;
float velX2 = 0;
float velY2 = 0;
float accelX2 = 0;
float accelY2 = 0;
ball = new Ball(canvasWidth / 2, canvasHeight / 2, velX1, velY1, accelX1, accelY1,
radius, Color.BLUE);
ball2 = new Ball(x2, y2, velX2, velY2, accelX2, accelY2, radius / 5, Color.YELLOW);
// Init the Container Box to fill the screen
box = new ContainerBox(0, 0, canvasWidth, canvasHeight, Color.BLACK, Color.WHITE);
// Init the custom drawing panel for drawing the game
canvas = new DrawCanvas();
this.setLayout(new BorderLayout());
this.add(canvas, BorderLayout.CENTER);
// Handling window resize.
this.addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(ComponentEvent e) {
Component c = (Component)e.getSource();
Dimension dim = c.getSize();
canvasWidth = dim.width;
canvasHeight = dim.height;
// Adjust the bounds of the container to fill the window
box.set(0, 0, canvasWidth, canvasHeight);
}
});
// Start the ball bouncing
gameStart();
}
/** Start the ball bouncing. */
public void gameStart() {
// Run the game logic in its own thread.
Thread gameThread = new Thread() {
public void run() {
while (true) {
// Execute one time-step for the game
gameUpdate();
// Refresh the display
repaint();
// Delay and give other thread a chance
try {
Thread.sleep(1000 / UPDATE_RATE);
} catch (InterruptedException ex) {}
}
}
};
gameThread.start(); // Invoke GaemThread.run()
}
/**
* One game time-step.
* Update the game objects, with proper collision detection and response.
*/
public void gameUpdate() {
ball.moveOneStepWithCollisionDetection(box);
ball2.moveOneStepWithCollisionDetection(box);
}
/** The custom drawing panel for the bouncing ball (inner class). */
class DrawCanvas extends JPanel {
/** Custom drawing codes */
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g); // Paint background
// Draw the box and the ball
box.draw(g);
ball.draw(g);
ball2.draw(g);
// Display ball's information
g.setColor(Color.WHITE);
g.setFont(new Font("Courier New", Font.PLAIN, 12));
// g.drawString("Ball " + ball.toString(), 20, 30);
}
/** Called back to get the preferred size of the component. */
#Override
public Dimension getPreferredSize() {
return (new Dimension(canvasWidth, canvasHeight));
}
}
}
package chaneyBouncingBall;
import javax.swing.JFrame;
public class Main
{
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new JFrame("Matt Chaney's Gravity App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new BallWorld(550, 450));
frame.pack();
frame.setVisible(true);
}
});
}
}
In the constructor of your Ball class, you don't assign all the instance variables to their parameters, these need to be added.
public Ball(float x, float y, float velX, float velY, float accelX, float accelY, float radius, Color color) {
...
this.x = x;
this.y = y;
this.velX = velX;
this.velY = velY;
this.accelX = accelX;
this.accelY = accelY;
}

Categories

Resources