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

Postby leopardpm » May 04, 2017 0:39

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

Postby Boromir » May 04, 2017 1:43

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

Postby thesanman112 » May 04, 2017 16:52

added level upgrades...

Code: Select all

randomize timer
dim 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,effects
screen 18,32,2',1

speed=4:bspeed=10:level=1'increment .5
'level=2
start:
'speed=speed*level
SCRW = 640
SCRH = 480
c=0:tb=0:collide=0
xd=1
x=1:y=1
for y=75 to 125 step 25
for x=75 to 525/level step 50
c=c+1:bf(c)=0:bx(c)=x*level:by(c)=y:bys(c)=25:bxs(c)=50:bc(c)=int(rnd*25500000)+1
next x
next y
tb=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 ***********
do
screenset 1,0
color rgb(255,255,255),rgb(55,100,250)
cls
if 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)=1
if bx(tb+1)>scrw-75 then bx(tb+1)=scrw-75
ys=y
x=x+(cos(xd)*speed)
y=y+(sin(xd)*speed)
if y=ys then xd=xd-.5
if x-bspeed<1 then x=1+bspeed:xd=(xd*-1)+3.1415926
if x+bspeed>scrw then x=scrw-bspeed:xd=(xd*-1)+3.1415926
if y-bspeed<1 then y=1+bspeed:xd=xd*-1
if y+bspeed>scrh then y=scrh-bspeed:xd=xd*-1:goto start
for 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 if
next c
for 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,b

if 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 fl
end if
end if

   end if
next c
circle(x,y),8,&hff2020,,,,f
locate 1,1:print level
flip 1,0
sleep 1
if collide=tb then level=level+.2:speed=speed*level:goto start
loop 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

Postby thesanman112 » May 05, 2017 3:46

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

Postby datwill310 » May 31, 2017 15:14

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

Postby MrSwiss » May 31, 2017 15:29

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

Postby datwill310 » May 31, 2017 16:53

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

Postby leopardpm » May 31, 2017 17:15

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

Postby datwill310 » May 31, 2017 20:15

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

Postby leopardpm » May 31, 2017 20:52

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

Postby thesanman112 » Jun 03, 2017 4:59

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

Postby datwill310 » Jun 05, 2017 0:55

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

Postby thesanman112 » Jun 05, 2017 14:21

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

Postby datwill310 » Jun 06, 2017 20:09

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.

Return to “Game Dev”

Who is online

Users browsing this forum: No registered users and 4 guests