Simple 2D math and primitive collision detection

Started by LemonWizard, May 10, 2009, 09:09:31 PM

Previous topic - Next topic

LemonWizard

Hey everyone, I just read over the some other posts in the forum and it seems to me that alot of people who hang out here have a pretty good understanding of mathematics for use in 2d and 3d stuff.


Ok my first problem is that I want to create a free moving 2d sidescrolling engine.

I want the map system of it to work about the same way as my 2d tile engine system.
But I want to allow for free moving platforms (That aren't limited to a grid), and same for the player.

I was experimenting yesterday morning with 2d movement, and two months ago I was trying to make a 2d demo with velocity, height, falling speed, and air time (time you can sustain air born before you begin to fall)

My key issue at the moment seems to be the collision.


I would sample some code but I'm not on the pc I usually use with pb right now so ...


I'll explain it like this.

I have a square tile that's 32 by 32 pixels.
The entire ground plane is sh-32
(sh is screen height)

My collision check works just fine.
if py (player's y) is <sh-64 (because I want the ground plane to be 32 pixels high from the bottom of the screen) then plat=0 (the player begins falling)

if py=sh-64 then plat=1 (Plat is a variable I'm using to tell the engine that the player is standing on a platform)

if plat=0 then py=py+1

if upkey()
if plat=1 (the player can only jump from something solid)
jump=1 (jump =1 means player is jumping is true)


I'd say more but I'd rather just post the code..


ok I have the code right here:

PlayBASIC Code: [Select]
; PROJECT : SideScroller
; AUTHOR : LemonWizard
; CREATED : 5/10/2009
; EDITED : 5/10/2009
; ---------------------------------------------------------------------
sw=getscreenwidth()
sh=getscreenheight()

player=newimage(32, 32)
rendertoimage player
cls rgb(0, 0, 255)

solid=newimage(32, 32)
rendertoimage solid
cls rgb(0, 255, 0)

screen=newimage(sw, sh)
rendertoimage screen
cls rgb(0,0,0)

solidx=3
solidy=3
jump=0
jumptime=0
px=10*32
py=sh-64
plat=0 ;whether or not player is standing on a solid

goto main


inp:


if leftkey() then px=px-1
if rightkey() then px=px+1


if upkey()
if plat=1
if jump=0
jump=1
jumptime=100
plat=0
endif
endif
endif

if jump=1
jumptime=jumptime-1
py=py-1
endif

if jump=1
if jumptime <1
jump=0
jumptime=0
endif
endif

return



main:
gosub inp
if py=sh-64 then plat=1
if py <15*32 and py >14*32
if px <3*32 and px >2*32
plat=1
else
plat=0
endif
endif

if plat=0
if jump=0
py=py+1
endif
endif

rendertoimage screen
cls rgb(0,0,0)
for temp=1 to sw/32
drawimage solid, (temp-1)*32, sh-32, 0
next temp
drawimage solid, 3*32, 15*32, 0


drawimage player, px, py , 0


rendertoscreen
drawimage screen, 0, 0, 0
sync
goto main





stevmjon

to lemonwizard

thought i would have a crack at this, so i looked at your code making only minor adjustments. i left your code as intact as i could.

i made it so the player jumps cleanly onto the 'floating' ground.

see the zip file.

  hope it helps,  stevmjon


p.s.  if you like i can add a jump/fall sequence using cos(angle)*jump. this makes a smooth jump arc.
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

LemonWizard