AutoHotkey Community

It is currently May 27th, 2012, 11:26 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Picture Collision Help
PostPosted: August 8th, 2009, 12:49 am 
Offline

Joined: March 27th, 2009, 10:48 pm
Posts: 71
Im wondering how to do this,
im making a game and I need to tell when the player you are controlling is hit.
There are a bunch of moving objects so it would be really hard to get this to work and I am stuck.
I have tried a cheezy way by just image searching every second,
but this lags alot and makes the game run much slower.
I was wondering if anyone knows of a script to tell when two pictures collide.
I have searched the forums with no results.

Here is a picture of the game.
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 8th, 2009, 1:42 am 
since u mention all those are moving parts, i assume u mange to declare the image location as x,y point
and if u are planning to make a game with this kind of stuff, i also assume that u have some sort of math background
so if u dont understand the stuff i am explaining below, well maybe u should move on to another project

so lets assume there are 2 stones in the screen moving, which their location of the center point of the image will be:
x1,y1
x2,y2
then simply, u need to define a size for the object, and lets define the size of:
stone1 radius = 5
stone2 radius = 10

then as what u are trying to do, is to know whether they overlap each other or not

because as u said, the location of the stone is constantly moving, so settimer will be good to use

so lets say u have a function to decide where the stones are, so u have x1,y1,x2,y2 values already. in the settimer function, u would like to have something like:
Code:
function:
x1_low :=x1-5
x1_up  :=x1+5
y1_low :=y1-5
y1_up  :=y1+5
x2_low :=x2-10
x2_up  :=x2+10
y2_low :=y2-10
y2_up  :=y2+10
if x1_low between %x2_low% and %x2_up%
if y1_low between %y2_low% and %y2_up%
msgbox, overlap
if x1_up between %x2_low% and %x2_up%
if y1_up between %y2_low% and %y2_up%
msgbox, overlap
if x1_low between %x2_low% and %x2_up%
if y1_up between %y2_low% and %y2_up%
msgbox, overlap
if x1_up between %x2_low% and %x2_up%
if y1_low between %y2_low% and %y2_up%
msgbox, overlap
return


ok as u can see, this is only a simple example using 4 corners to check whether they are overlap or not, so if u want it to be more accurate checking overlapping, more points define is required

anyhow, with only 2 stones and 4 points checking, there consist with 4 checking sets. if it is 3 stones and 4 points checking, there will consist with 12 checking sets. 5 stones 4 points = 40 sets equations...etc etc
the more stones u have, the slower it gets, as u can see in the concept, u need to check stone1 to stone2, stone3,....stoneN, and then check stone2 to stone3, stone4...stoneN...etc, like:
Image
but of course with today's computer speed, even with 1000 calculation per second is good enough, and also very depends on ur refresh moving stones speed


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 8th, 2009, 5:53 am 
Offline

Joined: March 27th, 2009, 10:48 pm
Posts: 71
Yea, that is what I was thinking of before, a type of hitbox.
I have tried it out and cant seem to get it to work. :cry:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 8th, 2009, 6:31 am 
Offline

Joined: August 8th, 2009, 6:28 am
Posts: 8
Location: Melbourne, AU
Well, it is easy. You just need to declare the object that is spinning, or moving on the screen to be 'lit' or ready to click on. That object then can be associated to other code, or routines, or things you want for your game.

Signed, SB
Just my Opinion / AHK Guru


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 8th, 2009, 7:02 am 
Offline

Joined: March 27th, 2009, 10:48 pm
Posts: 71
Ok, I finally got the hitboxes to work.
Heres the function for it:

Code:
Define_Hitbox(_X, _Y, _Size)
   {
   static
   Global Px, Py, Health
   _Y += 25
   _X += 5
   If _Size = 1
      {
      1x_TL := _X
      1y_TL := _Y
      1x_TR := _X+25
      1y_TR := _Y
      1x_BL := _X
      1y_BL := _Y+15
      1x_BR := _X+25
      1y_BR := _Y+15
      }
   If _Size = 2
      {
      1x_TL := _X
      1y_TL := _Y
      1x_TR := _X+45
      1y_TR := _Y
      1x_BL := _X
      1y_BL := _Y+45
      1x_BR := _X+45
      1y_BR := _Y+45
      }
   If _Size = 3
      {
      1x_TL := _X
      1y_TL := _Y
      1x_TR := _X+100
      1y_TR := _Y
      1x_BL := _X
      1y_BL := _Y+75
      1x_BR := _X+100
      1y_BR := _Y+75
      }
   2x_TL := Px
   2y_TL := Py
   2x_TR := Px+25
   2y_TL := Py
   2x_BL := Px
   2y_BL := Py+25
   2x_BR := Px+25
   2y_BR := Py+25
   If 2x_TL Between %1x_TL% AND %1x_TR%
      {
      If 2y_TL Between %1y_TL% AND %1y_BL%
         {
         Health -= 1
         GuiControl,, HealthBar, %Health%
         Return
         }
      }
   If 2x_TR Between %1x_TL% AND %1x_TR%
      {
      If 2y_TR Between %1y_TL% AND %1y_BL%
         {
         Health -= 1
         GuiControl,, HealthBar, %Health%
         Return
         }
      }
   If 2x_BL Between %1x_TL% AND %1x_TR%
      {
      If 2y_BL Between %1y_TL% AND %1y_BL%
         {
         Health -= 1
         GuiControl,, HealthBar, %Health%
         Return
         }
      }
   If 2x_BR Between %1x_TL% AND %1x_TR%
      {
      If 2y_BR Between %1y_TL% AND %1y_BL%
         {
         Health -= 1
         GuiControl,, HealthBar, %Health%
         Return
         }
      }
   Return
   }


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 9th, 2009, 3:24 am 
Offline

Joined: August 8th, 2009, 6:28 am
Posts: 8
Location: Melbourne, AU
That looks excellent. Good work! :D


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Yahoo [Bot] and 19 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group