Need Help With Another Game!

Blitz3D Forums/Blitz3D Beginners Area/Need Help With Another Game!

Hi Everyone,

If you wouldn't mind, I could use some help with 2D
collisions from an over head perspective.
I just can't get it to work very well.

You see, I recently decided to do a complete graphical
over haul for my Across The Land game, and changed
the view to an over head perspective.

Things are coming along quite well, except for this collision
problem.

DownLoad the Heavily Commented File at
http://hosted.filefront.com/werdnaworld

and if you have any ideas, or suggestions on how to get
the collisions working, please tell me.

The One Gnome(The Gnomeliest Of All Gnomes)

This works pretty well, except it isn't smooth when you are against the tree
AppTitle "Across The Land; Need Help With Collisions!"
Graphics 800,600,0,1
SetBuffer BackBuffer()
AutoMidHandle True
SeedRnd MilliSecs()

Global ScrollX = 0

Global ScrollY = 0

Global FieldMusic = PlayMusic("A.L Field.wav")

Type Player;The Players Type
Field x,y,hit,image,frame,TrueX,TrueY,Atk1,Atk2,Atk3,Atk4,Atk5,Atk6,Sleep
Field Paralyze,Confuse,Poison10,Poison20,Poison30,Poison40,Poison50,GP
Field UnderControl,DecoyGnome1,DecoyGnome2,DecoyGnome3,MaxHit,Item1,Item2,Item3,Item4,Item5,Item6
Field Atk1Name,Atk2Name,Atk3Name,Atk4Name,Atk5Name,Atk6Name
End Type 


Global Player.Player = New Player;Make A New Player
Player\x = 400
Player\y = 300
Player\image = LoadImage("Across The Land Gnome.bmp")
Player\frame = 0


Type Terrain;The Type For The Trees, And Other Terrain.(Such As Water, Boulders,Ect.)
Field x,y,TrueX,TrueY,image
End Type 

Global ALTreei = LoadImage("A.L Tree Tile.bmp")

Global StopLeft = 0

Global StopRight = 0

Global StopUp = 0

Global StopDown = 0

Global BoardImage = LoadImage("A.L Field BackGround.bmp")

;Make 1 Tree
MakeTree(100,300)

While Not KeyDown(1)
Cls

If Not ChannelPlaying(FieldMusic);If The Music Stops, Restart It.
FieldMusic = PlayMusic("A.L Field.wav")
End If 

TileImage BoardImage,Scrollx,ScrollY;Tile The BackGround Image
DrawImage Player\image,Player\x,Player\y,Player\Frame;Draw The Player

MovePlayer();Move The Player
DrawTree();Draw The Trees, And Check Collisions Between Tree And Player.
Flip 
Wend 
End

Function MakeTree(x,y)
Tree.Terrain = New Terrain
Tree\x = x
Tree\y = y
Tree\TrueX = x
Tree\TrueY = y
Tree\image = ALTreei
End Function 


Function DrawTree()



If KeyDown(205) 
ScrollX = ScrollX - 5
MoveRight = 1
End If 

If KeyDown(203)
MoveLeft = 1
ScrollX = ScrollX + 5
End If 

If KeyDown(200)
MoveUp = 1
ScrollY = ScrollY + 5
End If 

If KeyDown(208)
ScrollY = ScrollY - 5
MoveDown = 1
End If 







For Tree.Terrain = Each Terrain


DrawImage Tree\image,Tree\x,Tree\y
Tree\x = Scrollx + Tree\TrueX
Tree\y = Scrolly + Tree\TrueY

If ImagesOverlap(Player\image,Player\x,Player\y,Tree\image,Tree\x,Tree\y)
If ImagesCollide(Player\image,Player\x,Player\y,0,Tree\image,Tree\x,Tree\y,0)


If MoveRight = 1
ScrollX = ScrollX + 10
End If 

If MoveLeft = 1
ScrollX = ScrollX - 10
End If 

If MoveUp = 1
ScrollY = ScrollY - 10
End If 

If MoveDown = 1
ScrollY = ScrollY + 10
EndIf 



TreeHit = 1
Else
TreeHit = 0
EndIf
EndIf




;Check Collisions Between Tree And Player.This is What I need Help With.
;If You Can Come Up With A Better Way To Do This, I Would Be Truly GreatFul.
;
;Currently It Works Like This,
;When The Player Hits The Left Side Of The Tree(Tree\x-50),The Variable StopRight = 1.
;And In The Move Player Function,You Will Notice That The Player Can Only Move Right, 
;If StopRight = 0.
;So When The Player Hits The Left Edge Of The Tree, He Can No Longer Move Right.
If TreeHit = 1


;But If The Player Is Not Touching The Right Edge Of The Tree, StopRight Then = 0,
;So That He Can Move Right.
;Because OtherWise, The First Time That The Player Touched The Tree He Would No Longer
;Be Able To Move Right AT ANY TIME!. Even While Not Touching The Tree.
ElseIf TreeHit = 0

End If 


;You Can Uncomment These 3 Sections Of Code If You Want.They Work Just Like The Above, Except For
;The Right,Bottom, And Top Sides Of The Tree


;If ImagesCollide(Player\image,400,300,0,Tree\image,Tree\x+50,Tree\y,0)
;StopLeft = 1
;ElseIf Not ImagesCollide(Player\image,400,300,0,Tree\image,Tree\x+50,Tree\y,0)
;StopLeft = 0
;End If 

;If ImagesCollide(Player\image,400,300,0,Tree\image,Tree\x,Tree\y+50,0)
;StopUp = 1
;ElseIf Not ImagesCollide(Player\image,400,300,0,Tree\image,Tree\x,Tree\y+50,0)
;StopUp = 0
;End If 

;If ImagesCollide(Player\image,400,300,0,Tree\image,Tree\x+50,Tree\y-50,0)
;StopDown = 1
;ElseIf Not ImagesCollide(Player\image,400,300,0,Tree\image,Tree\x+50,Tree\y-50,0)
;StopDown = 0
;End If 

;Make Sure The Tree Stays Firmly Glued To Where It's Supposed To Be.



Next






End Function 


Function MovePlayer()
;Scroll The Screen In The Appropiate Direction While The Player Is Holding Down One Of The Arrow Keys.



End Function 


Hum...
Very close KillerX,
Certainly better than what I had.
Still not quite there yet.

If you, or anyone else, can maybe come up with a variation
on this, that would probable work.

So thanks for your help, and I'll play around with the code myself, and see what I can do.

The One Gnome(Whos game is about 50% complete.
Most of the coding is done, but I'm completly lacking in
graphics)

Optimised. Still needs a bit of improvement.
AppTitle "Across The Land; Need Help With Collisions!"
Graphics 800,600,0,1
SetBuffer BackBuffer()
AutoMidHandle True
SeedRnd MilliSecs()

Global ScrollX = 0
Global IHitATree = 0
Global ScrollY = 0
Global MoveRight
Global MoveUp
Global MoveDown
Global MoveLeft

Global FieldMusic = PlayMusic("A.L Field.wav")

Type Player;The Players Type
Field x,y,hit,image,frame,TrueX,TrueY,Atk1,Atk2,Atk3,Atk4,Atk5,Atk6,Sleep
Field Paralyze,Confuse,Poison10,Poison20,Poison30,Poison40,Poison50,GP
Field UnderControl,DecoyGnome1,DecoyGnome2,DecoyGnome3,MaxHit,Item1,Item2,Item3,Item4,Item5,Item6
Field Atk1Name,Atk2Name,Atk3Name,Atk4Name,Atk5Name,Atk6Name
End Type 


Global Player.Player = New Player;Make A New Player
Player\x = 400
Player\y = 300
Player\image = LoadImage("Across The Land Gnome.bmp")
Player\frame = 0


Type Terrain;The Type For The Trees, And Other Terrain.(Such As Water, Boulders,Ect.)
Field x,y,TrueX,TrueY,image
End Type 

Global ALTreei = LoadImage("A.L Tree Tile.bmp")

Global StopLeft = 0

Global StopRight = 0

Global StopUp = 0

Global StopDown = 0

Global BoardImage = LoadImage("A.L Field BackGround.bmp")

;Make 1 Tree
MakeTree(100,300)

While Not KeyDown(1)
Cls

If Not ChannelPlaying(FieldMusic);If The Music Stops, Restart It.
FieldMusic = PlayMusic("A.L Field.wav")
End If 

TileImage BoardImage,Scrollx,ScrollY;Tile The BackGround Image
DrawImage Player\image,Player\x,Player\y,Player\Frame;Draw The Player

MovePlayer();Move The Player
DrawTree();Draw The Trees, And Check Collisions Between Tree And Player.
Flip 
Wend 
End

Function MakeTree(x,y)
Tree.Terrain = New Terrain
Tree\x = x
Tree\y = y
Tree\TrueX = x
Tree\TrueY = y
Tree\image = ALTreei
End Function 


Function DrawTree()


If IHitATree = 1
If MoveRight = 1
ScrollX = ScrollX + 5
End If 

If MoveLeft = 1
ScrollX = ScrollX - 5
End If 

If MoveUp = 1
ScrollY = ScrollY - 5
End If 

If MoveDown = 1
ScrollY = ScrollY + 5
EndIf
IHitATree = 0 
EndIf



If KeyDown(205) 
ScrollX = ScrollX - 5
MoveRight = 1
Else
MoveRight = 0
End If 

If KeyDown(203)
MoveLeft = 1
ScrollX = ScrollX + 5
Else
MoveLeft = 0
End If 

If KeyDown(200)
MoveUp = 1
ScrollY = ScrollY + 5
Else
MoveUp = 0
End If 

If KeyDown(208)
ScrollY = ScrollY - 5
MoveDown = 1
Else
MoveDown = 0
End If 







For Tree.Terrain = Each Terrain


DrawImage Tree\image,Tree\x,Tree\y
Tree\x = Scrollx + Tree\TrueX
Tree\y = Scrolly + Tree\TrueY

If ImagesOverlap(Player\image,Player\x,Player\y,Tree\image,Tree\x,Tree\y)
If ImagesCollide(Player\image,Player\x,Player\y,0,Tree\image,Tree\x,Tree\y,0)


IHitATree = 1



TreeHit = 1
Else
TreeHit = 0

EndIf
EndIf




;Check Collisions Between Tree And Player.This is What I need Help With.
;If You Can Come Up With A Better Way To Do This, I Would Be Truly GreatFul.
;
;Currently It Works Like This,
;When The Player Hits The Left Side Of The Tree(Tree\x-50),The Variable StopRight = 1.
;And In The Move Player Function,You Will Notice That The Player Can Only Move Right, 
;If StopRight = 0.
;So When The Player Hits The Left Edge Of The Tree, He Can No Longer Move Right.
If TreeHit = 1


;But If The Player Is Not Touching The Right Edge Of The Tree, StopRight Then = 0,
;So That He Can Move Right.
;Because OtherWise, The First Time That The Player Touched The Tree He Would No Longer
;Be Able To Move Right AT ANY TIME!. Even While Not Touching The Tree.
ElseIf TreeHit = 0

End If 


;You Can Uncomment These 3 Sections Of Code If You Want.They Work Just Like The Above, Except For
;The Right,Bottom, And Top Sides Of The Tree


;If ImagesCollide(Player\image,400,300,0,Tree\image,Tree\x+50,Tree\y,0)
;StopLeft = 1
;ElseIf Not ImagesCollide(Player\image,400,300,0,Tree\image,Tree\x+50,Tree\y,0)
;StopLeft = 0
;End If 

;If ImagesCollide(Player\image,400,300,0,Tree\image,Tree\x,Tree\y+50,0)
;StopUp = 1
;ElseIf Not ImagesCollide(Player\image,400,300,0,Tree\image,Tree\x,Tree\y+50,0)
;StopUp = 0
;End If 

;If ImagesCollide(Player\image,400,300,0,Tree\image,Tree\x+50,Tree\y-50,0)
;StopDown = 1
;ElseIf Not ImagesCollide(Player\image,400,300,0,Tree\image,Tree\x+50,Tree\y-50,0)
;StopDown = 0
;End If 

;Make Sure The Tree Stays Firmly Glued To Where It's Supposed To Be.



Next






End Function 


Function MovePlayer()
;Scroll The Screen In The Appropiate Direction While The Player Is Holding Down One Of The Arrow Keys.



End Function 


If Not ImagesCollide(Player\image,Player\x,Player\y,0,Tree\image,Tree\x,Tree\y)
StopRight = 0
endif

Clean,and simple.

Optimized Indeed,
Thanks Killer.

This does still need some improvement, but it is certainly
good enough so I can continue work on the game and
simply come back and perfect it later.

And thanks for your help too Cp, but I think Killers way
is the way to go.

If either of you can come up with an even better version,
please post it. And thanks again for the help.

The One Gnome(Who traveled across the land)

Why not only check for collisions? You don't need to find out whether it's a left collision or not.

Record the players x and y co-ords.
Check for movement control moving the player along the x-axis.
If movement causes collision, restore old x value.
Check for movement control moving the player along the y-axis.
If movement cause collision, restore old y value.

Draw_everything()

DONE

Just make sure you draw everything last. No movement should be done, or corrected AFTER the drawing is done, as you will see the effects of positions moving due to collisions.

The players X and Y coordinates never change for this game,
They remain at 400, and 300.

I use ScrollX and ScrollY to move things instead.

But I get what you mean, and I think that I can apply that to ScrollX
and ScrollY.

So Thanks for the suggestion Ross C:)

The One Gnome(Who will save the land from evil)

;Update
The above problem has been fixed, so major thanks guys.

But LOL, I now have another collision problem.

I have begun work on the games first town, and am having
some problems with the inhabitants.

They can collide with the town walls just fine,(using a slight variation
of KillerX's collision code) but they sometimes get stuck on the walls
edges and can't get back out.

I have commented the new additions to my code, and if someone can
help fix it so they stop getting stuck on the walls I would certainly be
grateful.

Over Mountain, Hill, and lee,
Over the desert sand,
The one gnome cries out joyfully;
"I'm wandering across the land!"

You'll want to use Loadanimimage() once for the entire potato(all four sides).

Ok, thanks for the tip.

Will that help with the collisions?

For the evil wizard Bruner DarkHeart,
has strecthed his oppressive hand,
and sucked all joy and cheerfullness
from our once happy land!

It might help.

Got it,
I'll try it out.
Thanks again Killer.

And the fairie of joy and good cheer
has been put into an iron barred cage,
the gnomes are all fearful and downcast,
because of this odious mage!