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 02, 2017 2:06

best if from center points, any other point will give different and strange results...
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Need help with 2d collision ASAP

Postby datwill310 » May 02, 2017 2:07

leopardpm wrote:best if from center points, any other point will give different and strange results...

OK yes, I think I'm experiencing some of those right now 8|! Thanks for the tip.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Need help with 2d collision ASAP

Postby leopardpm » May 02, 2017 2:30

yup, thinking about it, if you were doing it from UL point then it could explain your teleport 'feature'...aka bug
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Need help with 2d collision ASAP

Postby thesanman112 » May 02, 2017 14:48

I think every programmer has worked on collision of some sort for many hours, some of us for many days, getting it to work good and fast, everyone with their own perspective or take on it. I use to have lots of code for collision, spent many days and gallons of coffee working on it. I remember using a method that would scan in a circle around a sprite or player and check for collision which in turn would give you the angle of collision as well...
basically just check points along circle and see if they are inside of an objects rectangular view, when the scan resulted in a hit, then I would use the negative value of movement relevant to the angle of collision, it was crude but very fast.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Need help with 2d collision ASAP

Postby datwill310 » May 02, 2017 20:04

thesanman112 wrote:I think every programmer has worked on collision of some sort for many hours, some of us for many days, getting it to work good and fast, everyone with their own perspective or take on it. I use to have lots of code for collision, spent many days and gallons of coffee working on it. I remember using a method that would scan in a circle around a sprite or player and check for collision which in turn would give you the angle of collision as well...
basically just check points along circle and see if they are inside of an objects rectangular view, when the scan resulted in a hit, then I would use the negative value of movement relevant to the angle of collision, it was crude but very fast.

Yes, what you say is true.

Just a little final update, I've fixed all the bugs related to collision (they aren't in the example posted by BC2, except for one which occurs when the static rectangle is smaller than the moving rectangle and positioned like this:

Code: Select all

+------------+
|            |
|            |
|         +--|--------+
|         |  |        |
|         |  |        | <- static rectangle
|         |  |        |
|         +--|--------+
|            |
|            |
+------------+ <- moving rectangle

I'll post an edited version of BC2's code with a few changes to give an example.
Note that we will also have to check for the sides of the moving rectangle, and not just its corners in this case.
And I reckon that might become an issue, but I can amend the code for that if it does.). That includes randomly teleporting to the tops of platforms ;). A little issue with the scrolling engine has popped up, but I've done a quick patch job to cover the error up and it is satisfactory for the most part. I've adjusted the way the corner angles are measured so that the character doesn't prematurely fall off a platform (but that was part of my code exclusively). Also angles are now measuring correctly since I measured from the centroids of the character and entity instead of the UL corner (thanks leopard ;D)!

I would like to thank you guys again for your help in understanding how collision works!.And hopefully it will also help others who come across this post!

EDIT:
OK, a few notes:
1. I can't remember if this measures the angles wrong or not (due to my tinkering), but that's not where the issue lies in this case.
2. You move the rectangle now with the mouse.

Code: Select all

'some useful defines
Const Pi = 4 * Atn(1)
Dim Shared As single TwoPi = 8 * Atn(1)
Dim Shared As single RtoD = 180 / Pi   ' radians * RtoD = degrees
Dim Shared As single DtoR = Pi / 180   ' degrees * DtoR = radians

const SCRW = 640
const SCRH = 480
screenres SCRW,SCRH,32

type RECTANGLE
    as integer x
    as integer y
    as integer w
    as integer h
    as integer xd
    as integer yd
    as single  xc   'centroid of rectangle
    as single  yc
    as ulong   c
end type

dim shared as RECTANGLE rec1,rec2
dim shared as integer collision

rec1.x = 10
rec1.y = 10
rec1.w = 63
rec1.h = 137
rec1.xd = -1
rec1.yd = 1
rec1.c  = rgb(255,100,100)

rec2.x = 200
rec2.y = 200
rec2.w = 100
rec2.h = 100
rec2.xd = 0
rec2.yd = 0
rec2.c  = rgb(100,255,100)

function spriteCollision(s1 as RECTANGLE,s2 as RECTANGLE) as integer
    dim as integer hit
    hit = 0
    'top/left corner
    if s1.x > s2.x and s1.x < (s2.x+s2.w) then
        if s1.y > s2.y and s1.y < (s2.y+s2.h) then
            hit = hit or 1
        end if
    end if
    'top/right corner
    if s1.x+s1.w > s2.x and s1.x+s1.w < (s2.x+s2.w) then
        if s1.y > s2.y and s1.y < (s2.y+s2.h) then
            hit = hit or 2
        end if
    end if
    'bottom/left corner
    if s1.x > s2.x and s1.x < (s2.x+s2.w) then
        if s1.y+s1.h > s2.y and s1.y+s1.h < (s2.y+s2.h) then
            hit = hit or 4
        end if
    end if
    'bottom/right corner
    if s1.x+s1.w > s2.x and s1.x+s1.w < (s2.x+s2.w) then
        if s1.y+s1.h > s2.y and s1.y+s1.h < (s2.y+s2.h) then
            hit = hit or 8
        end if
    end if
    return hit
end function

sub moveRectangle(rec as RECTANGLE)
    rec.x = rec.xd
    rec.y = rec.yd
    'check for border collision
    'if rec.x + rec.w > SCRW or rec.x < 0 or rec.y+rec.h > SCRH or rec.y<0 then
    '    rec.x = rec.x - rec.xd  'undo move
    '    rec.y = rec.y - rec.yd
    'end if
end sub


sub update()
    dim as integer hit = 0
    dim as single angle,xx,yy
    screenlock
    cls
    'draw rectangles
    line (rec1.x,rec1.y)-(rec1.x+rec1.w,rec1.y+rec1.h),rec1.c,b
    line (rec2.x,rec2.y)-(rec2.x+rec2.w,rec2.y+rec2.h),rec2.c,b
    'move rectangles
    moveRectangle(rec1)
   rec1.x = rec1.xd
   rec1.y = rec1.yd
   
    'moveRectangle(rec2)
    hit = spriteCollision(rec1,rec2)
    print hit
    print rec1.xd,rec1.yd
    rec1.xc = rec1.x 'rec1.x+(rec1.w)/2
    rec1.yc = rec1.y 'rec1.y+(rec1.h)/2
    rec2.xc = rec2.x 'rec2.x+(rec2.w)/2
    rec2.yc = rec2.y 'rec2.y+(rec2.h)/2
   'draw line
    line (rec1.xc,rec1.yc)-(rec2.xc,rec2.yc),rgb(255,255,0)
    xx = rec1.xc-rec2.xc
    yy = rec1.yc-rec2.yc
    angle = RtoD*(atan2(yy,xx))+180
    print angle
   
   'find and draw positions where rec1 meets corners of rec2
   pset (rec2.xc-rec1.w, rec2.yc-rec1.h) 'ul
   pset (rec2.xc+rec2.w, rec2.yc-rec1.h) 'ur
   pset (rec2.xc-rec1.w, rec2.yc+rec2.h) 'll
   pset (rec2.xc+rec2.w, rec2.yc+rec2.h) 'lr
   print rec2.xc-rec1.w, rec2.yc-rec1.h
    screenunlock
end sub

do
    update
    'ARROW KEYS
    rec1.xd = 0
    rec1.yd = 0
   
   getmouse(rec1.xd, rec1.yd)
   
    ''If MultiKey(&H4B)  Then rec1.xd = - 1:rec1.yd =  0
    ''If MultiKey(&H4D)  Then rec1.xd = + 1:rec1.yd =  0
    ''If MultiKey(&H48)  Then rec1.xd =   0:rec1.yd = -1
    ''If MultiKey(&H50)  Then rec1.xd =   0:rec1.yd = +1
    sleep 10
loop until multikey(&H01)
BasicCoder2
Posts: 3596
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Need help with 2d collision ASAP

Postby BasicCoder2 » May 02, 2017 21:55

datwill310 wrote:1. I can't remember if this measures the angles wrong or not (due to my tinkering)...

The reason I used centroids is because that gives you a proper spatial relationship between the areas.

The angles are "correct" depending what you mean by that.
left is 360 or 0 degrees
right is 180 degrees
up is 90 degrees
down is 270 degrees

Motion is relative so which rectangle is moving is unimportant to the collision routine it is only relevant when figuring out which direction they collided. I guess that could be incorporated into the collision routine? What counts is which rectangle is first in the call to the collision routine as we are looking at it from its point of view. All the collision routine does is tell you what combination of corners of the rectangle are inside the other rectangle coded as a 4 bit number. What you do with that information is up to you.
Thus if corner 1 and 2 are inside the other rectangle it will return 3
You can print them out like this:

Code: Select all

 print hit AND 1; hit AND 2; hit AND 4; hit AND 8
' 1 +-----+ 2
'   |     |
'   |     |
' 4 +-----+ 8
 


The collision rectangles are often smaller than the actual image rectangle. This allows overlap without collision. It is useful in things such as slopes where a rectangle would have the character floating above the surface.

If you google for programming sprite collisions you should find lots of useful stuff.

.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Need help with 2d collision ASAP

Postby thesanman112 » May 03, 2017 2:33

here is something I just whipped up to maybe help along the way....doesn't get much simpler than this, use the arrow keys to change direction.

Code: Select all


dim as single x,y,xd
xd=1
x=1:y=1

dim as integer scrw,scrh
SCRW = 640'1280
SCRH = 480'1024

screenres SCRW,SCRH,32',,1 for fullscreen

do
  '  update
    'ARROW KEYS
  'rec1.xd
   'rec1.xd = 0
   ' rec1.yd = 0
    If MultiKey(&H4B)  Then xd=xd-.1'rec1.xd = - 1:rec1.yd =  0  ****get some control of direction within a 360 degree pane****
    If MultiKey(&H4D)  Then xd=xd+.1'rec1.xd = + 1:rec1.yd =  0  **** basically just adding to XD or minus from XD with arrow keys***
'    If MultiKey(&H48)  Then yd=yd-.1'rec1.xd =   0:rec1.yd = -1
'    If MultiKey(&H50)  Then yd=yd+.1'rec1.xd =   0:rec1.yd = +1
    sleep 10
cls
x=x+(cos(xd)*2)'**** math to manipulate or translate the direction of movement or XD, set by arrow keys ****
y=y+(sin(xd)*2)'**** increase the value of 2 to move faster ****

'****** collision detected and XD is multiplied by -1 to give you the deflection angle but x pane references at 0 ...
'****** so include extra 180 degrees if collision is horizontal or along the x pane ******
'****** a complete circle is actually PI * 2 so we can just add PI to get deflection of X pane after multiplying by negative *****
'just check screen out of bounds collision and adjust angles if collision accurs
if x<1 then x=1:xd=(xd*-1)+3.1415926
if x>scrw then x=scrw:xd=(xd*-1)+3.1415926
if y<1 then y=1:xd=xd*-1
if y>scrh then y=scrh:xd=xd*-1

line(x,y)-(x+(10*cos(xd)),y+(10*sin(xd)))

locate 1,1:print xd

loop until multikey(&H01)


thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Need help with 2d collision ASAP

Postby thesanman112 » May 03, 2017 2:44

next I will draw a box in the middle and show collision against that....
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Need help with 2d collision ASAP

Postby thesanman112 » May 03, 2017 3:28

simple but effective box collision with moving object.


Code: Select all



dim as single x,y,xd,bx,by,bs'''
bx=300:by=200:bs=200
xd=1
x=1:y=1

dim as integer scrw,scrh,collide
SCRW = 640'1280
SCRH = 480'1024
screenres SCRW,SCRH,32',,1 for fullscreen
do
    If MultiKey(&H4B)  Then xd=xd-.1'rec1.xd = - 1:rec1.yd =  0  ****get some control of direction within a 360 degree pane****
    If MultiKey(&H4D)  Then xd=xd+.1'rec1.xd = + 1:rec1.yd =  0  **** basically just adding to XD or minus from XD with arrow keys***
'    If MultiKey(&H48)  Then yd=yd-.1'rec1.xd =   0:rec1.yd = -1
'    If MultiKey(&H50)  Then yd=yd+.1'rec1.xd =   0:rec1.yd = +1
    sleep 10
cls
x=x+(cos(xd)*2)
y=y+(sin(xd)*2)
if x<1 then x=1:xd=(xd*-1)+3.1415926
if x>scrw then x=scrw:xd=(xd*-1)+3.1415926
if y<1 then y=1:xd=xd*-1
if y>scrh then y=scrh:xd=xd*-1

'*** check ahead of line for box collision ****
if x+(cos(xd)*2)>bx and x+(cos(xd)*2)<bx+bs then
     if x<bx or x>bx+bs then
       if y+(sin(xd)*2)>by and y+(cos(xd)*2)<by+bs then
        xd=(xd*-1)+3.1415926'xd=xd*-1
       end if
     end if
end if

if x+(cos(xd)*2)>bx and x+(cos(xd)*2)<bx+bs then
   if x>bx and x<bx+bs then
      if y+(sin(xd)*2)>by and y+(cos(xd)*2)<by+bs then
        xd=xd*-1
      end if
   end if
end if

'**** draw box ****
line (bx,by)-(bx,by+bs)
line (bx,by+bs)-(bx+bs,by+bs)
line (bx+bs,by+bs)-(bx+bs,by)
line (bx+bs,by)-(bx,by)
'**** draw line ****
line(x,y)-(x+(10*cos(xd)),y+(10*sin(xd)))

loop until multikey(&H01)

dodicat
Posts: 6726
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Need help with 2d collision ASAP

Postby dodicat » May 03, 2017 9:24

For fun
Entrapment.

Code: Select all

 Screen 19,32
dim as integer xres,yres
screeninfo xres,yres
Type point
    As Single x,y,dx,dy
    As Integer radius
End Type

Type line
    As Single v1x,v1y,v2x,v2y
    as ulong col
End Type

#define onscreen (mx>10) and (mx<(xres-10)) and (my>10) and (my<(yres-10))
#define incircle(cx,cy,radius,x,y) (cx-x)*(cx-x) +(cy-y)*(cy-y)<= radius*radius

Function segment_distance(lx1 As Single, _
                         ly1 As Single, _
                         lx2 As Single, _
                         ly2 As Single, _
                         px As Single,_
                         py As Single, _
                      Byref ox As Single=0,_
                      Byref oy As Single=0) As Single
                 
             Dim As Single M1,M2,C1,C2,B
             B=(Lx2-Lx1):If B=0 Then B=1e-20
             M2=(Ly2-Ly1)/B:If M2=0 Then M2=1e-20
             M1=-1/M2
             C1=py-M1*px
             C2=(Ly1*Lx2-Lx1*Ly2)/B
    var L1=((px-lx1)*(px-lx1)+(py-ly1)*(py-ly1)),L2=((px-lx2)*(px-lx2)+(py-ly2)*(py-ly2))
    var a=((lx1-lx2)*(lx1-lx2) + (ly1-ly2)*(ly1-ly2))
        var a1=a+L1
        var a2=a+L2
        var f1=a1>L2,f2=a2>L1
         If f1 Xor f2 Then
    var d1=((px-Lx1)*(px-Lx1)+(py-Ly1)*(py-Ly1))
    var d2=((px-Lx2)*(px-Lx2)+(py-Ly2)*(py-Ly2))
    If d1<d2 Then Ox=Lx1:Oy=Ly1 : Return Sqr(d1) Else  Ox=Lx2:Oy=Ly2:Return Sqr(d2)
End If
var M=M1-M2:if M=0 then M=1e-20
    Ox=(C2-C1)/(M1-M2)
    Oy=(M1*C2-M2*C1)/M
    Return Sqr((px-Ox)*(px-Ox)+(py-Oy)*(py-Oy))
  End Function
  'optimize detection to save cpu.
Function DetectPointCollisions(Byref _that As point,_this As point) As Single
    Dim As Single xdiff = _this.x-_that.x
    Dim As Single ydiff = _this.y-_that.y
    If Abs(xdiff) > _this.radius*2 Then Return 0
    If Abs(ydiff) > _this.radius*2 Then Return 0
    var L=Sqr(xdiff*xdiff+ydiff*ydiff)
    If L<=(_this.radius+_that.radius) Then Function=L
End Function

Sub Check_PointCollisions(points() As point)
    For n1 As long =Lbound(points) To Ubound(points)-1
        For n2 As long =n1+1 To Ubound(points)
            var L=DetectPointCollisions(points(n1),points(n2))
            If L Then
                Var impulsex=(points(n1).x-points(n2).x)/L
                Var impulsey=(points(n1).y-points(n2).y)/L
                'In case of overlap circles, reset to non overlap positions
                points(n1).x=points(n2).x+(points(n1).radius*2)*impulsex
                points(n1).y=points(n2).y+(points(n1).radius*2)*impulsey
                Var impactx=points(n1).dx-points(n2).dx
                Var impacty=points(n1).dy-points(n2).dy
                Var dot=impactx*impulsex+impacty*impulsey
                points(n1).dx-=dot*impulsex
                points(n1).dy-=dot*impulsey
                points(n2).dx+=dot*impulsex
                points(n2).dy+=dot*impulsey
            End If
        Next n2
    Next n1
End Sub

  Sub check_line_collisions(LN() As Line, ball() As point)
    For z As Integer=Lbound(ball) To Ubound(ball)
        For z2 As Integer=Lbound(Ln) To Ubound(Ln)
            Dim As point closepoint
            Var seperation=segment_distance(Ln(z2).v1x,Ln(z2).v1y,Ln(z2).v2x,Ln(z2).v2y,ball(z).x,ball(z).y,closepoint.x,closepoint.y)
            If seperation<=ball(z).radius Then
                Var impactx=-ball(z).dx
                Var impacty=-ball(z).dy
                Var impulsex=(closepoint.x-ball(z).x)/seperation
                Var impulsey=(closepoint.y-ball(z).y)/seperation
                ball(z).x=closepoint.x-ball(z).radius*impulsex
                ball(z).y=closepoint.y-ball(z).radius*impulsey
                Var dv=impactx*impulsex+impacty*impulsey
                ball(z).dx+= 2*dv*impulsex
                ball(z).dy+= 2*dv*impulsey
            End If
        Next z2
    Next z
End Sub

Sub drawline(L() As line)
    for n as long=lbound(L) to ubound(L)
    Line(L(n).v1x,L(n).v1y)-(L(n).v2x,L(n).v2y),L(n).col
    next
end sub

sub drawpoints(p() as point)
    dim as long d=20
    for n as long=1 to ubound(p)
        var a=atan2(p(n).dy,p(n).dx)
    line(p(n).x,p(n).y)-(p(n).x+d*cos(a),p(n).y+d*sin(a))
    next n
end sub

Function Regulate(Byval MyFps As Long,Byref fps As Long) As Long
    Static As Double timervalue,_lastsleeptime,t3,frames
    Var t=Timer
    frames+=1
    If (t-t3)>=1 Then t3=t:fps=frames:frames=0
    Var sleeptime=_lastsleeptime+((1/myfps)-T+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    _lastsleeptime=sleeptime
    timervalue=T
    Return sleeptime
End Function

sub display(L() as line,p() as point)
    static as long fps
    For z As Integer=1 To ubound(p)
    p(z).x+=p(z).dx
    p(z).y+=p(z).dy
Next z
 check_line_collisions(L(),P())
 Check_PointCollisions(p())
    screenlock
    cls
     draw string(50,10),"Drag the white circle, to reset it, right click"
    draw string(50,50),"Framerate "&fps
    drawline(L())
    drawpoints (p())
    circle (L(7).v2x,L(7).v2y),5,,,,,f
    screenunlock
    sleep regulate(80,fps),1
end sub

#macro mouse()
Dim As Long x=mx,y=my,dx,dy
While mb = 1
    display(lines(),p())
    Getmouse mx,my,,mb
    If onscreen Then
        If mx<>x Or my<>y  Then
            dx = mx - x
            dy = my - y
            x = mx
            y = my
            lines(7).v2x=x+dx
            lines(7).v2y=y+dy
        End If
    End If
Wend
#endmacro

dim as line  Lines(1 to 8)
'the screen edges
lines(1)=Type<line>(0,0,799,0,rgb(200,0,0))    'top
lines(2)=Type<line>(799,0,799,599,rgb(200,0,0))'right
lines(3)=Type<line>(799,599,0,599,rgb(200,0,0))'bottom
lines(4)=Type<line>(0,599,0,0,rgb(200,0,0))    'left
'a box
lines(5)=Type<line>(500,300,500,500,rgb(0,200,0))
lines(6)=Type<line>(500,500,400,500,rgb(0,200,0))
lines(7)=Type<line>(400,500,400,300,rgb(0,0,200))''moveable
lines(8)=Type<line>(400,300,500,300,rgb(0,200,0))

dim as point p(1 to 4)

p(1)=Type<point>(100,200,2,-2,10)
p(2)=Type<point>(100,300,-2,2,10)
p(3)=Type<point>(100,400,2,-2,10)
p(4)=Type<point>(100,500,-2,2,10)

dim as long mx,my,mb,fps
do
    getmouse mx,my,,mb
  if mb=2 then lines(7).v2x=400: lines(7).v2y=300
  display(lines(),p())
  if incircle( Lines(7).v2x,Lines(7).v2y,10,mx,my) and mb=1 then
      mouse()
      end if
    loop until len(inkey)
  sleep
BasicCoder2
Posts: 3596
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Need help with 2d collision ASAP

Postby BasicCoder2 » May 03, 2017 9:57

dodicat wrote:For fun. Entrapment.

A potential game? I played with it for a few minutes but could only capture one which quickly escaped when I tried for another.
.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Need help with 2d collision ASAP

Postby leopardpm » May 03, 2017 16:03

BasicCoder2 wrote:
dodicat wrote:For fun. Entrapment.

A potential game? I played with it for a few minutes but could only capture one which quickly escaped when I tried for another.

LOL! it IS fun! I was able to get two... also learned that i could drag the blue line around the screen to help direct the 'bullets' where I wanted them to go... potential fun indeed!
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Need help with 2d collision ASAP

Postby thesanman112 » May 03, 2017 19:02

for each persons take, is another persons adventure!!!! lol

very cool indeed....I was going to make a whole bunch of boxes...allow user to stear arrow and try to get to a specific box for next level....as each level progress's there are more boxes. maybe smaller and more of them each time. with a grid formation making it hard to stear through...hehehe
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Need help with 2d collision ASAP

Postby thesanman112 » May 03, 2017 22:26

the building blocks for the great Atari game BREAKOUT!!!


Code: Select all


randomize timer


dim as single x,y,xd,bx(100),by(100),bs(100),bxs(100),bys(100),bc(100),bf(100)'''
xd=1
x=1:y=1

dim as integer scrw,scrh,c,tb,speed

SCRW = 640'1280
SCRH = 480'1024

'generate
speed=4
dim as integer bsx,bsy
for y=75 to 125 step 25
for x=75 to 525 step 50
    c=c+1
    bf(c)=0
    bx(c)=x
    by(c)=y
    bys(c)=20
    bxs(c)=45
   bc(c)=int(rnd*16)+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)=15


screen 12,32,2',1
screenset 1,0
x=120:y=200
do
   
    screenset 1,0
    cls
    paint (1,1),9
    if MultiKey(&H4B)  Then bx(tb+1)=bx(tb+1)-6
    If MultiKey(&H4D)  Then bx(tb+1)=bx(tb+1)+6
    if bx(tb+1)<1 then bx(tb+1)=1
    if bx(tb+1)>scrw-75 then bx(tb+1)=scrw-75

x=x+(cos(xd)*speed)
y=y+(sin(xd)*speed)

if x<1 then x=1:xd=(xd*-1)+3.1415926
if x>scrw then x=scrw:xd=(xd*-1)+3.1415926
if y<1 then y=1:xd=xd*-1
if y>scrh then y=scrh:xd=xd*-1


for c=1 to tb+1
    if bf(c)=0 then
        if x+(cos(xd)*speed)>bx(c) and x+(cos(xd)*speed)<bx(c)+bxs(c) then
            if x<bx(c) or x>bx(c)+bxs(c) then
                if y+(sin(xd)*speed)>by(c) and y+(sin(xd)*speed)<by(c)+bxs(c) then
                  xd=(xd*-1)+3.1415926
                 if c<tb+1 then bf(c)=1
                end if
            end if
        end if

        if x+(cos(xd)*speed)>bx(c) and x+(cos(xd)*speed)<bx(c)+bxs(c) then
            if x>bx(c) and x<bx(c)+bxs(c) then
                if y+(sin(xd)*speed)>by(c) and y+(sin(xd)*speed)<by(c)+bys(c) then
                  xd=xd*-1
                 if c<tb+1 then bf(c)=1
                end if
            end if
        end if
    end if
next c

for c=1 to tb+1
    if bf(c)=0 then
        line (bx(c),by(c))-(bx(c),by(c)+bys(c)),bc(c)
        line (bx(c),by(c)+bys(c))-(bx(c)+bxs(c),by(c)+bys(c)),bc(c)',100
        line (bx(c)+bxs(c),by(c)+bys(c))-(bx(c)+bxs(c),by(c)),bc(c)
        line (bx(c)+bxs(c),by(c))-(bx(c),by(c)),bc(c)
        paint(bx(c)+1,by(c)+1),bc(c)
        line (bx(c),by(c))-(bx(c),by(c)+bys(c)),15
        line (bx(c),by(c)+bys(c))-(bx(c)+bxs(c),by(c)+bys(c)),15',100
        line (bx(c)+bxs(c),by(c)+bys(c))-(bx(c)+bxs(c),by(c)),15
        line (bx(c)+bxs(c),by(c))-(bx(c),by(c)),15
    end if
next c

circle(x,y),8,15,,,,f
flip 1,0
sleep 1

loop until multikey(&H01)



Last edited by thesanman112 on May 04, 2017 0:54, edited 3 times in total.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Need help with 2d collision ASAP

Postby datwill310 » May 03, 2017 23:41

These games seem cool! I might find time to play some of them ;)!

Return to “Game Dev”

Who is online

Users browsing this forum: No registered users and 4 guests