Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 18th, 2012, 12:41 PM   #1
Hornetusaf
Newbie
 
Join Date: Aug 2011
Posts: 12
Rep Power: 0 Hornetusaf is on a distinguished road
j2me canvas collision detection

hello i am developing a game that is like the movie tron where two bikes run around leaving a wall behind and you can´t crash with your own or the enemy´s wall so im having problems implementing a good collision detection system since the bikes are just canvas fill rectangle leaving a trail behind. so my question is how should i go about implementing this? is there a rectangle class/object that i can use in j2me since java has it and it contains and intersection method? or check if the color or the screen at certain spots is not black like the color of the background of the game? if so how do i get the color of a certain spot in the screen? here is the code of the class that implements the movement of the screen the r canvas variable is for the red bike and the variable for the blue bike is called a but this one is going to be computer controled so rigth now it just moves in one direction. the red bike is keyboard controled. please help.

public class animacion extends GameCanvas implements Runnable, CommandListener{
    HelloMIDlet hellomidlet;
    Graphics a,r,f;
    Thread subproceso; 
    
    public animacion(HelloMIDlet hm)
    {
        super(true);
        hellomidlet=hm;
        setCommandListener(this);
        addCommand(new Command("Salir",Command.OK,1));
        a = getGraphics();
        r = getGraphics();
        f = getGraphics();
        subproceso = new Thread(this);
        subproceso.start();
    }
    
    public void run()
    {
        int alto = getHeight();
        int ancho = getWidth();
        
        int posrX = 200;
        int posrY = alto/2;
        int velrX = -3;
        int velrY = 0;
        
        int posaX = 40;
        int posaY = alto/2;
        int velaX = 3;
        int velaY = 0;        
        
        int valortecla;
        
        f.setColor(0,0,0);
        f.fillRect(0, 0, ancho, alto+100);// black background
        
        while(true)
        {              
            r.setColor(255,0,0);
            r.fillRect(posrX, posrY, 3, 3);//red bike
            
            a.setColor(0,0,255);
            a.fillRect(posaX, posaY, 3, 3);//blue bike
            
            try
            {
                subproceso.sleep(50);
            }
            catch(InterruptedException e)
            {
            
            }
            
            valortecla=getKeyStates();
            
            if((valortecla & UP_PRESSED)!=0 && velrY!=3)
            {
                velrY=-3;
                velrX=0;
            }
            if((valortecla & DOWN_PRESSED)!=0 && velrY!=-3)
            {
                velrY=3;
                velrX=0;
            }
            if((valortecla & LEFT_PRESSED)!=0 && velrX!=3)
            {
                velrY=0;
                velrX=-3;
            }
            if((valortecla & RIGHT_PRESSED)!=0 && velrX!=-3)
            {
                velrY=0;
                velrX=3;
            }      
            
            posrX+= velrX;
            posrY+= velrY;
            
            posaX+= velaX;
            posaY+= velaY;
           
            flushGraphics();
        }        
    }
    
    public void commandAction(Command cmd, Displayable displayable)
    {
        if(cmd.getCommandType()== Command.OK)
        {        
            subproceso.interrupt();
            Display.getDisplay(hellomidlet).setCurrent(hellomidlet.getList());             
        }
    }
Hornetusaf is offline   Reply With Quote
Old Apr 18th, 2012, 5:35 PM   #2
Troncoso
Hobbyist Programmer
 
Join Date: Oct 2011
Posts: 127
Rep Power: 3 Troncoso is on a distinguished road
Re: j2me canvas collision detection

If you use the Rectangle class, it has a method called intersects(Rectangle other). It returns true when its boundaries intersect with the rectangle that you specify in the argument.
Troncoso is offline   Reply With Quote
Old Apr 18th, 2012, 6:09 PM   #3
Hornetusaf
Newbie
 
Join Date: Aug 2011
Posts: 12
Rep Power: 0 Hornetusaf is on a distinguished road
Re: j2me canvas collision detection

is there a rectangle class in j2me?
Hornetusaf is offline   Reply With Quote
Old Apr 18th, 2012, 6:45 PM   #4
Troncoso
Hobbyist Programmer
 
Join Date: Oct 2011
Posts: 127
Rep Power: 3 Troncoso is on a distinguished road
Re: j2me canvas collision detection

Yes, you can find the API documentation here:

http://j2me-docs.narod.ru/api/lwuit/...Rectangle.html
Troncoso is offline   Reply With Quote
Old Apr 18th, 2012, 7:12 PM   #5
Hornetusaf
Newbie
 
Join Date: Aug 2011
Posts: 12
Rep Power: 0 Hornetusaf is on a distinguished road
Re: j2me canvas collision detection

somehow i cannot find it under my netbeans imports do i have to download it from somewhere
Hornetusaf is offline   Reply With Quote
Old Apr 18th, 2012, 7:19 PM   #6
Troncoso
Hobbyist Programmer
 
Join Date: Oct 2011
Posts: 127
Rep Power: 3 Troncoso is on a distinguished road
Re: j2me canvas collision detection

It doesn't work when you import import com.sun.lwuit.geom.Rectangle?
Troncoso is offline   Reply With Quote
Old Apr 18th, 2012, 7:29 PM   #7
Hornetusaf
Newbie
 
Join Date: Aug 2011
Posts: 12
Rep Power: 0 Hornetusaf is on a distinguished road
Re: j2me canvas collision detection

no package does not exist
Hornetusaf is offline   Reply With Quote
Old Apr 18th, 2012, 7:36 PM   #8
Troncoso
Hobbyist Programmer
 
Join Date: Oct 2011
Posts: 127
Rep Power: 3 Troncoso is on a distinguished road
Re: j2me canvas collision detection

http://www.oracle.com/technetwork/ja...uit/index.html

Download that package. I unzipped and checked myself. Rectangle is in there
Troncoso is offline   Reply With Quote
Old Apr 18th, 2012, 7:39 PM   #9
Hornetusaf
Newbie
 
Join Date: Aug 2011
Posts: 12
Rep Power: 0 Hornetusaf is on a distinguished road
Re: j2me canvas collision detection

cool man thanks for your support that intersects method its going to make it much easier to detect collisions thanks a lot.
Hornetusaf is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bitmap Collision Detection with Allegro 5? PaCkEtPiRaTe C++ 0 Apr 21st, 2011 1:15 AM
DirectX Collision Detection theflamer14 C# 2 Jun 16th, 2008 4:59 PM
collision detection bconnor C 1 May 26th, 2006 10:13 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:42 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC