Boat to land collision

Blitz3D Forums/Blitz3D Beginners Area/Boat to land collision

Jeeps(Posted 2007) [#1]
Hi to all.

I need some pointers on setting up collision between irregular shaped land png image and a boat. This is a 2d game.

Islands I want the ship to get stuck in the shallow water if it gets to close to land and be able to back out if it hit land at slower speed at reverse angle and ship rotation it got stuck at. Faster speeds the ship would take damage and or get pernamently stuck.

On Icebergs it will take damage points the faster it hits and bounce off the Iceberg.

I have tried to just reverse the velocity by multipling by
- 1. If I hit an irregular piece of the land it like sucks the ship farther into the land and destroys it very fast. I have converted values to integers for draw image and images collide command.

I was thinking of keeping track of a series of x,y coordnates, velocity and ship rotation angle to back it out the way it came in. I'm sure there are better ideas

Please help.
Thanks


b32(Posted 2007) [#2]
I'm not sure exactly what you want, but the boat/image collisions reminded me of the bouncing ball program from this thread: http://www.blitzbasic.com/Community/posts.php?topic=65410
I mean Stevie's second post, it shows how to bounce a ball to an image. Hopefully it helps.


Buggy(Posted 2007) [#3]
It would hope if we knew whether your game is actual 2D, like with "Drawimage," or just 2D in 3D.


Jeeps(Posted 2007) [#4]
B32 Thanks for the link. I can use the angle deflection code for collision with Icebergs. I'm not sure if I can use collision with the color since the boat is cigar shaped it would not be as easy as a ball which is a circle. I dont know how I could set pixels around the outside of the boat to be read at each angle?

Currently I am using a png digital image of a battleship and a png background image for the islands. Imagescollide command I use to detect the collision.

Buggy I am using all 2d commands and graphics in Blitz3d. I wanted to learn the 2d stuff real good and then move into 3d stuff later. Thanks for asking.


Stevie G(Posted 2007) [#5]
Here's how to get the points, should be easy enough to implement into my bounce code ..

Graphics 640,480,16,1

ShipAng = 0
ShipX# = 320
ShipY# = 240
ShipWidth# = 5
ShipLength# = 15
SetBuffer BackBuffer()

Repeat

	Cls

	ShipAng = ( ShipAng + 1 ) Mod 360

	For CheckAng = 0 To 330 Step 30
	
		Lx# = ShipWidth * Cos( CheckAng )
		Ly# = ShipLength * Sin( CheckAng )
		Nx# = Lx * Cos( ShipAng ) - Ly * Sin( ShipAng )
		Ny# = Ly * Cos( ShipAng ) + Lx * Sin( ShipAng )
		Plot ShipX + Nx , ShipY + Ny
		
	Next

	Flip
	
Until KeyDown(1)	


Alternatively, use 3 circles , one at the bow and stern and another in the middle. If you constrain the bow and stern to be within a certain distance then you could also simulate rotation in the collisions.

Stevie


Jeeps(Posted 2007) [#6]
@Stevie G, Thanks for the code! I am still playing around with it to see how it works. This program creates an oval plot pattern around the image. I don't know what the difference would be to create 3 circles. Would they each have a circle plot pattern around them like separate bouncing balls, the first would react to the collision and the other would keep going till pulled by the 1st ball to cause rotation?


Stevie G(Posted 2007) [#7]

Would they each have a circle plot pattern around them like separate bouncing balls, the first would react to the collision and the other would keep going till pulled by the 1st ball to cause rotation?


Sort of. You'd have to align the ship rotation based on the angle from the stern to the bow. If I have time I could knock up an example over the weekend?

Stevie


Jeeps(Posted 2007) [#8]
I'm having problems or don't understand the values of readpixel() is supposed to return

From the command help it is supposed to return a RGB value, like 255,255,255 for black, but I get other numbers like -1 for this one.

I would like to compare an RGB value against the value of readpixel() to see if there is a collision with one color. this will allow me to run different operations when colliding with different color pixels.

Thanks!


b32(Posted 2007) [#9]
It has to do with the conversion of a 3-byte value to an integer. The integer has 4 bytes, and one bit is the sign (+/-) of the number. These two data types are not compatible, that is why ReadPixel returns these strange results.
This code shows how to convert the results from ReadPixel back into the original value:

However, when the display is set to 16-bit, the ReadPixel results are different from 32-bit modes.
An option is, to write the RGB color to the screen as well, and then reading it back:
Function Convert(R, G, B)

        ;convert RGB to integer
	col = R + G Shl 8 + B Shl 16
        ;store old pixel
	old = ReadPixel(0, 0)
        ;write RGB integer to (0,0)
	WritePixel 0, 0, col
        ;read color back from screen
	col = ReadPixel(0, 0)
        ;restore old pixel
	WritePixel 0, 0, old
        ;return converted value
	Return col
	
End Function

That way the code is automatically adjusted to the screen's bitdepth.
Or alternatively, if you know for certain where a yellow pixel is, use ReadPixel on that location and store the value in a variable "yellow". Then, during the collision code, check each ReadPixel against this variable.


Jeeps(Posted 2007) [#10]
@B32 Thanks for the info and code snippets! Looks like it would be easier to read a color pixel on the screen into a varible. What would happen if and image moved over this pixel that was being read into a varible?

I know if this effected read pixel value you could make diiferent colored pixels on edge of screen. I would like to have images moving off all sides of the screen. Would I setup multiple screen buffers or some other trick?

Thanks!


b32(Posted 2007) [#11]
Ah, sorry it took me so long to notice your post. That could be a good solution as well. Use ReadPixel(x, y, ImageBuffer(image)) to read a pixel from the original image.


Jeeps(Posted 2007) [#12]
@B32 Thanks alot! I didn't know you could ReadPixel() from an image. There's just not enough info in the command help. I thought I could only ReadPixel from back and front buffers.