Help with projectile collision detection
Author |
Message |
Wasabicheese
|
Posted: Mon May 25, 2009 11:52 am Post subject: Help with projectile collision detection |
|
|
Im wondering how tio work projectile collision detection. I've got collsion detection for the characters working, but the projectile collision isnt. Could anybody help me out?
"Blast" is the projectile, here's my code.
Turing: |
%Music
process BgmSound
loop
Music.PlayFile ("music.mp3")
end loop
end BgmSound
fork BgmSound
View.Set ("title:Space Survival")
var mDamage, hp, Enemyhp : int
var mypic1 : int := Pic.FileNew ("StrikerRight.bmp")
var mypic2 : int := Pic.FileNew ("Enemy.bmp")
var blast : int := Pic.FileNew ("Blast.bmp")
var CharacterX : array 1 .. 2 of int
var CharacterY : array 1 .. 2 of int
var MonsterX : array 1 .. 2 of int
var MonsterY : array 1 .. 2 of int
var blastX : array 1 .. 2 of int
var blastY : array 1 .. 2 of int
var chars : array char of boolean
var shoot : boolean := false
View.Set ("offscreenonly, graphics:650;410")
CharacterX (1) := 50 % Characters X value (Horizontal Axis)
CharacterY (1) := 50 % Characters Y value (Verticle Axis
CharacterX (2) := 0
CharacterY (2) := 0
MonsterX (1) := 550 % Monsters X value (Horizontal Axis)
MonsterY (1) := 200 % Monsters Y value (Verticle Axis)
MonsterX (2) := 0
MonsterY (2) := 0
blastX (1) := CharacterX (1) + 11
blastY (1) := CharacterY (1) + 11
blastX (2) := 0
blastY (2) := 0
hp := 500
Enemyhp := 2
const Speed := 5
MonsterX (2) := MonsterX (1) + Pic.Width (mypic2 )
MonsterY (2) := MonsterY (1) + Pic.Height (mypic2 )
blastX (2) := blastX (1) + Pic.Width (blast )
blastY (2) := blastY (1) + Pic.Height (blast )
loop
drawfillbox (1, 1, maxx, maxx, black)
drawfillbox (5, 384, hp + 5, 384, red) %Hp bars
drawfillbox (5, 385, hp + 5, 385, red) %Hp bars
drawfillbox (5, 386, hp + 5, 386, red) %Hp bars
drawfillbox (5, 387, hp + 5, 387, red) %Hp bars
drawfillbox (5, 388, hp + 5, 388, red) %Hp bars
drawfillbox (5, 389, hp + 5, 389, red) %Hp bars
drawfillbox (5, 390, hp + 5, 390, red) %Hp bars
drawfillbox (5, 391, hp + 5, 391, red) %Hp bars
drawfillbox (5, 392, hp + 5, 392, red) %Hp bars
drawfillbox (5, 393, hp + 5, 393, red) %Hp bars
drawfillbox (5, 394, hp + 5, 394, red) %Hp bars
drawfillbox (5, 395, hp + 5, 395, red) %Hp bars
drawfillbox (5, 396, hp + 5, 396, red) %Hp bars
drawfillbox (5, 397, hp + 5, 397, red) %Hp bars
drawfillbox (5, 398, hp + 5, 398, red) %Hp bars
drawfillbox (5, 399, hp + 5, 399, red) %Hp bars
%Collision Detection
if CharacterX (2) > MonsterX (1) and CharacterX (1) < MonsterX (2) and CharacterY (2) > MonsterY (1) and CharacterY (1) < MonsterY (2) and hp > 0 then
hp := hp - 2
end if
Input.KeyDown (chars )
if chars (KEY_UP_ARROW) and hp > 0 then
CharacterY (1) + = Speed
blastX (1) := CharacterX (1) + 11
blastY (1) := CharacterY (1) + 11
if CharacterX (2) > MonsterX (1) and CharacterX (1) < MonsterX (2) and CharacterY (2) > MonsterY (1) and CharacterY (1) < MonsterY (2) and hp > 0 then
hp := hp - 2
end if
CharacterY (2) := CharacterY (1) + Pic.Height (mypic1 )
if CharacterY (1) + Pic.Height (mypic1 ) >= maxy then
CharacterY (1) := maxy - Pic.Height (mypic1 )
end if
end if
if chars (KEY_RIGHT_ARROW) and hp > 0 then
CharacterX (1) + = Speed
blastX (1) := CharacterX (1) + 11
blastY (1) := CharacterY (1) + 11
if CharacterX (2) > MonsterX (1) and CharacterX (1) < MonsterX (2) and CharacterY (2) > MonsterY (1) and CharacterY (1) < MonsterY (2) and hp > 0 then
hp := hp - 2
end if
CharacterX (2) := CharacterX (1) + Pic.Width (mypic1 )
if CharacterX (1) + Pic.Width (mypic1 ) >= maxx then
CharacterX (1) := maxx - Pic.Width (mypic1 )
end if
end if
if chars (KEY_LEFT_ARROW) and hp > 0 then
CharacterX (1) - = Speed
blastX (1) := CharacterX (1) + 11
blastY (1) := CharacterY (1) + 11
if CharacterX (2) > MonsterX (1) and CharacterX (1) < MonsterX (2) and CharacterY (2) > MonsterY (1) and CharacterY (1) < MonsterY (2) and hp > 0 then
hp := hp - 2
end if
CharacterX (2) := CharacterX (1) + Pic.Width (mypic1 )
if CharacterX (1) <= 0 then
CharacterX (1) := 0
end if
end if
if chars (KEY_DOWN_ARROW) and hp > 0 then
CharacterY (1) - = Speed
blastX (1) := CharacterX (1) + 11
blastY (1) := CharacterY (1) + 11
if CharacterX (2) > MonsterX (1) and CharacterX (1) < MonsterX (2) and CharacterY (2) > MonsterY (1) and CharacterY (1) < MonsterY (2) and hp > 0 then
hp := hp - 2
end if
CharacterY (2) := CharacterY (1) + Pic.Height (mypic1 )
if CharacterY (1) <= 1 then
CharacterY (1) := 1
end if
end if
if chars (' ') then
shoot := true
end if
if shoot = true then
blastX (1) + = 10
Pic.Draw (blast, blastX (1), blastY (1), picMerge)
end if
if blastX (1) >= maxx and hp > 0 then
blastX (1) := CharacterX (1) + 11
blastY (1) := CharacterY (1) + 11
blastX (2) := 0
blastY (2) := 0
shoot := false
if blastX (2) >= MonsterX (1) and blastX (1) <= MonsterX (2) and blastY (2) >= MonsterY (1) and blastY (1) <= MonsterY (2) and hp >= 0 then
hp := hp - 2
end if
end if
if hp <= 0 then
put "You have died" %Hp System
end if
View.Set ("offscreenonly")
Pic.Draw (mypic2, MonsterX (1), MonsterY (1), picMerge) % Enemy
Pic.Draw (mypic1, CharacterX (1), CharacterY (1), picMerge) % Character
View.Update
delay (13)
cls
end loop
|
Right now if it works it should drain my characters hp. If anybody could help me out, it would be greatly appreciated.
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Kharybdis
|
Posted: Tue May 26, 2009 8:01 am Post subject: RE:Help with projectile collision detection |
|
|
Can you put ALL of your extra things (like pictures) in a zip folder and attach 'em?
|
|
|
|
|
|
Wasabicheese
|
Posted: Tue May 26, 2009 1:33 pm Post subject: Re: Help with projectile collision detection |
|
|
ok, here it is
Description: |
|
Download |
Filename: |
Summative.zip |
Filesize: |
3.51 MB |
Downloaded: |
71 Time(s) |
|
|
|
|
|
|
Kharybdis
|
Posted: Fri May 29, 2009 5:37 pm Post subject: RE:Help with projectile collision detection |
|
|
Use this to see the values every time they hit.. put it before the main loop and type 'fork lol' right as the loop starts
Turing: | process lol
put blastY (1)
put MonsterY (2)
put blastX (1)
put MonsterX (1)
put hp
delay (1000)
end lol |
You have too many 'and' statements that combine to evaluate to false, which is why the hp ain't going down.
I've also noticed that you use very many little arrays. I have no clue why you would do it this way, but since many of your arrays are the same, i suggest you use Types...
Implementation of Types in your code:
Turing: | type generalType : array 1 ..2 of int |
and you would assign each array to be like:
Turing: | var CharacterX : generalType |
anyways.. thats just tips on how to help you solve this yourself.
As for your problem...
Turing: | if blastX (1) >= maxx and hp > 0 then
blastX (1) + = CharacterX (1) + 11
blastY (1) + = CharacterY (1) + 11
blastX (2) := 0
blastY (2) := 0
shoot := false
if blastX (1) >= MonsterX (1) and blastX (2) <= MonsterX (2) and hp >= 0 then
hp := hp - 1
end if
end if |
This is how i changed your code when i looked for your error. Main thing you should see is that when the blast hits the monster, your hp will go down indefinitely unless you move your character. That's because its not fully error - trapped (i deleted part of your code that made it only work for a certain part of the screen, thus it will always do it.
Main change is that i changed the assignment statement blastX(1) := CharacterX(1) + 11 to have it add to blastX.
You see, your blastX wasn't actually changing, which was giving you an error...
Try continuing solving your problem by yourself.
|
|
|
|
|
|
|
|