Comparing 2 types

Blitz3D Forums/Blitz3D Beginners Area/Comparing 2 types

slenkar(Posted 2003) [#1]
I have been using this to compare 2 values in different types
for blue.blue_s=each blue_s
for red.red_s=each red_s

if blue\collision = red\handle
blue\collisions=blue\collisions+1
endif
next
next


Is this really the fastest way to do it?


Beaker(Posted 2003) [#2]
Yes. You might be able to use an Exit (or 2) as well tho.


MSW(Posted 2003) [#3]
fastest way? -no

But what is it that you are trying to do here anyway?


slenkar(Posted 2003) [#4]
Im trying to do several different things in the 2 FOR NEXT loops so exit wont work unless I have a FOR NEXT loop for each process which I have noticed is slower than doing them all at once.

For example I want to get the direction an entity is facing and move the entity which it collided with in the same direction.

I have to use a type to do this so I can keep track of the movement so I can bring them to a halt (momentum,decelleration etc.)


MSW(Posted 2003) [#5]
Okay...what is blue\collision?

a "pointer" to a specific entity or some sort of value (ie. 1= moveing left, 2= moveing up, etc..)

What is red\handle?

A "pointer" (to itself)? Or some other value (1=move left, etc..)

What are the ranges of these values if they arn't pointers?


The way you have that code set up...if there were 100 blue entities and another 100 red entities....the innermost section of the loop (the if-then) would execute 10,000 times (100 * 100)

If you can reduce the values tested to something with a small range (ie. red\handle can only be 0,1,2,3,4,5,6,7,8,9...same for blue\collision) then you could greatly speed up the routine by useing arrays and such...

for example if red\handle can only be in the range of 0 to 9...during your update red routine simply "report" the instance to an array

DIM array%(9)

For red.red_s = each red_s

array(red\handle)=array(red\handle)+1

Next



then to do the thing with the blues

For blue.blue_s = each blue_s

blue\collisions = blue\collisions + array(blue\collision)

Next

Otherwise if blue\collision is a pointer

for blue.blue_s = each blue_s

red = blue\collision

if red <> null then blue\collisions = blue\collisions + 1

next


slenkar(Posted 2003) [#6]
Yes 'handle' is a pointer to itself and collision is the pointer to the entity that it collided with.

O.k.

I have

1.Bullet collisions with ships
If red\bullet_collision=blue\handle
blue\shield=blue\shield-10
endif

2.
Ship collision with ship
If red\ship_collision=blue\handle
blue\translatex=red entityyaw(red\handle)
endif

The reason why I used types:
1.
I need to know which ship fired the bullet so I can tell which type of bullet was used
2.
I need to give the ship that got hit a direction to go.The opposite direction to the other ship.

Is there a better way?
(everything works fine, it is just a bit slow)


slenkar(Posted 2003) [#7]
I tried putting 'exit' in there but it didnt improve the speed.


MSW(Posted 2003) [#8]

Yes 'handle' is a pointer to itself and collision is the pointer to the entity that it collided with.



so red\handle = itself?

If so then why loop through all the reds if the blue\collision = the exact red that collided with it?

so this:
for blue.blue_s=each blue_s
for red.red_s=each red_s

if blue\collision = red\handle
blue\collisions=blue\collisions+1
endif
next
next


could be this:
for blue.blue_s=each blue_s

if blue\collision <> NULL
 blue\collisions=blue\collisions+1
endif

next


Even then I'm not sure I understand why you have a collisions counter, unless you intend to keep track of how many times the blue instance got hit...afterall the way you have this set up, blue can only collide with one red per game update.


The reason why I used types:
1.
I need to know which ship fired the bullet so I can tell which type of bullet was used
2.
I need to give the ship that got hit a direction to go.The opposite direction to the other ship.

Is there a better way?



Give the bullet a flag to tell it what type of bullit it is, along with variables to describe it's direction...else you can end up with some funky problems if the fireing ship is freed (destroyed) before the bullet hits anything, or if the fireing ship changes direction before the bullet hits something.


slenkar(Posted 2003) [#9]
That collision counter was just a bad example

If red\bullet_collision=blue\handle
if red\bullet_type =laser
blue\shield=blue\shield-10
endif
if red\bullet_type=plasma
blue\shield=blue\shield-50
endif
endif


How would I give the bullet a 'flag'?


Who was John Galt?(Posted 2003) [#10]
to give a bullet a flag just have a field in bullet called say 'firing_ship' which can be set to 0 for red, 1 for blue, say. If red bullets can't collide with the red ship, it would be faster to have separate type for red bullet,blue bullet and you only need to check for collisions between these and the opposing ship.


MSW(Posted 2003) [#11]

How would I give the bullet a 'flag'?



Just a variable that indicates the type of bullet it is...

Type bullet

Field shottype%
Field collision%
Field X#,Y#
Field VX#,VY#

End Type


here shottype indicates the type of bullet...
so that:

1 = player basic bullet
2 = player power-up level 1
3 = player power-up level 2

4 = enemy weak type
5 = enemy medium type
6 = enemy powerfull type

then you can do a Select-Case statement to assign damage and such:

For shot.bullet = Each bullet

hit% = Shot\collision

If hit<>NULL Then

;if the bullet hits something
;hit is a pointer to the ship being hit

Select shot\shottype

 Case 1
  ;player weak shot damage
    hit\hitpoints = hit\hitpoints - 10
    delete shot
 Case 2
  ;player higher level damage
    hit\hitpoints = hit\hitpoints - 20
    delete shot
End Select
End If



X and Y are the current location of the bullet...VX and VY are the vector the bullet follows...so with each update shot\x = shot\x + shot\vx (and the same for y)...this keeps the bullet moveing onscreen...and when the bullet hits something the bullet vx and vy can be applied to the ships vx and vy inorder to "push" it in the direction that the bullet was traveling...this way when the player fires the bullet you can:

bull = new bullet

bull\shottype = player\weapon

bull\x = player\x
bull\y = player\y

bull\vx = player\vx
bull\vy = player\vy


slenkar(Posted 2003) [#12]
O.k. but to access the players hit_points you have to cycle through all the players using types dontcha?