473,397 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How can I get an Element based on position? (collision detection)

example...

Div1 (moves with arrow keys and is the top layer)
Div2 (bottom layer)

what I want to try to do is the following:

when Div1 moves over Div2 (if Div1's position = Div2's position) execute code.

the problem I have is I know how to do it as listed above if I know the ID or Div2,3,4 etc, but all the Div's except for Div1 are going to be dynamically generated.

here is a better example: Checkers.

the border is created dynamically (red/black squares)
Now if I move a object above a red square - is there a way using javascript to say detect that the square I moved to is red?

I will know Div1's positioning, but need to know when's it's positioning matching another Div - how to pull that Div's ID or other info.
Jun 29 '10 #1
4 2302
Dormilich
8,658 Expert Mod 8TB
the problem I have is I know how to do it as listed above if I know the ID or Div2,3,4 etc, but all the Div's except for Div1 are going to be dynamically generated.
that doesn’t matter. you can always store the reference to those created div objects (I mean, what other thing do you do with an id than using it to get the div object?)

to detect, whether the square is red or black, that depends on how you determine the position. (e.g. in a mouseup event, the event should occur in the div as well)
Jun 29 '10 #2
maybe a better example is needed.

Here is a code example:

Expand|Select|Wrap|Line Numbers
  1. for (i=0;i<10;i++)
  2. {
  3. if (i==5)
  4. {
  5. document.write('<br>');
  6. }
  7. document.write('<div class="bgdiv" style="width=32px;height=32px;">'+i+'>');
  8. }
  9.  
so now I have a 5x2 set of 32px divs with each having the div number as the innerHTML, on top of that (z-index:100) I have another div that moves based on the arrow keys. as the top div moves I need to be able to find out which of the generated divs is below it.

so if the top div is moved (by the arrow keys, if it were a game this would be the player) to position 96, 64 (which would be on top of div#7) how do I pull the innerHTML of that div with only knowing the position of the player div?

I know this may sound really confusing - let me know if you need more information.
Jun 29 '10 #3
Dormilich
8,658 Expert Mod 8TB
so now I have a 5x2 set of 32px divs with each having the div number as the innerHTML, on top of that (z-index:100)
wouldn’t it be easier to use a table?

I have another div that moves based on the arrow keys. as the top div moves I need to be able to find out which of the generated divs is below it.
that means you change the top/left position by pressing the arrow keys?
Jun 29 '10 #4
wouldn’t it be easier to use a table?
I could do with a table for the 32px divs - can change that.

that means you change the top/left position by pressing the arrow keys?
yes, the top/left position changes based on arrow keys (example left arrow moves -8px) and would want to be able to find out when moved -8px left is it now above box 152 or box 151 still since the boxes are 32px

pretty much if I had the ability to use the X,Y of the moving object and get the other objects located at that X,Y then I can make it work.


UPDATE:
found my fix.

Expand|Select|Wrap|Line Numbers
  1. function getPosition(playerX,playerY)
  2. {
  3. objectCounter=document.getElementsByTagName('td');
  4. detectCollision=false;
  5. for (i=0;i<objectCounter.length;i++)
  6. {
  7.     if (playerX==objectCounter[i].offsetLeft && playerY==objectCounter[i].offsetTop)
  8.     {
  9.         document.getElementById('guiDebug').innerHTML=objectCounter[i].id;
  10.         document.getElementById('guiDebug').innerHTML+='<br>'+objectCounter[i].offsetLeft;
  11.         document.getElementById('guiDebug').innerHTML+=', '+objectCounter[i].offsetTop;
  12.         document.getElementById('guiDebug').innerHTML+='<br>'+objectCounter[i].value;
  13.         if (objectCounter[i].value=="Wall")
  14.         {
  15.             detectCollision=true;
  16.         }
  17.     }
  18. }
  19. return detectCollision;
  20. }
  21.  
Jun 29 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Thorsten Reichelt | last post by:
Hi, I'm involved in a research project on spatial prepositions. In that project we use very simple, static 3D maps that are represented in a tiny subset of x3d enriched with some few linguistic...
17
by: Mark | last post by:
uhhmmm... not really sure how to word this. i cant get get this to compile.. i'm not sure what the proper syntax to do this is.. hopefully it's self explanatory. here's my class: ------ class...
3
by: wandoledzep | last post by:
I have been banging my head on a wall for two days now. All I have is a picbox bouncing around the form, and I want it to react when it hits the block (another picbox). Everything works correctly...
2
by: petermichaux | last post by:
Hi, I tried the following and everything worked fine. element.style.position="relative"; Then I tried to make the CSS rule important and it didn't work. The positioning was all wrong in...
1
by: vinz4u | last post by:
I have to write codes for the following, theres suppose to be three threads running at the same time... if you guys cant write code, then can u please direct me to some websites where i can find some...
5
by: vunet.us | last post by:
I have no perfect cross-browser solution to position html element on the bottom of the page. I know that with Firefox I do it as: position:fixed; bottom:0px; However, IE does not understand that....
0
by: licombo | last post by:
I am making a flash game which is similar to the classic game Asteroids. I am stuck in the final step of the game where I have to make the collision detection working before the whole game is...
6
by: Markus | last post by:
Hello I have a navigation structured as follows: <ul> <li> <a>Chapter1</a> <ul> <li><a>Chapter 1.1</a></li> <li><a>Chapter 1.2</a></li>
0
by: Microsoft Newsserver | last post by:
Im using IE 6/7 / ASP.NET / Javascript. I need to determine the absolute position of elements on my page client side. In summart, I need to know if an event triggered mouse position appears...
7
by: Humakt | last post by:
This is 2D game where there are breakable boxes in the middle two bats on both sides and one or more balls breaking the boxes. Kind of like a mixup of pong and breakout. Collision detection...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.