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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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;
    			}
    		}
    Attached Files Attached Files
    Last edited by JavaPF; May 25th, 2011 at 10:51 AM. Reason: Added highlight tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

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

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

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

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

    Quote Originally Posted by Norm View Post
    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.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default 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?

Similar Threads

  1. 2 problems...
    By Day2Day in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 22nd, 2010, 01:51 PM
  2. [SOLVED] Have a few odd problems.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 18
    Last Post: October 19th, 2010, 06:08 PM
  3. rmi problems
    By deepthought in forum Java SE APIs
    Replies: 7
    Last Post: September 7th, 2010, 07:25 PM
  4. How to compile and run java program?
    By ebalari56 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2009, 09:33 PM
  5. If you have any .NET problems
    By antony_t in forum The Cafe
    Replies: 1
    Last Post: August 26th, 2009, 10:49 AM