## Need help with 2d collision ASAP

Game development specific discussions.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

### Re: Need help with 2d collision ASAP

thesanman112 wrote:the building blocks for the great Atari game BREAKOUT!!!

its amazing that you basically replicated that game in under 70 lines of code..... I think the original atari had about 128 byes RAM onboard and the program cartridge was limited to 4kb total...lol

if we could only go back to those days with the knowledge we amateurs have today... we could make some awesome games and money...
Boromir
Posts: 451
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

### Re: Need help with 2d collision ASAP

dodicat wrote:For fun
Entrapment.

After quite some time of playing I managed to catch them all. :)
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

### Re: Need help with 2d collision ASAP

Code: Select all

`randomize timerdim as single x,y,xd,xs,ys,bx(100),by(100),bs(100),bxs(100),bys(100),bc(100),bf(100),level'''dim as integer scrw,scrh,c,tb,speed,bspeed,collide,bsx,bsy,fl,effectsscreen 18,32,2',1speed=4:bspeed=10:level=1'increment .5'level=2start:'speed=speed*levelSCRW = 640SCRH = 480c=0:tb=0:collide=0xd=1x=1:y=1for y=75 to 125 step 25for x=75 to 525/level step 50c=c+1:bf(c)=0:bx(c)=x*level:by(c)=y:bys(c)=25:bxs(c)=50:bc(c)=int(rnd*25500000)+1next xnext ytb=c:c=c+1:bf(c)=0:bx(c)=300:by(c)=440:bys(c)=15:bxs(c)=75:bc(c)=200015:x=120:y=200'******** program loop ***********doscreenset 1,0color rgb(255,255,255),rgb(55,100,250)clsif MultiKey(&H4B)  Then bx(tb+1)=bx(tb+1)-(5*level)If MultiKey(&H4D)  Then bx(tb+1)=bx(tb+1)+(5*level)if bx(tb+1)<1 then bx(tb+1)=1if bx(tb+1)>scrw-75 then bx(tb+1)=scrw-75ys=yx=x+(cos(xd)*speed)y=y+(sin(xd)*speed)if y=ys then xd=xd-.5if x-bspeed<1 then x=1+bspeed:xd=(xd*-1)+3.1415926if x+bspeed>scrw then x=scrw-bspeed:xd=(xd*-1)+3.1415926if y-bspeed<1 then y=1+bspeed:xd=xd*-1if y+bspeed>scrh then y=scrh-bspeed:xd=xd*-1:goto startfor c=1 to tb+1    if bf(c)=0 then        if x+(cos(xd)*bspeed)>bx(c) and x+(cos(xd)*bspeed)<bx(c)+bxs(c) then            if x<bx(c) or x>bx(c)+bxs(c) then                 if y+(sin(xd)*bspeed)>by(c) and y+(sin(xd)*bspeed)<by(c)+bys(c) then                  xd=(xd*-1)+3.1415926                if level<1.2 then  if c<tb+1 then collide=collide+1:bf(c)=1                  if c=tb+1 then if MultiKey(&H4B) then xd=xd-.5                  if c=tb+1 then if MultiKey(&H4D) then xd=xd+.5                end if            end if        end if        if x+(cos(xd)*bspeed)>bx(c) and x+(cos(xd)*bspeed)<bx(c)+bxs(c) then            if x>bx(c) and x<bx(c)+bxs(c) then                if y+(sin(xd)*bspeed)>by(c) and y+(sin(xd)*bspeed)<by(c)+bys(c) then                  xd=xd*-1                  if c<tb+1 then bf(c)=1:collide=collide+1                  if c=tb+1 then if MultiKey(&H4B) then xd=xd-.5                  if c=tb+1 then if MultiKey(&H4D) then xd=xd+.5                end if            end if        end if    end ifnext cfor c=1 to tb+1    if bf(c)=0 then         line (bx(c)+2,by(c)+2)-(bx(c)+bxs(c)-2,by(c)+bys(c)-2),bc(c),bf       ' line (bx(c),by(c))-(bx(c)+bxs(c),by(c)+bys(c)),&h000000,bif effects=0 then    if c<tb+1 then   for fl=1 to 50:pset(bx(c)+int(rnd*(bxs(c))),by(c)+int(rnd*3)),&hffffff:next fl   for fl=1 to 50:pset(bx(c)+int(rnd*(bxs(c))),by(c)+bys(c)-int(rnd*3)),&hffffff:next fl   for fl=1 to 25:pset(bx(c)+int(rnd*3),by(c)+int(rnd*(bys(c)))),&hffffff:next fl   for fl=1 to 25:pset(bx(c)+bxs(c)-int(rnd*3),by(c)+int(rnd*(bys(c)))),&hffffff:next flend ifend if   end ifnext ccircle(x,y),8,&hff2020,,,,flocate 1,1:print levelflip 1,0sleep 1if collide=tb then level=level+.2:speed=speed*level:goto startloop until multikey(&H01)'sleep 10'do 'loop until multikey(&h4b)`
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

### Re: Need help with 2d collision ASAP

Boromir wrote:
dodicat wrote:For fun
Entrapment.

After quite some time of playing I managed to catch them all. :)

Best i could do was getting 2 trapped......
datwill310
Posts: 355
Joined: May 29, 2015 20:37

### Re: Need help with 2d collision ASAP

Hi all, just thought I'd give an update on the collision situation.

Thanks to you guys, collision works perfect! With quite a lot of angle measurements, and changing of moving states upon different directions of collision, characters can now land on things, get damaged, bump their heads! Even those one-way platforms you get which you can jump through and land on!
But I am having only ONE more issue: if the co-ordinates of any entity are negative (i.e. say if character is partially off the screen near the top, or just walking off of the screen to the left), collision begins to screw up. (i.e. inoperable at all until co-ordinates are back to positive).
I am having a few ideas of what's going wrong, but I'd like to get confirmation of sorts before I delve into solutions too deep, because there may be A LOT of calculations to change and I don't want to screw up and find out when it's too late...

I'll post a (rather large) snippet of code which deals with the angles of collision, and maybe you guys could point out some maths errors or point me to the right direction (nice pun there ;]):

Code: Select all

`-code removed-`

Yes, a lot of angles, no ;)? I am wondering if some of the co-ordinates in those measurements, and the initial positional conditions at the start may need some editing in terms of dealing with negative values.
Here is a lengthy description of what each line does:

Code: Select all

`-code removed-`

If there is no issue with this code as far as using negative values is concerned, then the issue is elsewhere in the engine 8|.
Thanks for all your help guys, I wouldn't be here without it!

EDIT: also forgot to mention, one of my ideas involves the fact that the X and Y values are LONGS, while the WIDTH and HEIGHT values are UNSIGNED LONGS. Could this have something to do with it?
Last edited by datwill310 on May 31, 2017 16:58, edited 2 times in total.
MrSwiss
Posts: 3786
Joined: Jun 02, 2013 9:27
Location: Switzerland

### Re: Need help with 2d collision ASAP

datwill310 wrote:EDIT: also forgot to mention, one of my ideas involves the fact that the X and Y values are LONGS, while the WIDTH and HEIGHT values are UNSIGNED LONGS. Could this have something to do with it?
First quick try I'd make: define them all as Long ...
datwill310
Posts: 355
Joined: May 29, 2015 20:37

### Re: Need help with 2d collision ASAP

MrSwiss wrote:
datwill310 wrote:EDIT: also forgot to mention, one of my ideas involves the fact that the X and Y values are LONGS, while the WIDTH and HEIGHT values are UNSIGNED LONGS. Could this have something to do with it?
First quick try I'd make: define them all as Long ...

Thanks for the pointer: collision works as expected now!
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

### Re: Need help with 2d collision ASAP

datwill310 wrote:
MrSwiss wrote:
datwill310 wrote:EDIT: also forgot to mention, one of my ideas involves the fact that the X and Y values are LONGS, while the WIDTH and HEIGHT values are UNSIGNED LONGS. Could this have something to do with it?
First quick try I'd make: define them all as Long ...

Thanks for the pointer: collision works as expected now!

lol - hate those stupid 'little things' like datatypes that can give errors 'sometimes'...

congrats on you finishing the routine!
datwill310
Posts: 355
Joined: May 29, 2015 20:37

### Re: Need help with 2d collision ASAP

leopardpm wrote:
datwill310 wrote:
MrSwiss wrote:First quick try I'd make: define them all as Long ...

Thanks for the pointer: collision works as expected now!

lol - hate those stupid 'little things' like datatypes that can give errors 'sometimes'...

congrats on you finishing the routine!

Yes, so do I!

Thanks, I've given credit to you and BasicCoder2 for the algorithm ;)
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

### Re: Need help with 2d collision ASAP

datwill310 wrote:
leopardpm wrote:
datwill310 wrote:Thanks for the pointer: collision works as expected now!

lol - hate those stupid 'little things' like datatypes that can give errors 'sometimes'...

congrats on you finishing the routine!

Yes, so do I!

Thanks, I've given credit to you and BasicCoder2 for the algorithm ;)
no credit needer for me, just love
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

### Re: Need help with 2d collision ASAP

WHAT!!! Man oh man, it doesn't matter how hard you work you just cant satisfy some people...LOL!!!
datwill310
Posts: 355
Joined: May 29, 2015 20:37

### Re: Need help with 2d collision ASAP

thesanman112 wrote:WHAT!!! Man oh man, it doesn't matter how hard you work you just cant satisfy some people...LOL!!!

Yes I'll give credit you too ;)!
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

### Re: Need help with 2d collision ASAP

so many perspectives of collision code appear from a persons own take on it, its amazing the spin offs of code that come from them. There is SO much to learn no matter how much you know!!! and just looking at other peoples code you can learn so much!!! I posted a topic in this forum i believe almost 10 years ago, it summed up collision in a few lines of code, i didnt really explain them all but it had all the formulas, at the time there was at least one new member a day wanting to learn about it. once you grasp the basics everything else is left to the imagination.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

### Re: Need help with 2d collision ASAP

thesanman112 wrote:so many perspectives of collision code appear from a persons own take on it, its amazing the spin offs of code that come from them. There is SO much to learn no matter how much you know!!! and just looking at other peoples code you can learn so much!!! I posted a topic in this forum i believe almost 10 years ago, it summed up collision in a few lines of code, i didnt really explain them all but it had all the formulas, at the time there was at least one new member a day wanting to learn about it. once you grasp the basics everything else is left to the imagination.

I agree! It's like that with programming some games: once the basics are done (physics, collision, AI), the rest is actually pretty easy, it's just a matter of putting it all together.