Collisions. elp.

Blitz3D Forums/Blitz3D Programming/Collisions. elp.

Paul "Taiphoz"(Posted 2004) [#1]
OK here is the problem.

Im moving a bullet. xvel=5.2 and yvel=-4.6 so the bullet should be moving up to the right really fast.

My problem is that the distance the bullet is moving is to far to correctly detect a collision between a small alien.

This is 2D.

So what I need is a way to move the bullet at that speed and yet have it hit exact pixel perfect on the alien even if the bullet jumps right over it in one of its increments.

Ideas please ??


TeraBit(Posted 2004) [#2]
How big is a bullet? You could trace a line between the start and end points of that frame and test for a collision at each point.


Paul "Taiphoz"(Posted 2004) [#3]
TeraBit . could you possibly explain that a bit more or provide an example. of how to do that.

Say a bullet is about 16*16 circle. and the alien is about 16*32 rough size.

The problem is that some times the bullet can pass over the alien. so I need a way to keep moving the bullet as fast as I am now but at the same time get really good collisions.


simonh(Posted 2004) [#4]
Have your game logic update at ~300 per second, that way you can move your bullet in smaller steps but still have it move at the same speed.


Paul "Taiphoz"(Posted 2004) [#5]
At the moment thats not practicle Si ..

I'm interested in what TeraBit has to say though as this waould be easier to implement in the current code.


Paul "Taiphoz"(Posted 2004) [#6]
Graphics 400,400,16,2
SetBuffer BackBuffer()
Cls


Type bullets
	Field x,y
	Field vx,vy
	Field image
End Type

Global bullet

Global alien


bullet	=CreateImage(16,16)
alien 	=CreateImage(30,16)

SetBuffer ImageBuffer(bullet)
Color 255,0,0
Rect 0,0,16,16

SetBuffer ImageBuffer(alien)
Color 0,255,0
Rect 0,0,30,16


SetBuffer BackBuffer()



Cls


While Not KeyHit(1)
Cls
	update
	render
Flip
Wend


Function render()
	DrawImage alien,300,300,0
	draw.bullets()
End Function


Function update()
	If KeyHit(2) Then create.bullet()
	If KeyHit(3) Then create1.bullet()
	collision.bullets()
	control.bullets()
End Function

Function draw.bullets()
	For draw.bullets = Each bullets
		DrawImage draw\image,draw\x,draw\y,0
	Next
End Function

Function collision.bullets()
	For collision.bullets = Each bullets
		If ImagesCollide(collision\image,collision\x,collision\y,0,alien,300,300,0)=1
			Stop
		End If
	Next
End Function

Function control.bullets()
	For update.bullets = Each bullets
		update\x=update\x+update\vx
		update\y=update\y+update\vy
		If update\x>400 And update\y>400
			FreeImage update\image
			Delete update
		End If
	Next
End Function

Function create1.bullets()
	b.bullets=New bullets
		b\x=100
		b\y=100
		b\vx=1
		b\vy=1
		b\image=CopyImage(bullet)
End Function


Function create.bullets()
	b.bullets=New bullets
		b\x=100
		b\y=100
		b\vx=60
		b\vy=60
		b\image=CopyImage(bullet)
End Function


This demonstrates what im on about. Button 1 Fires a bullet, of the speed that I need to trap and button 2 firs off a bullet of the speed I get the result I want from.
lol if you follow me ..


Stevie G(Posted 2004) [#7]
This any good - just replace the collision function with this ... oh and make sure the bullet type fields vx,vy,x,y are floats. To get it more accurate increase the 'increment' variable by less - in your case .06. This will be slow so suggest only using the increment thing when you're closer to the target.

[code]

Function collision.bullets()
For collision.bullets = Each bullets

collided = False
increment# = 0
While ( increment < 1.0 ) And ( collided = False )
increment = increment + 0.1
check_x# = collision\x + increment * collision\vx
check_y# = collision\y + increment * collision\vy
collided = ImagesCollide(collision\image,check_x,check_y,0,alien,300,300,0)
Wend

If collided = True Stop

Next
End Function

[code\]


Paul "Taiphoz"(Posted 2004) [#8]
Yeah thats a lot better than what I was getting. I need to tweak it a bit though but thanks man.