1 Attachment(s)
I have problems with my 2d car game.
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.
Code Java:
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;
}
}
Re: I have problems with my 2d car game.
Please post the code with code tags so we can read it.
Re: I have problems with my 2d car game.
Quote:
Originally Posted by
Norm
Please post the code with code tags so we can read it.
I have posted the code for you this time. Please post the code within the highlight tags in future.
We are going to need to see all of your code to work out what the problem is.
Re: I have problems with my 2d car game.
Can you make a simple driver/test program that would call your method and demonstrate the problem?