Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Line/Point Collsion?  (Read 610 times)
0 Members and 1 Guest are viewing this topic.
Offline yoshidude

Junior Newbie




Java games rock!


« Posted 2005-05-29 15:41:29 »

I am working on a game that's similar to Missile Command (in fact, it was supposed to be Missile Command, but I got stuck in a loop that wouldn't let me do anything else until the missile was done being drawn, and I'd have to re-do the whole game to fix that and I don't have time. If anyone wants to help me with that instead, that'd be awesome  Tongue). Essentially, you click and a line appears, and everything that runs into it blows up. I'm not exactly sure how to do the collision there, because you need to follow up the slope of the line and I find the slope by y2-y1/x2-x1. By the end, the whole slope is found, but I'm not sure how to have it actually know where the line is, as opposed to a box with the line in it.

Here's my draw method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
      public void draw(int _x3,int x, int y, Graphics g)
{
            double x3=(double)_x3;
            MissileCommand Commander = new MissileCommand();
            Graphics2D g2D=(Graphics2D)g;
            if(x>=x3)
            {
                  m=((double)y/((double)x-x3)); //I have to parse it to a double for a true fraction.
                 System.out.println("Slope: "+m+"X change?: "+x);
                  double m2=m;
            //      m+=100;
                 x2=x3+1;
                  x+=x3;
                  while(m<=y && x2<=x )
                  {
                        //System.out.println("Slope: "+m+", "+m2+"\nX value: "+x+"\nY value: "+y);
                       Shape liney = new Line2D.Double(x3,0,x2,m); //I draw to a double. Yay²! I saved Seth from having to do an algorithm.
                       g2D.draw(liney);
                        Commander.delay();
                        x2++;
                        m+=m2;
                        System.out.println("Far: "+Far);
                  }
                  x-=x3;
            }

            if(x><x3)             {
                  m=(Math.abs((double)y/((double)x-x3))); if(m==0) m++;
                  double m2=m; //      m+=100; x2=x3; //      x+=100; while(m><=y && x2>=x)
                 {
                        //system.out.println("slope: "+m+", "+m2+"\nx value: "+x+"\ny value: "+y);
                       shape liney = new line2d.double(x3,0,x2,m);
                        g2d.draw(liney);
                        commander.delay();
                        //repaint();
                       x2--;
                        m+=m2;
                        system.out.println("other far: "+far);
                  }
            }
      }


any help would be greatly appreciated, thanks.>
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #1 - Posted 2005-05-29 17:04:17 »

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/Line2D.html

intersects()

弾幕 ☆ @mahonnaiseblog
Offline yoshidude

Junior Newbie




Java games rock!


« Reply #2 - Posted 2005-05-29 17:07:28 »

Wow, I feel dumb. Especially because I use Line2D in my method.
Thanks so much though, I appreciate it.

Although, that is a boolean. I need the coordinate value of where it happens. Is there a way to get that?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #3 - Posted 2005-05-29 17:50:01 »

>Is there a way to get that?

From that method? No.

Well, the changes between the frames are pretty small. I guess it's hard to spot the difference. I mean we're talking about 1-2 pixels here.

You could do that stuff with vector math. Basically you need to find the spot were those two linear graphs intersect. (Math from 8th grade is enough for that. Try to remember that stuff Wink)

弾幕 ☆ @mahonnaiseblog
Offline yoshidude

Junior Newbie




Java games rock!


« Reply #4 - Posted 2005-05-29 17:57:38 »

I used a different method and got it, ptLineDist.

Thank you so much for pointing me there, you have no idea how awesome it is. I had so much done, but that wouldn't work, so it wasn't even a game. Thank you thank you thank you!

I did see an open-source Missile Command that used Vectors, but I wasn't sure how it worked and I really didn't have time to tear down my program and re-work it. Otherwise, I'd have done that instead.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (81 views)
2013-05-17 21:29:12

alaslipknot (91 views)
2013-05-16 21:24:48

gouessej (122 views)
2013-05-16 00:53:38

gouessej (114 views)
2013-05-16 00:17:58

theagentd (126 views)
2013-05-15 15:01:13

theagentd (113 views)
2013-05-15 15:00:54

StreetDoggy (158 views)
2013-05-14 15:56:26

kutucuk (180 views)
2013-05-12 17:10:36

kutucuk (180 views)
2013-05-12 15:36:09

UnluckyDevil (187 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.156 seconds with 21 queries.