Hello everyone, I am new here and i want some help. I've started 2d java game but here is my problem. When the car hits the one of the wall the collision/reflection of the wall is okay, BUT sometimes the angle is impossible. Let's say the car hits the wall at 60 degrees sometimes not bounce but it's return the way back . I use rectangle for walls.
I attach txt file.
package cargame; import java.awt.geom.Point2D; public class InnerBounceOffWallsBehaviour implements Behaviour { protected float x1; protected float y1; protected float x2; protected float y2; public InnerBounceOffWallsBehaviour(float x1, float y1, float x2, float y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; } public void update(Vehicle v, float dt) { Point2D.Float p = v.getPosition(); Point2D.Float vel = v.getVelocity(); if (p.x >= x1 && p.x <= x2 && p.y < y2 && p.y > y1) { if (p.x >= x1) { p.x = p.x; vel.x = -vel.x; } else if (p.x <= x2) { p.x = p.x; vel.x = -vel.x; } if (p.y >= y1) { p.y = p.y; vel.y = -vel.y; } else if (p.y <= y2) { p.y = p.y; vel.y = -vel.y; } }