PDA

View Full Version : Sprite Engine Collision, cmRadius , always colliding



robert83
14-09-2014, 03:08 PM
Hello everyone ,

It's been a while, anyway I decided I go another round with the sprite engine and asphyre :) , and finally I did my breakthrough , smooth scrolling is working now, I was scrolling the world in a way in which it was not meant to be scrolled.
Once I'm done with my latest findings, I will collect my documents and do a more detailed step-by-step guide thing.

Anyway the problem I am facing now is this :

OnFormCreate after I initialize the Apshyre Device , I create the Player Ship, then I create a test enemy , that is only moving left and right ... like this


Enemy := TEnemy.Create(SpriteEngine);
with TEnemy(Enemy) do
begin
ImageName := 'Enemy.image';
Z := 40;
X := 100;
y := 4350;
horz := 1;
CollideMethod := cmRadius;
CollideRadius := 5;
DoCollision := true;
end;


** the Enemy size is 32x64 , I guess the collide radius means that it checks Enemy.X,Enemy.Y +5 radius (circle) for collision...

Now using PlayerSprite.Move I move the ship , I can shoot (create Bullet sprites) like this :



procedure TPlayerSprite.Move(const MoveCount: Single);
var Bullet : TBullet;
begin
// here we move the ship and the world with world.y
inherited;
if Engine.WorldY > 0 then
Y := Y - 0.5; // we are moving constantly ahead till we reach the end
if Form1.Keyboard.Key[203] then
begin
if X > 0 then
X := X - 3
else
X := 0; // we put the ship to the edge of the screen
end;
if Form1.Keyboard.Key[205] then
begin
if X < 608 then
X := X + 3
else
X := 608; // we put the ship to the edge of the screen
end;
if fireRate > 1 then dec(fireRate);
if ((Form1.Keyboard.Key[29]) or (Form1.Keyboard.Key[57])) and (firerate=1) then
begin
Bullet := TBullet.Create(Form1.SpriteEngine);
Bullet.CollideMethod := cmRadius;
Bullet.CollideRadius := 5;
Bullet.DoCollision:=true;
Bullet.MoveSpeed := 7;
Bullet.DoCenter := False;
Bullet.X := PlayerSprite.X;
Bullet.Y := PlayerSprite.Y + 10;
Bullet.Z := PlayerSprite.Z;
Bullet.ImageName := 'Bullet.image';
firerate:=20;
end;
Engine.WorldY := Y - (480-64);
end;



And then I move the Bullet and the Enemy sprite and check for collision like this :


procedure TEnemy.Move( const MoveCount: Single);
begin
inherited;
y:=Engine.WorldY+64;
x:=x+horz;
if x > 640 then horz:=horz*-1;
if x < 0 then horz:=horz*-1;
end;

procedure TBullet.Move(const MoveCount: Single);
begin
inherited;
Y := Y - FMoveSpeed;
if Y < WorldY then
begin
Dead;
Visible := False;
end;
Collision;
end;

procedure TBullet.OnCollision(const Sprite: TSprite);
begin
if (Sprite is TEnemy) then
begin
Dead;
end;
end;


The problem is the newly created Bullet sprites are instantly colliding with the TEnemy sprite , and thus as long as the TBullet.OnCollision is
not commented out, Bullet's aren't visible...
They are not colliding properly , what am I doing wrong here ? Any ideas? I was banging my head against the wall all day long, trying to check samples
but unfortunately they use another newer Sprite Engine...

Please Help.

Greetings
Robert

robert83
14-09-2014, 06:51 PM
Hi,

I solved it, forgot for both Bullet and Enemy movement


CollidePos := Point2(X, Y);


Though I will need to modify this... CollidePos is supposed to be the center of the enemy?
For example if I have a 32x64 enemy, the correct colidepos would be 16,32 ? and there I would set a radius of 32...
And would create the illusion that the enemy is only destroyed if the middle of it is hit... which I think is perfectly fine, I think Jamestown used this method for
the player ship, not sure if it was meant like this by design, or if it was just a shortcut with using Radius collision...

Wow can't believe this, we are going somewhere... once I'm done I'll post the entire project step-by-step ... how I did it .

Greetings
Robert

SilverWarior
14-09-2014, 07:05 PM
Glad to see you are progressing.

robert83
14-09-2014, 07:21 PM
This forum has a positive effect on me :D just by posting here I sometimes solve my own problems...

... and I also took a 3 hour walk in the City to clear my head.... after that ...it just hit me...

CollisionPos is always the center of the sprite I'm checking for collision, and I must update it onMove ,...because it is constantly moving... makes sense now.
Also radius for a circle that fits into a 32x32 square is 16...that works to now.

I did the Editor, now I'm working in Lazarus IDE :D . I've cut back on a lot of features, last time I think I did the huge mistake of wanting to do everything at once, and dying painfully in the end.

My current goals are, scrolling map, waves of enemies coming at me , firing at enemies, enemies have different hit points some take 1 hit, some 2 or 3... my ship has 3 lifes...

I enemy ship type 1 gives 25 point , type 2 50 etc.... every 1000 points you get an extra life... game will have I hope 3-4 levels...
A nice menu...with information...I'm thinking about doing a comic style briefing before each mission, nothing to fancy...

Right now with the scrolling done, and the collision working, I have everything but the artwork...

so I'm drawing right now.

Level01 is done, going from Earth to Moon Base...

Greetings
Rob

paul_nicholls
22-09-2014, 11:10 PM
Welcome back Robert :)
Glad to hear it is working so far :)