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 / How should I handle collision using setSpritePosition?

Author
Message
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 22nd Sep 2012 07:11
I use setSpritePosition to move my player around. I have a ton of walls and want collision. Using setSpritePhysicsOn doesn't work when you use setSpritePosition. I can do a simple collision check but I am not sure how to do this to test all the walls in the level. I don't want to make several hundred checks each loop. Thanks in advance.
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 22nd Sep 2012 08:14
Quote: "I can do a simple collision check but I am not sure how to do this to test all the walls in the level. I don't want to make several hundred checks each loop. "

You'll have to perform some sort of check against each wall (unless you've set up your program in such a way that it doesn't need collision detection e.g tiles). You could do a distance check to narrow it down but you'll still have to check each wall (either distance or collision). You can also check for collision every x amount of milliseconds.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 22nd Sep 2012 09:06
You could put all your walls in a particular group using setSpriteGroup and then rayCastGroup around your player to see what is around him.

Another way would be to set your character up as a physics sprite and let box2d take care of collisions for you...


this.mess = abs(sin(times#))
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 22nd Sep 2012 20:44
Quote: "Another way would be to set your character up as a physics sprite and let box2d take care of collisions for you..."


I have physics on both the walls and my player. The problem is that setSpritePosition overrides the physics.

Maybe I could move the player in a different fashion to allow box2d to work. Right now when you click a button (such as up, left, right, or down) I run through a small loop increasing the movement by .25 until it reaches a total movement of 10%.

Here is my movement code for north or up.


Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 22nd Sep 2012 21:20
I use the SetSpritePhysicsImpulse command to move my player. It lets you set a direction and speed and then the physics takes over.

Decide what you want the x/y direction is and how fast and then something like this:


Cheers,
Ancient Lady
AGK Community Tester
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 22nd Sep 2012 22:43
Impulse works but I tend to use velocity as it's easier to control. Impulse keeps building and building whereas velocity is a set speed and your player still reacts to other objects.


this.mess = abs(sin(times#))
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd Sep 2012 01:25
The same code works with velocity, just don't worry about the current position. It reacts much faster.

Cheers,
Ancient Lady
AGK Community Tester
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 24th Sep 2012 19:54
I got velocity to work but still having problems with physics not working. If I set the wall to physicsOn 2, then I can push the wall around. Any other setting and I walk right through it.

Here is my character code


Here is the code for my wall


Here is the check to see which button is pushed


Here is the new movement and animation code


Two things. I am not sure how to stop the velocity so I set it to 0,0. Also I am not sure if the collision isn't picking up do to the loop I am running inside the function.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 24th Sep 2012 20:54
Your player should be dynamic: setSpritePhysicsOn(SMW, 2)

Do you want the wall to move? If not, make it static: setSpritePhysicsOn (wall1, 1)

Do you want your player to stop when they reach a wall or bounce?

Cheers,
Ancient Lady
AGK Community Tester
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 24th Sep 2012 20:56
And I don't really see what you are accomplishing with the do loop in your function. It looks like it just adjusts a value until it reaches some value and then you stop the player. This is definitely killing your collision.

Cheers,
Ancient Lady
AGK Community Tester
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 24th Sep 2012 21:10 Edited at: 24th Sep 2012 21:11
OK I set the player physics to 2 and gravity to 0,0. Now the collision works but the player now spins when he hits the wall.

How should I stop the velocity? I was moving him in the loop and then using the number to determine the stopping point to stop the velocity and stop the animation.

edit: I want the player to stop when he hits the wall.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 24th Sep 2012 22:09
Do you always want to stop the player when it hits an object?

If so, you can check for collision with a wall each main loop (take the loops out of your north(), NW(), etc. functions, they aren't doing anything useful) and stop if there is a collision.

Here is some sample code based on yours:


I might also suggest creating a UDT with x and y velocity and start and stop frames for each direction. Then make an array of them and initialise with all the values for each direction, use the 'dir' value (adjust the sprite ids and the returned value so that it matches the appropriate entry in the array). Then you can have one function that you pass an index and it handles the rest. And you would not not the select or if/elseif setup.

Cheers,
Ancient Lady
AGK Community Tester
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 25th Sep 2012 20:00
OK I got the collision working now. Thank goodness I didn't want to have to check every single wall individuality.

I used the loop in the functions to control the distance traveled. The player will roll a die and then move that many spaces. The loop determined how much distance to move for one space. Now I need a way to only allow one button push to move the player 10% of the Y axis and 12% of the X axis.
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 29th Sep 2012 06:24
well, everything worked correctly. Until I walked off the screen and realized I need screen scrolling. So I added that in and now my collision is broken again. I can't even do a collision check. I printed out the collision to make sure it was working and nothing. If the screen is moving I have no collision. Is there a different collision command for a scrolling screen? Thanks again.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 29th Sep 2012 11:23
You might need to change screen co-ordinate a into world co-ordinate a using screenToWorldX and screenToWorldY?


this.mess = abs(sin(times#))
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 30th Sep 2012 07:13
Ok well here is my code it. As far as I can tell it never checks for a collision.

definitions:



loop
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 30th Sep 2012 08:38
I'm not sure about your collision but a small piece of programming advice: it's generally not a good idea to do an equality comparison between two floats.

E.g if currentY# = newY# +negY#

currentY# will rarely, if ever, be equal in the computer's eyes to newY# + negY#. It's best to cater for some small degree of variance.

A quick work around:

if abs(currentY# - (newY# + negY#)) < 0.2

Where 0.2 is the variance permitted (you can change this).

Cheers

Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 30th Sep 2012 17:44
Thanks. For the tip.
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 1st Oct 2012 18:04
Ok I found my problem. It turns out that the collision sticks to the screen. When I move my view the sprite moves correctly but the collision seems to be fixedToScreen. So the collision is always the same distance from my player since my player is fixedToScreen. Any thoughts on how to get the collision to stay with the sprite it is assigned to?
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Oct 2012 18:07
The collision shape will only be fixed to the screen if the player sprite is also fixed using "fixSpriteToScreen". It's not really a good idea to use this for anything you want to interact with the rest of the "world".


this.mess = abs(sin(times#))
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 1st Oct 2012 18:15
My wall collision is what seems to be fixed to screen. The wall itself is not. The wall moves correctly when I change my view offset but the wall's collision is fixed to the screen.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Oct 2012 18:19
Try using "fixSpriteToScreen(wallSpriteID,0)", that should stop it from being fixed to the screen but it is set to zero by default so if that fixes the problem it must be set to one somewhere in your code.


this.mess = abs(sin(times#))
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 1st Oct 2012 18:36
Didn't fix the problem. This is strange. I added a second wall and it does the exact same thing. I played around with the shape and the physicsOn options and nothing seems to be working.

Is there a way to place the wall in world coordinates?
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Oct 2012 18:38
When you position a sprite it is always in world co-ordinates unless you fix to screen.

Have you checked for that in your code? Maybe you could post something to debug?


this.mess = abs(sin(times#))
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 1st Oct 2012 18:55
I uploaded my whole file. just put into the basic folder. I know my code is not clean and it isn't perfect.

I only have one call to the fixSpriteToScreen that is for (move) which really should say player.

How to move and test. First you need to hit the up arrow button in the middle of the screen to roll the dice. I fixed the dice to always give 10 moves for right now. The buttons on the left move the player, disregard the direction of the arrows I need to fix them. The position of the button will tell you the direction it moves. The middle button switches which character the player is controlling.

North and East buttons move the screen and you will see he walks through the wall. The other buttons only move the player. After you walk through the wall, use the NorthWest and NorthEast buttons to walk up and hit the collision from the wall.

password is abc

Attachments

Login to view attachments
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Oct 2012 19:17
Yes, the player is being fixed to the screen:
FixSpriteToScreen(move, 1)

Your collision system seems to work if you don't set this.


this.mess = abs(sin(times#))
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 1st Oct 2012 19:30
Yes that is true. The problem lies with the wall's collision. If the player is fixed to the screen the wall's collision shouldn't be.

Maybe I can move the player at the same speed that the scrolling is happening.
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 1st Oct 2012 19:38
ok I turned off the fixSpriteToScreen for the player and now I am moving him when the screen scrolls. I just have to match the speed and I should be good.
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 1st Oct 2012 21:50
Thanks Baxslash, I am all set moving perfect. Colliding perfect. All 8 directions working perfect. I have never been so happy to hit a wall.

Now I can work on updating the screen position when I switch characters. That should be fun.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Oct 2012 22:14
Excellent! Looks like a fun project


this.mess = abs(sin(times#))

Login to post a reply

Server time is: 2024-05-04 11:42:05
Your offset time is: 2024-05-04 11:42:05