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 13 of 13

Thread: collision detection moving objects

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default collision detection moving objects

    I have the 2d 2 state game set up. menu and play are my states.

    the play state has a guy who can walk around a map. I have a .png of a pig I would like to add as an enemy and I want to give my character the ability to shoot the enemy. if he runs in to the enemy I want him to die.

    1. what is the best way to implement my enemy, considering it should come on the screen randomly and as the players score increases the enemy comes more frequently and moves faster.

    2. how to give character the ability to shoot.

    3 an explanation of adding collision detection on moving objects as well as a radius around an area on the map such as a tree or a building.

    this is what i have so far.

      package javagame;
     
    import org.lwjgl.input.Mouse;
    import org.newdawn.slick.*;
    import org.newdawn.slick.state.*;
     
    public class Play extends BasicGameState{
     
    	Animation bucky, movingUp, movingDown, movingLeft, movingRight;
    	Image worldMap;
    	boolean quit = false;
    	int [] duration = {200,200};
    	float buckyPositionX = 0;
    	float buckyPositionY = 0;
    	float shiftX = buckyPositionX + 360;
    	float shiftY = buckyPositionY + 160;
     
    	public Play(int state){
     
    	}
     
    	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
    		worldMap = new Image("res/world.png");
    		Image[] walkUp = {new Image("res/buckysBack.png"), new Image("res/buckysBack.png")};
    		Image[] walkDown = {new Image("res/buckysFront.png"), new Image("res/buckysFront.png")};
    		Image[] walkLeft = {new Image("res/buckysLeft.png"), new Image("res/buckysLeft.png")};
    		Image[] walkRight = {new Image("res/buckysRight.png"), new Image("res/buckysRight.png")};	
     
    		movingUp = new Animation(walkUp, duration, false);
    		movingDown = new Animation(walkDown, duration, false);
    		movingLeft = new Animation(walkLeft, duration, false);
    		movingRight = new Animation(walkRight, duration, false);
    		bucky = movingDown;
    	}
     
    	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
    	worldMap.draw(buckyPositionX, buckyPositionY);
    	bucky.draw(shiftX, shiftY);
    	g.drawString("Buckys X: " + buckyPositionX +"\nBuckys Y: " +buckyPositionY, 500, 50);
     
    	if(quit==true){
    		g.drawString("Resume (R)", 250, 100);
    		g.drawString("Main Menu (M)", 250, 150);
    		g.drawString("Quit (Q)", 250, 200);
    		if(quit==false){
    			g.clear();
    		}
    	}
    	}
     
    	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
    		Input input = gc.getInput();
     
    		//Up
     
    		if(input.isKeyDown(Input.KEY_UP)){
    			bucky = movingUp;
    			buckyPositionY += delta * .1f;
    			if(buckyPositionY>161){
    				buckyPositionY -= delta * .1f;
    			}
    		}
     
    		//Down
    		if(input.isKeyDown(Input.KEY_DOWN)){
    			bucky = movingDown;
    			buckyPositionY -= delta * .1f;
    		if(buckyPositionY<-598){
    			buckyPositionY += delta * .1f;
    			}
    		}
     
    		//Left
    		if(input.isKeyDown(Input.KEY_LEFT)){
    			bucky = movingLeft;
    			buckyPositionX += delta * .1f;
    			if(buckyPositionX>358){
    				buckyPositionX -= delta * .1f;
    			}
    		}
     
    		//Right
    		if(input.isKeyDown(Input.KEY_RIGHT)){
    			bucky = movingRight;
    			buckyPositionX -= delta * .1f;
    		if(buckyPositionX<-796){
    			buckyPositionX += delta * .1f;
    			}
    		}
     
    		//Up
    		if(input.isKeyDown(Input.KEY_W)){
    			bucky = movingUp;
    			buckyPositionY += delta * .1f;
    		if(buckyPositionY>161){
    			buckyPositionY -= delta * .1f;
    			}
    		}
    		//Down
    		if(input.isKeyDown(Input.KEY_S)){
    			bucky = movingDown;
    			buckyPositionY -= delta * .1f;
    		if(buckyPositionY<-598){
    			buckyPositionY += delta * .1f;
    			}
    		}
    		//Left
    		if(input.isKeyDown(Input.KEY_A)){
    			bucky = movingLeft;
    			buckyPositionX += delta * .1f;
    			if(buckyPositionX>358){
    				buckyPositionX -= delta * .1f;
    			}
    		}
     
    		//Right
    		if(input.isKeyDown(Input.KEY_D)){
    			bucky = movingRight;
    			buckyPositionX -= delta * .1f;
    			if(buckyPositionX<-796){
    				buckyPositionX += delta * .1f;
     
    			}
    	}
    		//escape
    		if(input.isKeyDown(Input.KEY_ESCAPE)){
    			quit = true;
    		}
    		//quit menu
    		if(quit==true){
    			if(input.isKeyDown(Input.KEY_R)){
    				quit = false;		
    			}
    			if(input.isKeyDown(Input.KEY_M)){
    				sbg.enterState(0);
    		}
    			if(input.isKeyDown(Input.KEY_Q)){
    				System.exit(0);
    			}
    		}
    	}
    	public int getID(){
    		return 1;
    	}
    }


  2. #2
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: collision detection moving objects

    i see you are doing bucky tutorial i did the same...not sure how to do the enemy but for collision detection on the trees and such you can do the same as the sides of the map

  3. The Following User Says Thank You to derekxec For This Useful Post:

    nichodiaz (June 13th, 2013)

  4. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: collision detection moving objects

    Awesome you can relate! thanks for your response. I was thinking that but wouldn't it need to be a radius around the tree? or would each tree have 4 collision points? 1 for each side of the tree or building????

  5. #4
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: collision detection moving objects

    im not soooo good at it so the 4 points is the only way i know of but maybe or hopefully someone answers how to do it a better way because i would love to know too

  6. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: collision detection moving objects

    so are you stuck at the same point as me? and how is this forum? I am sick of being stuck all the time... lol I need to find a forum will people actually help. its actually because of this recent project ive been making a point to answer questions on forums cause it drives me crazy that its so hard to get people to answer my questions

    The worst thing is that the tutorial we both followed stopped where it did... I mean why not finish the game and teach us everything? now we both are skilled enough to build a game half way... yay for us being half way useful java developers lol.

  7. #6
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: collision detection moving objects

    hahahaha yeap stuck at same spot...he said he was supposed to finish it but i guess he never got to it lol

    this forum is great they dont usually give you the answer outright but they do help you to learn it and point you in a nice direction to get it done...you should stick around i have learned a ton here

  8. #7
    Junior Member
    Join Date
    Jun 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: collision detection moving objects

    awesome thanks for your input. I like java a lot I wish I would of started with programming instead of websites. I will let you know when I figure the solution out. and I hope you will do the same for me.

  9. #8
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: collision detection moving objects

    If you want to learn about collition, look into the Rectangle clas. And for more gameprograming tutorials look at:
    The Cherno - YouTube
    He has some really good 2D game programming tutorials.

  10. #9
    Junior Member
    Join Date
    Jun 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: collision detection moving objects

    awesome thank you!

    --- Update ---

    question... for learning java is making games a good way to learn? or is making other programs better for learning in order to become professional and be able to build anything. I want to learn java to the point I can build anything off the top of my head. will sticking to game tutorials limit me or make it easier?

  11. #10
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: collision detection moving objects

    If you are already started with java its better to stick with it. Otherwise you might get confused between diffrent langauges. Java is easier to make games in then C++, C++ is really hard to learn.
    For learning stuff, there are certain tricks for certain things(entity for example). But once you learned those and you're a bit creative you can make litearly games out of your head.
    I make random short games in computer science classes atm(I have finised all my work and i am programming why more advanced then the rest). And these games are just random ideas or copies of other games.

  12. #11
    Junior Member
    Join Date
    Jun 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: collision detection moving objects

    currently I am switching back and forth between objective C and Java. What I meant was is programming games good enough practice to teach me enough about Java to build anything such as understanding classes and ojects. Or am I going to need to see some tutorials on building other types of applications as well in order to really get a full grasp of Java?

  13. #12
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: collision detection moving objects

    I think so but searching up tutorials is allways good

  14. #13
    Junior Member
    Join Date
    Jun 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: collision detection moving objects

    cool thanks for your input

Similar Threads

  1. Trouble with Collision Detection
    By Gravity Games in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 6th, 2012, 03:00 PM
  2. Collision Detection Between Two Squares
    By thegreatzo in forum Loops & Control Statements
    Replies: 7
    Last Post: August 22nd, 2012, 10:13 AM
  3. AI, Collision Detection, and Timing
    By Staticity in forum Java Theory & Questions
    Replies: 0
    Last Post: March 20th, 2012, 03:12 PM
  4. collision detection not working...
    By skberger21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2011, 09:02 PM
  5. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 11:39 AM

Tags for this Thread