Results 1 to 2 of 2
Thread: Word Collider
- 11-10-2012, 04:56 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
Word Collider
Hi people Im really new to Java barely understand I know I have to do a loop or something in void animate Randomize the position of the two words repeatedly and stop when the bounding box of the two words overlaps. But I dont know how to do it how is the program is going to know when is overlaps im so confused help me please!!!Java Code:/** * Write a description of class WordCollider here. * * @author (your name) * @version (a version number or a date) */ public class WordCollider { // instance variables - replace the example below with your own private Text word1; private Text word2; // the characters contained in word1 private Text[] charWord1; // the characters contained in word2 private Text[] charWord2; /** * Constructor for objects of class WordCollider */ public WordCollider(String w1, String w2) { // initialise instance variables word1 = new Text(w1); word1.randomizePosition(); word1.changeColor("green"); word1.changeSize(48); word1.makeVisible(); word2 = new Text(w2); word2.randomizePosition(); word2.changeColor("orange"); word2.changeSize(48); word2.makeVisible(); charWord1 = new Text[w1.length()]; charWord2 = new Text[w2.length()]; fillChars(charWord1, w1); fillChars(charWord2, w2); } private void fillChars(Text[] a, String w) { char[] cs = w.toCharArray(); for (int i=0; i<a.length; i++) { a[i] = new Text(""+cs[i]); a[i].changeSize(48); a[i].changeColor("red"); } } /** * Randomize the position of the two words repeatedly and stop * when the bounding box of the two words overlaps. */ public void animate() { word1.makeInvisible(); word2.makeInvisible(); } /** * erase the words and any other characters on the display */ public void clearDisplay() { } /** * check if the bounding box of the two words overlaps. * @return true when the words overlap and false otherwise. */ private Boolean checkOverlap() { return false; } }
- 11-13-2012, 05:36 PM #2
Re: Word Collider
Woah there, slow down. You're asking several things at once. If I understand you, you are asking how to:
1. Animate, or at least randomize the position of the items
2. Collision detection on those items
Is that correct? If so, part 1 would involve making a look (outside of your paint method) that tells the system how often to refresh the screen. You would also need a method that actually paints the items onto the screen, and then usually another method that performs logic (like movement and collision). A normal loop would do something like:
- Perform logic (move items, bounce items, whatever)
- Paint the new state of all the objects onto a panel
- refresh the screen
Part 2 is more complex depending on the number of entities. For just a few bouncing entities, a brute force iteration would be fine (check every item against every other item for a collision). There are ways to optimize this, such as only checking every pair of elements once, etc... If you need to work with hundreds or thousands of entities, this is a MUCH more complex problem which is usually solved with data structures (like QuadTrees).
Similar Threads
-
how to reverse word by word?
By yamicaitsith in forum New To JavaReplies: 13Last Post: 09-16-2012, 11:30 PM -
!word
By walsby14 in forum New To JavaReplies: 2Last Post: 07-16-2012, 04:36 PM -
Find a text in a file and write a word following this word
By lemontree45 in forum New To JavaReplies: 3Last Post: 08-30-2011, 04:44 PM -
Word
By right2001 in forum New To JavaReplies: 2Last Post: 04-07-2009, 03:25 AM -
Word OLE
By Java Tip in forum SWTReplies: 0Last Post: 07-25-2008, 02:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks