Author Topic: bouncing points inside polygon  (Read 2227 times)

JSM

  • Guest
bouncing points inside polygon
« on: January 11, 2013, 08:43:19 PM »
Here is an example showing some points bouncing around inside and outside a polygon. the functions may be useful for something like a pee wee golf game? the polygon was exported from the polygon editor program, i do not remember who wrote it.

Code: [Select]
rem poly bounce.
rem by jsm.

import "Math.lib"
import "Speed.lib"

rem exported from polygon editor.
poly[][] = [[-140,160], [-80,40], [-140,-60], [-20,-60], [-40,-160], [120,-140], [60,20], [180,100], [60,160], [20,60]]

rem convert to screen coords and make a float duplicate.
points = sizeof(poly, 0)
polyf#[points][4]
for i = 0 to points - 1
poly[i][0] = 640*(poly[i][0] + 200)/400
poly[i][1] = 480*(poly[i][1] + 200)/400
polyf[i][0] = float(poly[i][0])
polyf[i][1] = float(poly[i][1])
next
rem vectors between points in closed polygon.
for i = 0 to points - 1
polyf[i][2] = float(poly[(i + 1)%points][0] - poly[i][0])
polyf[i][3] = float(poly[(i + 1)%points][1] - poly[i][1])
next

rem init a couple of bouncers.
bouncers?[500]
for i = 0 to sizeof(bouncers) - 1
bouncers[i] = NewBouncer(float(rnd(640)), float(rnd(480)), float(rnd(360)), 8.0*float(rnd(75) + 25)/100.0)
next

rem uh, return a new bouncer.
function NewBouncer?(x#, y#, angle#, spd#)
b?.x# = x
b.y# = y
b.dx# = cos(angle)
b.dy# = sin(angle)
b.spd# = spd
return b
endfunc

set redraw off
do
rem move bouncers.
for i = 0 to sizeof(bouncers, 0) - 1
proc MoveBouncer bouncers[i], polyf
next

rem draw.
set color 0, 0, 0
cls

set color 255, 255, 255
proc DrawPoly poly

set color 255, 255, 0
for i = 0 to sizeof(bouncers, 0) - 1
draw pixel int(bouncers[i].x#), int(bouncers[i].y#)
next

redraw
proc SPD_HoldFrame 60
until keydown(27, true)

rem move bouncers. hope you know your linear algebra :)
procedure MoveBouncer(&bouncer?, &p#[][])
px# = bouncer.x#
py# = bouncer.y#
dx# = bouncer.dx#*bouncer.spd#
dy# = bouncer.dy#*bouncer.spd#
pointCount = sizeof(p, 0)
prevHit = -1
do
hadCollision = false
minParam# = 2.0
minPoint = -1
for i = 0 to pointCount - 1
if i <> prevHit
param# = RayLineIntersection(px, py, dx, dy, p[i][0], p[i][1], p[i][2], p[i][3])
if param >= 0.0 and param < minParam
minParam = param
minPoint = i
endif
endif
next
if minParam <= 1.0
hadCollision = true
prevHit = minPoint
px = px + dx*minParam
py = py + dy*minParam
r#[] = [dx, dy]
l#[] = [p[minPoint][2], p[minPoint][3]]
v#[] = V_ReflectGet(r, l)
proc V_Scale v, (minParam)
dx = v[0]
dy = v[1]
else
px = px + dx
py = py + dy
endif
until not hadCollision

bouncer.x# = px#
bouncer.y# = py#
k# = 1.0/sqr(dx*dx + dy*dy)
bouncer.dx# = dx*k
bouncer.dy# = dy*k
endproc

rem detect intersection between positive ray and line segment.
rem if there is an intersection, the parameter of the
rem intersection for the ray is returned, else -1.
function RayLineIntersection#(rx#, ry#, rdx#, rdy#, lx#, ly#, ldx#, ldy#)
dx# = rx - lx
dy# = ry - ly
d# = rdx*ldy - rdy*ldx

if d = 0.0 then return -1.0

u# = (dy*rdx - dx*rdy)/d
if u < 0.0 or u > 1.0 then return -1.0

v# = (dy*ldx - dx*ldy)/d
if v < 0.0 then return -1.0

return v
endfunc

procedure DrawPoly(&poly[][])
s = sizeof(poly, 0)
for i = 0 to s - 1
draw line poly[i][0], poly[i][1], poly[(i + 1)%s][0], poly[(i + 1)%s][1]
next
endproc

« Last Edit: January 11, 2013, 08:45:08 PM by JSM »

Mopz

  • Guest
Re: bouncing points inside polygon
« Reply #1 on: January 11, 2013, 09:48:41 PM »
I was going to ask about the point in moving points around. I mean, if I wanted to write a mini golf game I'd atleast need to use a circle (ball) rather than a point. But ... then I figured you can probably embedd the radius of the ball in the polygon used for collision testing rather than writing a more complicated collision test, right?..

Nice anyway :)

Edit: If I did, I didn't mean to sound rude  :-\  Sorry!

Edit 2: Yes, I did sound very rude, sorry again. But I know John in real life and we argue a lot about code for fun. I just needed to clearify this, after a bottle of wine, so that none of you think I'm a schmuck.
« Last Edit: January 12, 2013, 12:09:30 AM by Mopz »

JSM

  • Guest
Re: bouncing points inside polygon
« Reply #2 on: January 12, 2013, 12:14:08 AM »
no offense from my side :) drinking beer, working on a 3d pong with the Soft3D lib i posted. Drunk, yes.