Let's Go Swimming

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Let's Go Swimming

Post by tentus »

So, I'm currently tackling the issue of coding water in my platforming game. Water has a few special properties that I want to use, but haven't figured out the best way to approach. I thought it'd be a good idea to get the community in on this.

1. Water is permeable (being liquid).
2. Water slows down the player (again, being liquid).
3. Water exerts upward force on the player continuously (buoyancy).

My game uses physics to do collision, so I made the water permeable by simply making the water shape a sensor. I am looking for a way to continually reevaluate if the player is touching the water: right now it seems to be triggered when the player is moving through an edge.
Kurosuke needs beta testers
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Let's Go Swimming

Post by kikito »

You are using the physics module, I presume.

In that case, take a look at world:setCallbacks. Note that it accepts 4 callbacks.

The first one is triggered when two objects first "collide".
The second one is triggered every frame they collide.
The third one is triggered right after they stop colliding.
The last one is a mystery.

The second one was very processor intensive on 0.6.2 on the tests I made - I haven't repeated the tests on 0.7. You will probably be better off by setting a 'inWater' property to true on the first callback and setting it to false on the third.
When I write def I mean function.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Let's Go Swimming

Post by TechnoCat »

Someone made a game that involved a person and a car recently that had water. Don't remember what it was called, he was active in IRC.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Let's Go Swimming

Post by tentus »

I tried using the remove() callback in conjunction with a self.swimming variable, but remove gets called back immediately after entering the shape. I tried scaling up the shape by a few pixels, but that didn't fix it. Anyone want to take a gander at the source? The water code is in /plugins, and the avatar that it references is in /actors. You can see the water in action on the right side of Level 2. To save a file listing all the global variables, tables, and functions, hit F12 (it will go into a folder called vomit in the kurosuke save folder).

http://tsholden.com/files/kurosuke_beta.love 2.78 mb version 101221
Kurosuke needs beta testers
User avatar
ghostwriter
Prole
Posts: 38
Joined: Sat Dec 11, 2010 11:08 pm

Re: Let's Go Swimming

Post by ghostwriter »

I made a little buoyancy simulator... it's pretty ugly (especially the code) but here's the important bit:

Code: Select all

		local vx, vy = boatBody:getLinearVelocity()
		fx, fy = 0, 0
		fx = fx - vx*boatDrag
		fy = fy - vy*boatDrag
	
		-- the tricky part is figuring out how submerged the boat is
		distanceSubmerged = (waterBody:getY() - waterH/2) - (boatBody:getY() + boatH/2)
		fractionSubmerged = -distanceSubmerged / boatH -- I want this relative to teh total boat height
		
		fy = fy - boatBuoyancy * math.min(1, fractionSubmerged) -- you can't be mroe than 100% submerged
		
		boatBody:applyForce(fx, fy)
So this works for a single rectangular body of water with a single rectangular boat the never rotates. When underwater, the boat receives a drag force that depends on its velocity. It also receives a buoyancy force that depends on its buoyancy, and the fraction of the boat that is submerged. It results in a nice "splashing" behaviour that leaves the boat sitting in the water about 1/3 submerged.

You can pull the boat around by clicking.
Attachments
buoyancy.love
(1.09 KiB) Downloaded 213 times
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Let's Go Swimming

Post by tentus »

Awesome, that helped a lot. My code ended up being rather different, I was surprised how some values didn't have the same result (buoyancy, for example, had to be kicked up to 240 to work right, but then again my gravity is much higher). In the comments of my source do you want to be credited as ghostwriter?
Kurosuke needs beta testers
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Let's Go Swimming

Post by Robin »

tentus wrote:do you want to be credited as ghostwriter?
Hehehehe…

EDIT: explanation: ghostwriters are writers who are paid to write for someone else, so the commissioner gets credit for writing it.
Help us help you: attach a .love.
User avatar
ghostwriter
Prole
Posts: 38
Joined: Sat Dec 11, 2010 11:08 pm

Re: Let's Go Swimming

Post by ghostwriter »

tentus wrote:do you want to be credited as ghostwriter?
That would be awesome! Hope it turns out well
Post Reply

Who is online

Users browsing this forum: No registered users and 70 guests