Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Raycast or Collision

Author
Message
Juxton
9
Years of Service
User Offline
Joined: 21st Jan 2015
Location:
Posted: 21st Mar 2015 21:58
In the game I'm developing, I'm wanting an animated sprite to collide with another sprite at a certain animation frame.

I tried first just using collision, but it seems that collision is only recorded when the two sprites first touch (at least that's how it was behaving for me), and I want it to detect collision as long as the frame occurs while the sprites are overlapped all the way through the target sprite.

So I tried raycast, which is working great, except that my raycast dimensions don't seem to be staying confined to the target sprites. The fishspriteg sprite doesn't even have to be touching the pool sprites to cause a collision.

I'm going to include the pertinent code, but be nice. I'm still a beginner

The reason I have so much code for each individual pool is because I tried it with "for n = 1 to 6" and "pool[n]", but it seemed to create the new sprite plpfalls for each pool, so when the collision was zero, I had six plpfalls sprites falling around the screen. Plus each sprite starts in a different position that has a random x but a particular y. I don't think I can use the "n" procedure for that.

Anyway, here's the code. Is raycast the way to go for what I want, and am I trying to use it correctly?

And though I said be nice, if anyone sees other code that would work better a different way, I wouldn't be hurt if you pointed it out.

Juxton
9
Years of Service
User Offline
Joined: 21st Jan 2015
Location:
Posted: 21st Mar 2015 22:01
Oops, ignore the rem about the virtual buttons.
29 games
18
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 22nd Mar 2015 00:31
Hi Juxton

Okay, I managed to get your code running. I had to add in a virtual resolution at the start and declare an array for arrtower[].

I'm not entirely sure what the code is supposed to do but I'm seeing is some white squares falling and another square that I believe the player is supposed to control.

Looking at the various ray cast you're doing, you're ray casting diagonally 100 pixels down and to the right.

If none of the ray cast collides with a pool(?) then the code goes to the plpfalls() function where the player, for want of a better phrase, "flips out", i.e. starts spinning and bouncing around.

Anyway, I think this goes a bit beyond whether you should use a ray cast or sprite collision.

Quote: "seems that collision is only recorded when the two sprites first touch "


I'm guessing you tried the GetSpriteCollision command, which will return a 1 as long as the sprites are overlapping. There might something with the way you're using this command that's causing the behaviour you're seeing. To be honest, you're code is a bit of a mess* so this wouldn't surprise me.

Quote: "The reason I have so much code for each individual pool is because I tried it with "for n = 1 to 6" and "pool[n]", but it seemed to create the new sprite plpfalls for each pool, so when the collision was zero, I had six plpfalls sprites falling around the screen. Plus each sprite starts in a different position that has a random x but a particular y. I don't think I can use the "n" procedure for that"


Again, I think this could be caused by you're implementation. You should be able to loop through the "pool" sprites without issue. Chances are you're using a for-to-next loop.

I would also advise against creating a sprite during the game loop, as this can slow the game down, but rather create it before the start of the main loop and then just hide it until it's needed.

Here is a brief example that might give you some ideas:




*When I started out my code was as bad if not worse than this so don't feel bad. Heck, I wasn't even using arrays for the first couple of games I made. It's all part of the learning process.

Also you can edited your posts. There is an edit link to the bottom left of any post you've created, click that and it'll take you to an edit page.

Juxton
9
Years of Service
User Offline
Joined: 21st Jan 2015
Location:
Posted: 22nd Mar 2015 01:17
Quote: "Okay, I managed to get your code running. I had to add in a virtual resolution at the start and declare an array for arrtower[]."


Like I said, I didn't include all my code. I didn't even mean to include the arrtower arrays because they're not part of what I'm trying to figure out.


Quote: "
I'm not entirely sure what the code is supposed to do but I'm seeing is some white squares falling and another square that I believe the player is supposed to control."


Sorry, I guess I wasn't very clear on what I'm trying to do. The white squares are simply placeholders for the art that will come later. I made them 100x100, and yes, they are supposed to "fall" down the screen. They're part of a scrolling background.

The goal is to get the player sprite to collide with the squares during a certain animation frame. If it doesn't, it activates the plpfalls function that plays a death animation.

The player is a fish trying to jump from pool to pool on a scrolling background. At the point when the animation shows the fish coming back down and hitting the water, the player should be overlapping one of the pools or else he's killed.

It's important that the collision occurs during that final frame of animation.

Quote: "
Looking at the various ray cast you're doing, you're ray casting diagonally 100 pixels down and to the right."


I'm attempting to make a raycast the same size as the squares, so that should be right as long as it's 100x100. I'm going by the example given in the AppGameKit help section. the raycasts should also stay with the squares, which I just realized I don't think I coded for. I don't even know if that's possible.

Quote: "
If none of the ray cast collides with a pool(?) then the code goes to the plpfalls() function where the player, for want of a better phrase, "flips out", i.e. starts spinning and bouncing around."


The ray cast is supposed to be part of the pool, and if the player doesn't collide with the raycast, then the plpfalls function plays. And, yes, that's how the player behaves. It's a spinning, dazed fish that will fly off the screen once I fix the collide bits so it doesn't react to any other sprites. I haven't included that code yet so that's why it bounces all over the place. I'm trying to get the hard stuff out of the way.

Quote: "
I'm guessing you tried the GetSpriteCollision command, which will return a 1 as long as the sprites are overlapping. There might something with the way you're using this command that's causing the behaviour you're seeing."


I've tried GetSpriteCollision, GetPhysicsCollision, making the pools sensors, and now ray casting.

The first two commands only worked when the edges of the sprites touched. If I was in the middle of a sprite (and I made the pool sprite stretch across the screen just to test it) it doesn't detect the collision and goes straight to plpfalls.

Quote: "
To be honest, you're code is a bit of a mess* so this wouldn't surprise me."


Could you please explain? Most of the main loop is full of code taken directly from code given to me by BatVink (the increased speed in scrolling), so I assume you're referring to the rest of the code, but I don't see what the mess is you're speaking of.

As far as I can tell, I'm coding each command the way I've read it in the help guides, but I wouldn't doubt that while each command may be sound, they may not work together. Then again, I could be coding each command wrong.

Quote: "
I would also advise against creating a sprite during the game loop, as this can slow the game down, but rather create it before the start of the main loop and then just hide it until it's needed."


The only place I see that I created a sprite other than before the loop is in the plpfalls function, and I did that because it wouldn't recognize it if I created the sprite before the loop. It gave me the error that I hadn't initialized it and such.

In the main loop itself, I positioned sprites, but didn't create them, unless I have some code in there I'm not seeing.

I'll look into your code, thanks.

I might have been naive to think you wouldn't need to see all my code to see what's causing problems, so here it is.



Thank you for all your help.

Login to post a reply

Server time is: 2024-06-16 22:45:38
Your offset time is: 2024-06-16 22:45:38