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.

DarkBASIC Discussion / Moving Distance Difficulties

Author
Message
Enlightened Gamer
14
Years of Service
User Offline
Joined: 4th Oct 2009
Location:
Posted: 31st Dec 2009 18:08
Hey guys,

Ive come across an annoying problem that really has me stumped!

I am creating a hunting style game and all is going well but I have come across a problem in the part of code to get the animal to run away from the player if it sees him/her.

I have got the object(s) (animals) to detect how close the camera (player) is and if the player gets near it it will run away but not how i want it to!

the only two outcomes I can get are:

1.have the object move to the edge of the detection area and look like it is running (while this is good I want the animal to run further then just to the edge of the area so it isnt possible to have a chasing effect)

2.I can get the object to move the required distance away from the detection area but it simply jumps to the location as it is moving fast (which is just really poor).

I have tried getting the object to run properly to the edge of the detection area and using FOR NEXT and REPEAT UNTIL to loop sections of the code and have it keep running for a select time but this results in the object jumping straight to the location yet again.

I am sorry if I am not explaining every well or going on a bit but I have been trying to figure this out for a while now and it just wont work!

Here is the code I have used to get the object to move to the edge of the detection area and have a correct running look:


Any help or advice people could give me to solve this would be great, I hate being so close to having it work the way I want and not being able to figure out why it wont work! It wouldnt be so bad if it wasnt so close to how I want it as I would know Im doing it completely wrong lol

Anyway I hope someone can help me with this.

many thanks in advance
Sinani201
17
Years of Service
User Offline
Joined: 16th Apr 2007
Location: Aperture Science Enrichment Center
Posted: 31st Dec 2009 18:44 Edited at: 31st Dec 2009 18:55
You're making the object move way too fast. The while statement will make the move as fast as the program loops, which extremely fast. Use the method that you normally use to make your objects move and apply that to the runaway. You will need to calculate the distance and position that the object will need to run, but if you know how to use sin and cos then it will be very easy. If you don't know how to do that, I can show you.

Enlightened Gamer
14
Years of Service
User Offline
Joined: 4th Oct 2009
Location:
Posted: 31st Dec 2009 20:32
I have never used sin or cos before, are they an easy thingy to learn?

I tried to move the objects in the same way that I have made them move around randomly in other areas of my code.

Here is how I made them move normally:


the above code works perfectly and gives me the movement speed and style that I am looking for but to make it run instead of walk I speeded the movement up but when I tried to use this method on the running it freezes the program, for some reason it wont exept any number lower then 1 in the "if near" statement.

I will have a look at using sin and cos as you advised but any help you could offer to make them a bit easier to use would be great.

Thanks for the advice.
Sinani201
17
Years of Service
User Offline
Joined: 16th Apr 2007
Location: Aperture Science Enrichment Center
Posted: 1st Jan 2010 23:58
I see. Forget the sin and cos, use the code that you use for random movement, but set turn to 180 (to make the animal turn around) and set distance to the distance that you the animal to walk in (since the value runaway is 25, I assume that would be your distance, but I'm not sure if 25 is the distance or just a counting variable).

Enlightened Gamer
14
Years of Service
User Offline
Joined: 4th Oct 2009
Location:
Posted: 2nd Jan 2010 01:03
"runaway" is just a counting variable.

what the code I gave in my first post (at the top) is meant to do is...

runaway starts at 0 and counts to 25, every time runaway counts and adds one the object moves 2 places forward. this should mean that the model should move 50 places but the model actually stops moving once it is no longer "nearby" (nearby = 500 places from the camera).

What I want to happen is if the camera gets closer then 500 to the model the model then runs out of the 500 range and keeps going for a further 200-300.

I think that as soon as the model is no longer "nearby" that it stops counting through the "runaway" code and therefore the model returns to its random movement instead of carring on running but while I think this is the problem I cant think of a way around this as to my knowledge (which is limited lol) the runaway code needs to be within the nearby if statement overwise it will not work.

It really has me stumped. I have spent quiet a few hours trying to solve this but nothing I do seems to get it to work... it is really frustrating! lol
Sinani201
17
Years of Service
User Offline
Joined: 16th Apr 2007
Location: Aperture Science Enrichment Center
Posted: 2nd Jan 2010 01:46 Edited at: 2nd Jan 2010 01:50
Oh, I get it now.
In the main post, you talked about how the animal ran away at light-speeds. Now it seems you have a different problem.

So basically, you need to make two variables: one for being near, and one for being in the state of running away. Also, a FOR NEXT loop would be much more fitting for running away instead of WHILE. Here is how I would re-write your code for running away:



This is a bit off topic, but have you noticed that there hasn't been a single post on this forum since early yesterday except in this thread? It seems a bit weird...

Enlightened Gamer
14
Years of Service
User Offline
Joined: 4th Oct 2009
Location:
Posted: 2nd Jan 2010 02:39
thanks for the help, i will give it a try later on but it looks like it will work. I did try a FOR NEXT loop but i used it to replace the WHILE instead of within it!

I have noticed that it does seem a little quiet around here lately.
Enlightened Gamer
14
Years of Service
User Offline
Joined: 4th Oct 2009
Location:
Posted: 2nd Jan 2010 13:04 Edited at: 2nd Jan 2010 13:05
I think that you may have been right in your first post, I need to find away to make the movement slower.

Here is how my code looks now:


as you can see I took your advice of using WHILE as a TRUE or FALSE variable and putting a FOR NEXT inside of it. Wile this looks like it should work it actually makes the object move the correct distance but it moves extremely quickly (jumps) to the distance.

I dont know how I am going to make the movement slower though, the only way I have learnt of making a object move slowly is:


but when I have tried using this kind of thing the program crashes if I have "move" as anything lower than 1.

I am seriously thinking of dropping the runaway feature as it is giving me a lot of scoop creep in my project but without it the program would be like a simple duck shoot and that just isnt what I set out to make!

I will have a look at the SIN() and COS() and try to figure out something from that.

Thanks for all the advice you have been giving me.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 2nd Jan 2010 16:17
@Enlightened
I'm not on a computer with DarkBASIC at the moment so I can't test anything. But from what I'm reading, it seems a little bit like you are "trapping" the running away aspect into it's own loop outside of all ofthe other events that might be happening in the game. The slow down I think Sinani201 was getting at was by perhaps spreading out the escaping similarly to how your regular movement is controlled. Some animals may behave differently from others. You may have arrays holding information about an animal. You may have a value stored that represents the minimum distance of detection before the animal starts running, and you may have a maximum distance that the animal runs to before it stops running. This way you can have different animals run different distances and if you pursue certain ones always keeping them within the minimum detection distance, they will keep running.


In pseudo code, the general approach might be something like this:




The above pseudo code will check each animal and it's distance from the player once each time through the main loop. If a particular animal is close enough, it will run once each time the main loop loops until it is out of range. This way, all of the game elements are working within the main loop and the game is not waiting for a single animal to complete it's running of an entire distance. Using arrays to hold different speeds and distances for the animals, you can have them scatter and move somewhat uniquely.

Enjoy your day.
Enlightened Gamer
14
Years of Service
User Offline
Joined: 4th Oct 2009
Location:
Posted: 2nd Jan 2010 20:33
Hello Latch,

My code is very simular to the pseudo code that you have posted (just not quite as well laid out as yours is lol).

The trouble I am having is that I am not able to keep the animal running at the correct speed once it is out of the detection range. I have set a detection range of 500 for the animal and I have no problem getting the animal to run away but it stops running once it gets to 501 spaces away from the player but I am wanting the animal to keep running until it is 700-800 away from the player.

I had a brainwave and tried this:


My hope was that it would make the animal run out of the detection area which is now 700 unfortunatly this did not have the desired outcome as when the animal was 501 away from the player maxdist# would return from 700 to 500.

Im getting really annoyed with it now, all I need to do is get the animal to keep running for 200-300 once it is out of the "nearby" detection distance which didnt seem this difficult when I had the idea but it is turning out to be a pain in the bottom!

One bright side of all this frustration is that I am learn new things almost everyday now lol
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 3rd Jan 2010 03:13 Edited at: 3rd Jan 2010 03:17
I see what you're saying now. You can get the thing to run and at the proper speed, but you want it to keep running beyond the detection distance. So if a hunter snuck up on something only a few meters away (the detection distance), then the animal might take off running for a few hundred meters.

Ok. So let's take a look at using the animalinfo#() array I was proposing.



The pseudo code above uses the array to store information about the running state of the animal. When animalinfo#(animal,0) is 1, the animal is running and we can skip the check for near which should save some time. Only the animals that are not running will be checked against near. The distance that the animal has run total since it started running is stored in animalinfo#(animal,6). That is compared to maximum distance that it can run which is stored in animalinfo#(animal,5) so we can stop it from running when it has gone that far.

This will allow you to have a detection distance and a max distance that the animal will run before it stops.

Enjoy your day.
Enlightened Gamer
14
Years of Service
User Offline
Joined: 4th Oct 2009
Location:
Posted: 3rd Jan 2010 23:26
Thank you for your pseudo code Latch, it looks extremely promising and as soon as I get enough spare time I will use it to code my program.

I cant believe I did not think of using arrays as I have just learnt them over the past few months on Visual Basic so I should of thought about it!

One very big bonus of doing it the way you have suggested over the way I was doing it is that instead of having a lot of code all over the place like I do at the minute I can modify this to handle many of the things I want my program to do.

Thank you Latch and Sinani for your advice on this problem.
Enlightened Gamer
14
Years of Service
User Offline
Joined: 4th Oct 2009
Location:
Posted: 30th Jan 2010 11:48
Thansk for all your help guys,

I got this problem resolved using a couple of very simple IF statements. (I was over complicating things alot! lol)

Many thanks

Login to post a reply

Server time is: 2024-04-23 13:51:13
Your offset time is: 2024-04-23 13:51:13