Main Menu

World Closest POint

Started by kevin, July 26, 2005, 11:58:06 PM

Previous topic - Next topic

kevin

This examples show the previous rebound demo hacked to also calc and display the closest World impact points to the sprites axis point.

The command that does this is GetWorldClosestPoint.  This command will mostly be useful in AI, or rolling your own custom collision solutions.
 
GetWorldClosestPoint checks a region within a selected world.   Within this region it's looking to find the closest wall point along a walls to your selected point.
 
The demo is running 250 sprites rebounding and checking a 100*100 unit region around them for the closest points.  The function returns a false if nothing is inside the region your selected.  So your character could get some idea of it's environment perhaps... It's up to you..



PlayBASIC Code: [Select]
  LoadIMage "..\..\..\gfx\Bubble_64x64.bmp",1
Preparefximage 1

Makebitmapfont 1,$ffffff

WorldWidth=3000
WorldHeight=3000

; create a Camera
CreateCamera 1
Limitcamera 1,true
LimitCameraBounds 1,0,0,WorldWidth+1,WorldHeight+1



Restart:

if GetWorldStatus(2) then deleteworld 2

; Create world
CreateWorld 2
CaptureToWorld 2

; ==============================================
; draw a series of boarder line for this world
; ==============================================
Dot 0,0
line 0,0,worldwidth,0
line worldwidth,0,worldwidth,worldheight
line worldwidth,worldheight,0,worldheight
line 0,worldheight,0,0

; draw a series of polygon shaped obejct into the world
for lp=1 to rndrange(80,120)
xpos#=50+rnd(worldwidth-100)
zpos#=50+rnd(worldheight-100)
size=rndrange(30,100)
angle=rnd(359)
Make_Convex(rndrange(3,15),xpos#,zpos#,Size,angle)
next lp

; Partition The world up into 32 by 32 cells
PartitionWorld 2,48

; Tell PB to return to Immediate drawing mode
DrawGfxImmediate



; Create A batch of Ball Sprites



MaxBalls=250

Type tBalls
Angle#
Speed#
SpriteINdex
EndType

Dim Balls(MaxBalls) as tBalls



For ThisBall=1 to MaxBalls

Balls(Thisball).speed#=rndrange(1,6)
Balls(ThisBall).Angle#=rnd(359)

; Create the Sprite
CreateSprite ThisBall

; Assign this sprite image #1
SpriteImage ThisBall,1

; Set the Sprites to Rotated Draw mode
SpriteDrawMode ThisBall,2

; Center the Sprites handle for this image
Centerspritehandle ThisBall

; Set up this sprites collision
SpriteCollision Thisball,True

; Set Sprite to have STICKY World Collision
SpriteCollisionMode ThisBall, 5

; Set sprite to collide with world #2
SpriteCollisionWorld ThisBall,2

Scale#=0.5+(rnd(100)/100.0)

; Set the Size of the Collision Radius
SpriteCollisionRadius ThisBall,31*Scale#

ScaleSprite THisBall,Scale#


; Randomly POsition this Sprite within the world
Repeat
X=rnd(Worldwidth)
y=rnd(WorldHeight)
positionSpriteXyz ThisBall,X,y,50
MoveSprite ThisBall,1,1
until GetSpriteWorldIMpact(ThisBall)=False
next



; Force Sprite #1 to higher speed
Balls(1).speed#=rndrange(3,6)
; Balls(1).speed#=1
positionSpriteXyz 1,X,y,5


; statrt of DO/Loop
Do
; capture to scene and grab the world info
CaptureToScene
ClsScene


; Move All the sprites
For ThisBall=1 to MaxBalls
Angle#=Balls(ThisBall).Angle#
Speed#=Balls(ThisBall).Speed#
MoveSprite ThisBall,cosradius(angle#,speed#),sinradius(angle#,speed#)

; Set Sprites drw mode to rotated (2)
SpriteDrawMode ThisBall,2

; Check this sprite hit the world during the last move
If GetSpriteWorldIMpact(ThisBall)=True

; Get the Walls Normals
nx#=getnormalx#(0)
ny#=getnormaly#(0)

WallAngle#=AtanFull(ny#,nx#)
Angle#=wrapangle(angle#,180)
Angle#=WrapAngle(WallAngle#,WallAngle#-Angle#)

; Set the balls/sprites new direction
Balls(ThisBall).Angle#=Angle#

; Set sprite to AlphaADD with a random colour so it flashes
SpriteDrawMode ThisBall,2+4096
Login required to view complete source code