Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: I have problems with my 2d car game.

1. ## 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.

```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;
}
}```

2. ## Re: I have problems with my 2d car game.

Please post the code with code tags so we can read it.

3. ## Re: I have problems with my 2d car game.

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.

4. ## 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?