forums

TNT Basic Forums > Programming Q&A
collisions (again)
< Last Thread     Next Thread >
Author
Thread        Post A Reply

09-08-2003 21:04

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

could someone please help me before i start to tear my hair out!
not having had any particular problems making collisions work before, i am completely stumped as to why this peice of code doesnt work

if sprite col (1,3)=3 then hitsprite=true

if hitsprite=true
lives=lives-1
sprite off 3
hitsprite=false
draw text 100,100,"hit" 'test if it actually is working
end if


if lives=0 then end

ive tried everything, took working collision routines from previous projects, run a search on the forum for collision detection and tried these examples, copied sections from the collision detection tutoral - everything, but im still stumped

so my questions are 1: am i doing something blatently wrong
and 2: does it matter particularly where the collision algorythm is placed in the code i.e. would it be better to check for collisions after the main sprite (1) has moved

sprite 1 is the caracter and sprite 3 is a bullet fired by an enemy (gun turret type job)

this is the second time in a week that ive got stuck, i think im starting to loose it!

any suggestions

thanks
charlie

09-08-2003 21:22

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

oops

forget it , i had the above code in entirely the wrong place for it to work

thanks
charlie

09-08-2003 21:48

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

I know you found your mistake, but I wanted to pint out a mistake:

if sprite col(3,1)=1 then collision=true

should be

collision=false
if sprite col(3,1)=1 then collision=true

otherwise collision will remain true even if the sprites aren't colliding anymore.

09-08-2003 21:50

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

ignore last post. I read your code too fastly :)

09-11-2003 19:53

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

aaarg!!

just as i thought id got things sorted!

my new problem is that i cant get my bullets to hit the bad guys!
the bullets are stored in an array and my enemys arent
heres my code:

if sprite col (2,bullet+i)=2
hitspriteturret=true
else if
hitspriteturret=false
end if

the enemy is sprite 2 and i is the number of the bullet in the array, with bullet representing the index no of the bullet sprite (in the graphics menu/tab/screen)

the thing is, similar lines of code have worked in other projects and i cant figure out why it doesnt here

if i write if sprite col (2,bullet+i)<>-1 it still doesnt work an sometimes it causes bullets fired by my main charater to actually loose him a life

its disheartening to get so far in a project only to be scuppered by a minor thing that seemed insignificant at the start, especially as i have never really had a problem with it before

i have checked all the posts on the forum again, and also the collision tutorial, but still no joy


any advice would be much appreciated

thanks
charlie

09-11-2003 22:21

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

Syntax!

Always check your syntax!
collidingSprite=sprite col(testSprite,collidingSprite)
If collidingSprite is colliding with testSprite, then sprite col will return collidingSprite.

quote:

if sprite col (2,bullet+i)=2


This line says "if sprite 2 is colliding with sprite 2"
And a sprite cannot collide with itself!

So, try this instead
quote:

if sprite col (bullet+i,2)=2
hitspriteturret=true
else if
hitspriteturret=false
end if


09-12-2003 16:35

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

thanks

but it still doesnt work

its most frustrating

09-12-2003 23:03

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

if the code is smaller than 500 ko you could send it to me and i could probably fix it within a few minutes or help you corrct it

09-12-2003 23:34

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

quote:

if sprite col (bullet+i,2)=2
hitspriteturret=true
else 'if
hitspriteturret=false
end if



Maybe hitSpriteTurret is not being turned to false because you wrote "else if" on the third line instead of "else".

09-13-2003 15:05

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

nope, thats still not it.

i think my code is right, but perhaps the placement of the collision detection routine is incorrect, so my question is where do you guys put the collision detection in your code, im sure that this is the problem, but just in case im being thick ill post my shooting code so you can have a look at it, it may be something to do with the way i am calling the array which holds the bullets in the collision detection part.

SHOOTING CODE

if space and spaceup=true and facingleft=false
repeat
n=random(0,49)
until bulletexists[n]=false
bulletexists[n]=true
bx[n]=x
by[n]=y
spaceup=false
end if

if not space then spaceup=true

for i=0 to 49
if bulletexists=true
bx=bx+10
sprite i,bx,by-40,bullet
if bx>800 or bx<0
sprite off i
bulletexists=false
end if
end if
if map tile ( bx/48,by /48, 0 ) =8 then sprite off i
if map tile ( bx/48,by /48, 0 ) =9 then sprite off i

next


COlLISION DETECTION AT PRESENT

if sprite col (bullet+i,2)=2
hitspriteturret=true
else
hitspriteturret=false
end if


thanks
charlie

09-14-2003 14:30

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

You declared bulletexists as an array but a few lines after you use it as a normal int.

09-14-2003 14:31

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

>>>

if bx>800 or bx<0
sprite off i
bulletexists=false
end if
end if
if map tile ( bx/48,by /48, 0 ) =8 then sprite off i
if map tile ( bx/48,by /48, 0 ) =9 then sprite off i

>>>

if the bullet hits a tile it's hidden but not deactivated.

09-14-2003 15:33

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

oddly, this is present in my code but isnt on my post(bulletexists)
especially odd when you consider i cut and pasted it onto my post

but i still cant get the two sprites to collide...

thanks
charlie

09-14-2003 15:35

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

it should look like this

if space and spaceup=true and facingleft=false
shooting=true

repeat
n=random(0,49)
until bulletexists[n]=false
bulletexists[n]=true
bx[n]=x
by[n]=y
spaceup=false
end if

if not space then spaceup=true

for i=0 to 49
if bulletexists=true
bx=bx+10
sprite i,bx,by-40,bullet
if bx>800 or bx<0
sprite off i
bulletexists=false
end if
end if
if map tile ( bx/48,by /48, 0 ) =8 then sprite off i
if map tile ( bx/48,by /48, 0 ) =9 then sprite off i

next

09-14-2003 15:38

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

oh, its cut off all the s again, how odd

Matthew, ive just noticed you offered to have a look at my code,
its a mess, but it mostly works, except the collision detection between the turret and my bullets

ill send an email to you

thanks
charlie

09-14-2003 21:28

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

I understand the problem: [ i ] means Italic! That's why the [] were not displayed and your posts were half in italic! I'm checking my mail right now. Expect an answer soon.

09-14-2003 21:40

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

I haven't figured out the problem yet but I found a mistake you have to correct NOW when you still can: I noticed something like this:

if level=1
turretX=100
turretY=250
end if
if level=2
turretX=20
turretY=500
end if
if level=3
turretX=25
turretY=30
end if


this is not bad but OUTSIDE the main loop... in your code the computer does that piece of code about 60 times per second, so imagine how slow it'll be if you have 200 levels...

TNT Basic has an object editor in the map editor. Go in the map editor, in the tools window ( if you don't see it activate it from the genesis menu ), select the second square, create an object, set a name and color for it, and place it on the map. In your code, the enemy routine should look like that:

'variable declaration
int x[map object count-1],y[map object count-1]
' this is an array. It means each onject on the map will have its own x and y variables
' ex. object1 has x[1],y[1], object2 has x[2],y[2], etc.
...
for n=0 to map object count-1 ' for repeats an action for every object
if map object type(n)=0 ' getting X-Y values from the map
x[n]=map object x(n)*tile width
y[n]=map object y(n)*tile height
next n

if map object type(n)=0 ' actions for object 0
sprite n+1,x[n],y[n],0
'sprite n+1 because each objetc must have a diferet ID and main character is probably sprite in your code#1
end if

if map object type(n)=1 ' actions for object 0
sprite n+1,x[n],y[n],3
end if
next n

09-14-2003 21:43

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

I noticed another weird thing:

tbx=tbx-8
tby=tby-4
if tbx<0 or tby<0
...
end if

the if block will ALWAYS be executed, as the condition is set TRUE right before it executes! It's like:

int bool=true
if bool=true then print "Action"

09-14-2003 21:57

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

This time I have the mistake you are searching for!

if sprite col (bullet+i,2)=2

uses i but is not in a for loop. Try using

if sprite col (2,0 to 50)>0

------------------------------------------------

don't forget to delete

if sprite col (2,bullet+o)=2 then hitspriteturret=true

in the LEFT section.

------------------------------------------------

Aother mistake:

Bullets are sprite 0 to 49 but the turret is sprite # 2

set the turret to sprite 50 or higher maybe?

09-14-2003 22:04

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

I guess you can delete the line ( around line 245 )

if not space and spaceup=false then shooting=false


I don't think I've got all problems but that should be a step in the good direction for you ;) good luck with your game and ask me again if doesn't work!

09-15-2003 16:20

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

thanks

thanks for all the advice, i hadnt thought of using objects for the turrets but ill give it a go. i had been working on an array which would hold all the turret positions for each level, but not having to do that will save me some time and allow me to work more on drawing my sprites and tilesets (i aim to have a different look on each level)

i also hadnt thought of changing the sprite no for the collisions to work

so thanks again, its encoraging to have someone help you out and point you in the right direction from time to time, especially as i learned to program in pascal which has many more features and functions built in.

thanks
charlie

09-15-2003 21:44

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

just one more thing, the object code you posted returns an array out of bounds error when i use it and i cant understand why, the array should have that no in it
it says tn = 0 to map object-1 and there are only two objects on the current map, so haw can it be out of bounds?

for tn=0 to map object count-1 ' for repeats an action for every object
if map object type(tn)=0 ' getting X-Y values from the map
tx[tn]=map object x(tn)*map tile width
ty[tn]=map object y(tn)*map tile height
end if


if map object type(tn)=0 ' actions for object 0
sprite tn+50,tx[tn],ty[tn],15
sprite tn+50 'because each objetc must have a diferet ID and main character is probably sprite in your code#1
end if

if map object type(tn)=1 ' actions for object 1
sprite tn+50,tx[tn],ty[tn],3
end if
next tn


its this line that gives the error

tx[tn]=map object x(tn)*map tile width

and i imagine that the following line would do the same

i have changed bits of the example you posted but its only variable names

tanks
charlie

09-15-2003 23:54

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

As I wrote in my example ( I hope I did :) the map object type(tn)=0/1/2 lines must be in a for loop ( identical as the previous: the one that was setting the X-Y variables )

09-16-2003 19:03

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

no, my code if pretty much the same as the example, i only changed 1 thing (aside from variable names) which was that youd written 'next n' instead of end if after the first if statement

to be honest, i cant understand whats going wrong, map object count-1 gets the number of objects in the current map, so tn can only be 0 or 1 and the array out of bounds error should only occur when its looking for a value that doesnt exist, which it isnt

if this makes any difference, the error halts the code only after tnt starts and the error appears in the output console window
although thinkig about it, it always does that for out of bounds errors

im puzzled, this code must work, but just not how i expected it to

thanks
charlie

09-16-2003 22:40

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

The most helpful command in debugging is probably print. Write print n right before the line that causes problem and you will see what the index is. The only two problems I can see are

- Maybe the array is not declared correclty
- Maybe the line isn't in a for n loop.

09-16-2003 23:04

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

Maybe you don't have a map loaded or your map doesn't have any objects in it. Then "map object count-1" gives -1, and negative numbers are out of bounds.

09-17-2003 17:51

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

i tried print, it gives the values of 0 and 1

i put the statement all through the code and it always gives the value it should give.
the code will now get past that line i printed earlier, but now it gives an out of bounds error when i try to draw sprite tn+50,tx[tn],ty[tn],15

ive tried various methods in attempting to get around this, including having different map objects that draw the same thing

if i ask it only to draw object 0, then it will, but it only doesnt work when i ask it to draw both

thanks
charlie

09-17-2003 23:35

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

Are you sure that you declared those arrays to hold more than one value? When declaring arrays, each value counts. For example,
int x[2]
would create an array you can store two values in, x[0] and x[1]

09-17-2003 23:37

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

> it gives the values of 0 and 1

But you have only ONE object ( at least in the example you sent me ) so it should only give 0. That's probably the problem.

09-18-2003 16:16

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

but i have got two objects, they are both representing the same turret, but are different objects on the map editor.

also, has any one else got an error message on a rem statement.
thanks
charlie

09-18-2003 18:52

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

thanks for all your help guys, ive finally solved my problem

it was all down to variables

i was declaring x[map object count-1] and y[map object count-1]
and then putting for n=0 to map object count-1 so i was getting 0 and -1 instead of 0 and 1, so i was taking another object away every time the code went round. seems obvious now, but i thought the problem was in the for loop.

i still dont know why i was getting an invalid variable decleration error on a remmed line though, another of lifes mysteries

thanks
charlie

09-18-2003 22:21

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

I don't see why declaring your variables like this
[map object count-1]
would cause the loop to change the number of objects. The problem is that you declared the arrays to hold [map object count-1] items, and if map object count is 2 then you are declaring your items to hold one item, which would be variable[0]. When you declare an array, you declare how many items it has, not what index it goes to, because the highest index number is one behind the amount the array can hold. So when you declare arrays, you declare them like this
[map object count] so they can have items 0 to map object count-1.

09-19-2003 16:30

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

i only tried the code that matthew posted to see what i could do with it, i litterally lifted the posted code and pasted it into my program, only the variables in the example were [map object count-1] as was the loop

i just got confused for a moment there

again thanks for the help, maybe now i can stop hassling you all

charlie

09-19-2003 19:06

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

Oooops. My mistake!

10-24-2003 00:13

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Hi,

Is there any reason why Sprite Col should only detect sprite with even number ID's?
I know the odd IDd sprites are there because:

a) I can see them.
b) My program tells me there being drawn, and in the same canvas as the even numbered sprites.
c) Object count confirms they are there.

Thanks
MapMaker

10-24-2003 04:09

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

Let's see your sprite col line

10-24-2003 04:09

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

line of code, that is

10-24-2003 14:24

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

if moveship=true and aship=false
selected = Sprite Col (0,((turn - 1)*200 + 1) to (turn*200))
set pen colour black
fill rect 405,140 to 435,155
if selected <> -1
set pen colour color
draw text 405,150,as string(selected)
end if
end if

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.